所有要求都是用sql sever语句实现创建一个数据库,至少里面有5个表,每个表至少有十条数据,至少有两个表具有外键,使用模式匹配查询,使用top查询,in查询,降序查询,使用count集合函数查询,分组统计查询,使用连接条件的多表查询,比较运算符的子查询,使用in的子查询,创建视图并使用,创建索引,创建唯一性约束,检查约束,默认值约束,外键约束,创建一个存储过程并使用在某个表上创建一个插入和更新的dml触发
时间: 2023-12-12 08:03:15 浏览: 80
器,同时保证所有表和约束的命名规范化。
可以参考以下的 SQL Server 语句来实现:
-- 创建数据库
CREATE DATABASE MyDatabase;
GO
-- 使用 MyDatabase 数据库
USE MyDatabase;
GO
-- 创建表 Table1
CREATE TABLE Table1 (
ID INT PRIMARY KEY,
Name NVARCHAR(50) NOT NULL,
Age INT NOT NULL,
Gender NVARCHAR(10) NOT NULL
);
GO
-- 插入数据到 Table1
INSERT INTO Table1 (ID, Name, Age, Gender)
VALUES (1, 'Alice', 20, 'Female'),
(2, 'Bob', 25, 'Male'),
(3, 'Charlie', 30, 'Male'),
(4, 'David', 35, 'Male'),
(5, 'Eva', 40, 'Female'),
(6, 'Frank', 45, 'Male'),
(7, 'Grace', 50, 'Female'),
(8, 'Henry', 55, 'Male'),
(9, 'Ivy', 60, 'Female'),
(10, 'Jack', 65, 'Male');
GO
-- 创建表 Table2
CREATE TABLE Table2 (
ID INT PRIMARY KEY,
Table1ID INT NOT NULL,
Address NVARCHAR(100) NOT NULL,
FOREIGN KEY (Table1ID) REFERENCES Table1(ID)
);
GO
-- 插入数据到 Table2
INSERT INTO Table2 (ID, Table1ID, Address)
VALUES (1, 1, 'Beijing'),
(2, 2, 'Shanghai'),
(3, 3, 'Guangzhou'),
(4, 4, 'Shenzhen'),
(5, 5, 'Chongqing'),
(6, 6, 'Hangzhou'),
(7, 7, 'Nanjing'),
(8, 8, 'Wuhan'),
(9, 9, 'Chengdu'),
(10, 10, 'Xi''an');
GO
-- 创建表 Table3
CREATE TABLE Table3 (
ID INT PRIMARY KEY,
Table1ID INT NOT NULL,
Score INT NOT NULL,
FOREIGN KEY (Table1ID) REFERENCES Table1(ID)
);
GO
-- 插入数据到 Table3
INSERT INTO Table3 (ID, Table1ID, Score)
VALUES (1, 1, 80),
(2, 2, 85),
(3, 3, 90),
(4, 4, 95),
(5, 5, 100),
(6, 6, 80),
(7, 7, 85),
(8, 8, 90),
(9, 9, 95),
(10, 10, 100);
GO
-- 创建表 Table4
CREATE TABLE Table4 (
ID INT PRIMARY KEY,
Name NVARCHAR(50) NOT NULL UNIQUE,
Description NVARCHAR(100) NOT NULL
);
GO
-- 插入数据到 Table4
INSERT INTO Table4 (ID, Name, Description)
VALUES (1, 'Apple', 'A kind of fruit'),
(2, 'Banana', 'Another kind of fruit'),
(3, 'Carrot', 'A kind of vegetable'),
(4, 'Durian', 'A kind of fruit with strong smell'),
(5, 'Eggplant', 'A kind of vegetable'),
(6, 'Fig', 'A kind of fruit'),
(7, 'Grape', 'A kind of fruit'),
(8, 'Honeydew', 'A kind of fruit'),
(9, 'Iceberg lettuce', 'A kind of vegetable'),
(10, 'Jackfruit', 'A kind of fruit');
GO
-- 创建表 Table5
CREATE TABLE Table5 (
ID INT PRIMARY KEY,
Table4ID INT NOT NULL,
Amount INT NOT NULL,
FOREIGN KEY (Table4ID) REFERENCES Table4(ID)
);
GO
-- 插入数据到 Table5
INSERT INTO Table5 (ID, Table4ID, Amount)
VALUES (1, 1, 10),
(2, 2, 20),
(3, 3, 30),
(4, 4, 40),
(5, 5, 50),
(6, 6, 60),
(7, 7, 70),
(8, 8, 80),
(9, 9, 90),
(10, 10, 100);
GO
-- 创建视图 View1
CREATE VIEW View1
AS
SELECT Table1.ID, Table1.Name, Table2.Address, Table3.Score
FROM Table1
LEFT JOIN Table2 ON Table1.ID = Table2.Table1ID
LEFT JOIN Table3 ON Table1.ID = Table3.Table1ID;
GO
-- 创建索引 Index1
CREATE INDEX Index1 ON Table1 (Name);
GO
-- 创建唯一性约束 Constraint1
ALTER TABLE Table4 ADD CONSTRAINT Constraint1 UNIQUE (Name);
GO
-- 创建检查约束 Constraint2
ALTER TABLE Table4 ADD CONSTRAINT Constraint2 CHECK (LEN(Description) <= 100);
GO
-- 创建默认值约束 Constraint3
ALTER TABLE Table5 ADD CONSTRAINT Constraint3 DEFAULT 0 FOR Amount;
GO
-- 创建外键约束 Constraint4
ALTER TABLE Table2 ADD CONSTRAINT Constraint4 FOREIGN KEY (Table1ID) REFERENCES Table1(ID) ON DELETE CASCADE;
GO
-- 创建存储过程 Procedure1
CREATE PROCEDURE Procedure1
AS
BEGIN
SELECT COUNT(*) FROM Table4;
END;
GO
-- 创建插入触发器 Trigger1
CREATE TRIGGER Trigger1 ON Table1
FOR INSERT
AS
BEGIN
INSERT INTO Table2 (ID, Table1ID, Address)
SELECT ID, ID, 'Unknown' FROM INSERTED;
END;
GO
-- 创建更新触发器 Trigger2
CREATE TRIGGER Trigger2 ON Table1
FOR UPDATE
AS
BEGIN
UPDATE Table2
SET Address = 'Unknown'
FROM Table2
JOIN INSERTED ON Table2.Table1ID = INSERTED.ID;
END;
GO
-- 使用模式匹配查询
SELECT * FROM Table1 WHERE Name LIKE 'A%';
-- 使用 TOP 查询
SELECT TOP 3 * FROM Table2;
-- 使用 IN 查询
SELECT * FROM Table1 WHERE ID IN (1, 3, 5);
-- 使用降序查询
SELECT * FROM Table3 ORDER BY Score DESC;
-- 使用 COUNT 集合函数查询
SELECT COUNT(*) FROM Table4;
-- 分组统计查询
SELECT Table4.Name, SUM(Table5.Amount) AS TotalAmount
FROM Table4
JOIN Table5 ON Table4.ID = Table5.Table4ID
GROUP BY Table4.Name;
-- 使用连接条件的多表查询
SELECT Table1.Name, Table2.Address, Table3.Score
FROM Table1
JOIN Table2 ON Table1.ID = Table2.Table1ID
LEFT JOIN Table3 ON Table1.ID = Table3.Table1ID
WHERE Table3.Score IS NULL;
-- 比较运算符的子查询
SELECT * FROM Table1 WHERE Age > (SELECT AVG(Age) FROM Table1);
-- 使用 IN 的子查询
SELECT * FROM Table4 WHERE ID IN (SELECT DISTINCT Table4ID FROM Table5);
-- 调用视图 View1
SELECT * FROM View1;
-- 调用存储过程 Procedure1
EXEC Procedure1;
-- 插入数据到 Table1,触发插入触发器 Trigger1
INSERT INTO Table1 (ID, Name, Age, Gender)
VALUES (11, 'Kate', 30, 'Female');
-- 更新数据到 Table1,触发更新触发器 Trigger2
UPDATE Table1 SET Age = 40 WHERE ID = 1;
阅读全文