所有要求都是用sql sever语句实现 创建一个数据库,至少里面有5个表,每个表至少有十条数据,至少有两个表具有外键 ,使用模式匹配查询,使用top查询,in查询,降序查询,使用count集合函数查询,分组统计查询,使用连接条件的多表查询,比较运算符的子查询,使用in的子查询 创建视图并使用 创建索引 创建唯一性约束,检查约束,默认值约束,外键约束 创建一个存储过程并使用 在某个表上创建一个插入和更新的dml触发器
时间: 2023-12-11 17:02:15 浏览: 95
好的,以下是 SQL Server 实现上述要求的代码:
1. 创建数据库并添加数据表
CREATE DATABASE MyDatabase;
USE MyDatabase;
CREATE TABLE Table1 (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Gender VARCHAR(10)
);
INSERT INTO Table1 VALUES (1, 'John', 25, 'Male');
INSERT INTO Table1 VALUES (2, 'Alice', 30, 'Female');
INSERT INTO Table1 VALUES (3, 'Bob', 45, 'Male');
INSERT INTO Table1 VALUES (4, 'Jane', 20, 'Female');
INSERT INTO Table1 VALUES (5, 'Mike', 35, 'Male');
-- ...
CREATE TABLE Table2 (
ID INT PRIMARY KEY,
Category VARCHAR(50),
Description VARCHAR(100)
);
INSERT INTO Table2 VALUES (1, 'Electronics', 'Smartphone');
INSERT INTO Table2 VALUES (2, 'Home Appliances', 'Refrigerator');
INSERT INTO Table2 VALUES (3, 'Clothing', 'Shirt');
INSERT INTO Table2 VALUES (4, 'Books', 'Novel');
INSERT INTO Table2 VALUES (5, 'Sports', 'Tennis racket');
-- ...
CREATE TABLE Table3 (
ID INT PRIMARY KEY,
Table1ID INT FOREIGN KEY REFERENCES Table1(ID),
ProductName VARCHAR(50),
Price DECIMAL(10, 2)
);
INSERT INTO Table3 VALUES (1, 1, 'Product A', 10.99);
INSERT INTO Table3 VALUES (2, 1, 'Product B', 20.50);
INSERT INTO Table3 VALUES (3, 3, 'Product C', 5.00);
INSERT INTO Table3 VALUES (4, 4, 'Product D', 15.75);
INSERT INTO Table3 VALUES (5, 5, 'Product E', 8.99);
-- ...
CREATE TABLE Table4 (
ID INT PRIMARY KEY,
Table2ID INT FOREIGN KEY REFERENCES Table2(ID),
Table3ID INT FOREIGN KEY REFERENCES Table3(ID)
);
INSERT INTO Table4 VALUES (1, 1, 1);
INSERT INTO Table4 VALUES (2, 2, 2);
INSERT INTO Table4 VALUES (3, 4, 3);
INSERT INTO Table4 VALUES (4, 5, 4);
INSERT INTO Table4 VALUES (5, 3, 5);
-- ...
CREATE TABLE Table5 (
ID INT PRIMARY KEY,
Date DATE,
Value INT
);
INSERT INTO Table5 VALUES (1, '2021-01-01', 10);
INSERT INTO Table5 VALUES (2, '2021-01-02', 15);
INSERT INTO Table5 VALUES (3, '2021-01-03', 20);
INSERT INTO Table5 VALUES (4, '2021-01-04', 25);
INSERT INTO Table5 VALUES (5, '2021-01-05', 30);
-- ...
2. 使用模式匹配查询
SELECT * FROM Table1 WHERE Name LIKE 'J%';
3. 使用 TOP 查询
SELECT TOP 3 * FROM Table2;
4. 使用 IN 查询
SELECT * FROM Table3 WHERE Table1ID IN (1, 3, 5);
5. 降序查询
SELECT * FROM Table4 ORDER BY ID DESC;
6. 使用 COUNT 集合函数查询
SELECT COUNT(*) FROM Table5 WHERE Value > 20;
7. 分组统计查询
SELECT Gender, AVG(Age) AS AvgAge FROM Table1 GROUP BY Gender;
8. 使用连接条件的多表查询
SELECT Table1.Name, Table2.Category, Table3.ProductName
FROM Table1
JOIN Table3 ON Table1.ID = Table3.Table1ID
JOIN Table4 ON Table3.ID = Table4.Table3ID
JOIN Table2 ON Table4.Table2ID = Table2.ID;
9. 比较运算符的子查询
SELECT * FROM Table1 WHERE Age > (SELECT AVG(Age) FROM Table1);
10. 使用 IN 的子查询
SELECT * FROM Table2 WHERE ID IN (SELECT Table2ID FROM Table4);
11. 创建视图并使用
CREATE VIEW View1 AS
SELECT Name, Age FROM Table1 WHERE Gender = 'Male';
SELECT * FROM View1;
12. 创建索引
CREATE INDEX Index1 ON Table5 (Date);
13. 创建唯一性约束、检查约束、默认值约束、外键约束
CREATE TABLE Table6 (
ID INT PRIMARY KEY,
Name VARCHAR(50) UNIQUE,
Age INT CHECK (Age > 0),
Gender VARCHAR(10) DEFAULT 'Unknown',
Table1ID INT FOREIGN KEY REFERENCES Table1(ID)
);
14. 创建一个存储过程并使用
CREATE PROCEDURE Proc1
AS
BEGIN
SELECT * FROM Table1 WHERE Age > 30;
END;
EXEC Proc1;
15. 在某个表上创建一个插入和更新的 DML 触发器
CREATE TRIGGER Trigger1
ON Table1
AFTER INSERT, UPDATE
AS
BEGIN
PRINT 'A row has been inserted or updated in Table1.';
END;
阅读全文