所有要求都是用sql sever语句实现创建一个数据库,至少里面有5个表,每个表至少有十条数据,至少有两个表具有外键,使用模式匹配查询,使用top查询,in查询,降序查询,使用count集合函数查询,分组统计查询,使用连接条件的多表查询,比较运算符的子查询,使用in的子查询创建视图并使用创建索引创建唯一性约束,检查约束,默认值约束,外键约束创建一个存储过程并使用在某个表上创建一个插入和更新的dml触发器
时间: 2023-12-12 12:03:14 浏览: 58
好的,以下是实现要求的 SQL Server 语句:
-- 创建数据库
CREATE DATABASE mydatabase;
-- 使用数据库
USE mydatabase;
-- 创建表1
CREATE TABLE table1 (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
gender VARCHAR(10)
);
-- 插入数据到表1
INSERT INTO table1 (id, name, age, gender)
VALUES (1, 'Tom', 25, 'Male'),
(2, 'Lucy', 22, 'Female'),
(3, 'Jack', 28, 'Male'),
(4, 'Alice', 23, 'Female'),
(5, 'Bob', 30, 'Male'),
(6, 'Mary', 26, 'Female'),
(7, 'John', 27, 'Male'),
(8, 'Lily', 24, 'Female'),
(9, 'David', 29, 'Male'),
(10, 'Sandy', 21, 'Female');
-- 创建表2
CREATE TABLE table2 (
id INT PRIMARY KEY,
address VARCHAR(100),
phone VARCHAR(20),
email VARCHAR(50)
);
-- 插入数据到表2
INSERT INTO table2 (id, address, phone, email)
VALUES (1, 'Beijing', '1234567890', 'tom@example.com'),
(2, 'Shanghai', '2345678901', 'lucy@example.com'),
(3, 'Hangzhou', '3456789012', 'jack@example.com'),
(4, 'Guangzhou', '4567890123', 'alice@example.com'),
(5, 'Chengdu', '5678901234', 'bob@example.com'),
(6, 'Chongqing', '6789012345', 'mary@example.com'),
(7, 'Nanjing', '7890123456', 'john@example.com'),
(8, 'Wuhan', '8901234567', 'lily@example.com'),
(9, 'Xi\'an', '9012345678', 'david@example.com'),
(10, 'Shenzhen', '0123456789', 'sandy@example.com');
-- 创建表3
CREATE TABLE table3 (
id INT PRIMARY KEY,
table1_id INT,
content VARCHAR(200),
FOREIGN KEY (table1_id) REFERENCES table1(id)
);
-- 插入数据到表3
INSERT INTO table3 (id, table1_id, content)
VALUES (1, 1, 'This is content 1 for Tom'),
(2, 1, 'This is content 2 for Tom'),
(3, 2, 'This is content 1 for Lucy'),
(4, 2, 'This is content 2 for Lucy'),
(5, 3, 'This is content 1 for Jack'),
(6, 3, 'This is content 2 for Jack'),
(7, 4, 'This is content 1 for Alice'),
(8, 4, 'This is content 2 for Alice'),
(9, 5, 'This is content 1 for Bob'),
(10, 5, 'This is content 2 for Bob');
-- 创建表4
CREATE TABLE table4 (
id INT PRIMARY KEY,
table1_id INT,
content VARCHAR(200),
FOREIGN KEY (table1_id) REFERENCES table1(id)
);
-- 插入数据到表4
INSERT INTO table4 (id, table1_id, content)
VALUES (1, 6, 'This is content 1 for Mary'),
(2, 6, 'This is content 2 for Mary'),
(3, 7, 'This is content 1 for John'),
(4, 7, 'This is content 2 for John'),
(5, 8, 'This is content 1 for Lily'),
(6, 8, 'This is content 2 for Lily'),
(7, 9, 'This is content 1 for David'),
(8, 9, 'This is content 2 for David'),
(9, 10, 'This is content 1 for Sandy'),
(10, 10, 'This is content 2 for Sandy');
-- 创建表5
CREATE TABLE table5 (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
gender VARCHAR(10),
table3_id INT,
table4_id INT,
FOREIGN KEY (table3_id) REFERENCES table3(id),
FOREIGN KEY (table4_id) REFERENCES table4(id)
);
-- 插入数据到表5
INSERT INTO table5 (id, name, age, gender, table3_id, table4_id)
VALUES (1, 'Tom', 25, 'Male', 1, 7),
(2, 'Lucy', 22, 'Female', 3, 8),
(3, 'Jack', 28, 'Male', 5, 9),
(4, 'Alice', 23, 'Female', 7, 10),
(5, 'Bob', 30, 'Male', 9, 6),
(6, 'Mary', 26, 'Female', 2, 5),
(7, 'John', 27, 'Male', 4, 4),
(8, 'Lily', 24, 'Female', 6, 3),
(9, 'David', 29, 'Male', 8, 2),
(10, 'Sandy', 21, 'Female', 10, 1);
-- 模式匹配查询
SELECT * FROM table1 WHERE name LIKE '%a%';
-- TOP查询
SELECT TOP 3 * FROM table2;
-- IN查询
SELECT * FROM table1 WHERE name IN ('Tom', 'Lucy', 'Jack');
-- 降序查询
SELECT * FROM table2 ORDER BY id DESC;
-- COUNT集合函数查询
SELECT COUNT(*) FROM table3;
-- 分组统计查询
SELECT gender, COUNT(*) FROM table1 GROUP BY gender;
-- 连接条件的多表查询
SELECT table1.name, table2.email FROM table1 JOIN table2 ON table1.id = table2.id;
-- 比较运算符的子查询
SELECT * FROM table1 WHERE age > (SELECT AVG(age) FROM table1);
-- IN的子查询
SELECT * FROM table2 WHERE id IN (SELECT table2_id FROM table5);
-- 创建视图
CREATE VIEW myview AS
SELECT name, age FROM table1;
-- 使用视图查询
SELECT * FROM myview;
-- 创建索引
CREATE INDEX idx_table1_name ON table1 (name);
-- 创建唯一性约束
ALTER TABLE table2 ADD CONSTRAINT unique_email UNIQUE (email);
-- 创建检查约束
ALTER TABLE table1 ADD CONSTRAINT check_age CHECK (age >= 18 AND age <= 60);
-- 创建默认值约束
ALTER TABLE table2 ADD CONSTRAINT default_phone DEFAULT '0000000000' FOR phone;
-- 创建外键约束
ALTER TABLE table3 ADD CONSTRAINT fk_table1_id FOREIGN KEY (table1_id) REFERENCES table1(id);
-- 创建存储过程
CREATE PROCEDURE myprocedure
AS
SELECT * FROM table1;
-- 使用存储过程
EXEC myprocedure;
-- 创建插入的DML触发器
CREATE TRIGGER mytrigger_insert
ON table1
FOR INSERT
AS
INSERT INTO table3 (id, table1_id, content)
SELECT id, id, 'This is content for ' + name
FROM inserted;
-- 创建更新的DML触发器
CREATE TRIGGER mytrigger_update
ON table1
FOR UPDATE
AS
UPDATE table3
SET content = 'This is updated content for ' + name
FROM table3
INNER JOIN inserted ON table3.table1_id = inserted.id;
以上是实现要求的 SQL Server 语句,希望能够帮助到你。
阅读全文