所有要求都是用sql sever语句实现 创建一个数据库,至少里面有5个表,没个表至少有十条数据,至少有俩个表具有外键 使用模式匹配查询,使用top查询,in查询,降序查询,使用count集合函数查询,分组统计查询,使用连接条件的多表查询,比较运算符的子查询,使用in的子查询 创建视图并使用 创建索引 创建唯一性约束,检查约束,默认值约束,外键约束 创建一个存储过程并使用 在某个表上创建一个插入和更新的dml触发器
时间: 2024-04-25 20:26:11 浏览: 111
好的,我明白了您的问题。下面是针对您的要求的 SQL Server 语句实现。
首先,创建一个名为 "MyDatabase" 的数据库:
```
CREATE DATABASE MyDatabase;
```
然后,创建五个表并插入数据:
```
USE MyDatabase;
-- 创建表1
CREATE TABLE Table1 (
ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT NOT NULL,
Gender VARCHAR(10) NOT NULL
);
-- 插入数据到表1
INSERT INTO Table1 (ID, Name, Age, Gender) VALUES
(1, 'John', 25, 'Male'),
(2, 'Mary', 30, 'Female'),
(3, 'Tom', 20, 'Male'),
(4, 'Alice', 35, 'Female'),
(5, 'Bob', 28, 'Male'),
(6, 'Sara', 32, 'Female'),
(7, 'David', 27, 'Male'),
(8, 'Julia', 29, 'Female'),
(9, 'Alex', 24, 'Male'),
(10, 'Lisa', 31, 'Female');
-- 创建表2
CREATE TABLE Table2 (
ID INT PRIMARY KEY,
Department VARCHAR(50) NOT NULL,
Manager VARCHAR(50) NOT NULL
);
-- 插入数据到表2
INSERT INTO Table2 (ID, Department, Manager) VALUES
(1, 'Sales', 'John'),
(2, 'Marketing', 'Mary'),
(3, 'IT', 'Tom'),
(4, 'HR', 'Alice'),
(5, 'Finance', 'Bob'),
(6, 'Operations', 'Sara'),
(7, 'Engineering', 'David'),
(8, 'Customer Service', 'Julia'),
(9, 'Research', 'Alex'),
(10, 'Product Development', 'Lisa');
-- 创建表3
CREATE TABLE Table3 (
ID INT PRIMARY KEY,
ProductName VARCHAR(50) NOT NULL,
Price FLOAT NOT NULL
);
-- 插入数据到表3
INSERT INTO Table3 (ID, ProductName, Price) VALUES
(1, 'iPhone', 999.99),
(2, 'Samsung Galaxy', 799.99),
(3, 'Google Pixel', 899.99),
(4, 'Huawei P30', 699.99),
(5, 'OnePlus 7T', 599.99),
(6, 'Xiaomi Mi 9', 499.99),
(7, 'LG G8', 699.99),
(8, 'Sony Xperia', 749.99),
(9, 'HTC U12', 649.99),
(10, 'Nokia 9', 599.99);
-- 创建表4
CREATE TABLE Table4 (
ID INT PRIMARY KEY,
OrderDate DATE NOT NULL,
ProductID INT NOT NULL,
Quantity INT NOT NULL,
FOREIGN KEY (ProductID) REFERENCES Table3(ID)
);
-- 插入数据到表4
INSERT INTO Table4 (ID, OrderDate, ProductID, Quantity) VALUES
(1, '2020-01-01', 1, 2),
(2, '2020-02-01', 2, 3),
(3, '2020-03-01', 3, 1),
(4, '2020-04-01', 4, 4),
(5, '2020-05-01', 5, 2),
(6, '2020-06-01', 6, 3),
(7, '2020-07-01', 7, 1),
(8, '2020-08-01', 8, 4),
(9, '2020-09-01', 9, 2),
(10, '2020-10-01', 10, 3);
-- 创建表5
CREATE TABLE Table5 (
ID INT PRIMARY KEY,
CustomerName VARCHAR(50) NOT NULL,
OrderID INT NOT NULL,
FOREIGN KEY (OrderID) REFERENCES Table4(ID)
);
-- 插入数据到表5
INSERT INTO Table5 (ID, CustomerName, OrderID) VALUES
(1, 'John Smith', 1),
(2, 'Mary Johnson', 2),
(3, 'Tom Lee', 3),
(4, 'Alice Wang', 4),
(5, 'Bob Chen', 5),
(6, 'Sara Kim', 6),
(7, 'David Lee', 7),
(8, 'Julia Brown', 8),
(9, 'Alex Wong', 9),
(10, 'Lisa Zhang', 10);
```
接下来,使用模式匹配查询:
```
-- 查询表1中所有名字以 'J' 开头的人
SELECT * FROM Table1 WHERE Name LIKE 'J%';
```
使用 TOP 查询:
```
-- 查询表3中价格最高的前三个产品
SELECT TOP 3 * FROM Table3 ORDER BY Price DESC;
```
使用 IN 查询:
```
-- 查询表1中年龄为 25 或 30 的人
SELECT * FROM Table1 WHERE Age IN (25, 30);
```
降序查询:
```
-- 查询表4中按订单数量降序排列的产品ID
SELECT ProductID, SUM(Quantity) AS TotalQuantity
FROM Table4
GROUP BY ProductID
ORDER BY TotalQuantity DESC;
```
使用 COUNT 集合函数查询:
```
-- 查询表5中每个订单的客户数量
SELECT OrderID, COUNT(*) AS CustomerCount
FROM Table5
GROUP BY OrderID;
```
分组统计查询:
```
-- 查询表1中每个性别的平均年龄
SELECT Gender, AVG(Age) AS AverageAge
FROM Table1
GROUP BY Gender;
```
使用连接条件的多表查询:
```
-- 查询表5中每个客户的姓名、订单日期和产品名称
SELECT t5.CustomerName, t4.OrderDate, t3.ProductName
FROM Table5 t5
JOIN Table4 t4 ON t5.OrderID = t4.ID
JOIN Table3 t3 ON t4.ProductID = t3.ID;
```
比较运算符的子查询:
```
-- 查询表1中年龄大于平均年龄的人
SELECT * FROM Table1
WHERE Age > (SELECT AVG(Age) FROM Table1);
```
使用 IN 的子查询:
```
-- 查询表4中购买过价格为 899.99 的产品的订单ID
SELECT DISTINCT OrderID FROM Table4
WHERE ProductID IN (SELECT ID FROM Table3 WHERE Price = 899.99);
```
创建视图并使用:
```
-- 创建视图,显示表5中每个客户的姓名和订单数量
CREATE VIEW CustomerOrderCount AS
SELECT CustomerName, COUNT(*) AS OrderCount
FROM Table5
GROUP BY CustomerName;
-- 查询视图
SELECT * FROM CustomerOrderCount;
```
创建索引:
```
-- 为表1的 Name 列创建索引
CREATE INDEX idx_Name ON Table1 (Name);
-- 查询带有索引的列
SELECT * FROM Table1 WHERE Name = 'John';
```
创建唯一性约束、检查约束、默认值约束、外键约束:
```
-- 创建表6
CREATE TABLE Table6 (
ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT NOT NULL,
Gender VARCHAR(10) NOT NULL,
Phone VARCHAR(20) UNIQUE,
JobTitle VARCHAR(50) CHECK (JobTitle IN ('Manager', 'Developer', 'Designer')),
HireDate DATE DEFAULT GETDATE(),
DepartmentID INT NOT NULL,
FOREIGN KEY (DepartmentID) REFERENCES Table2(ID)
);
-- 插入数据到表6
INSERT INTO Table6 (ID, Name, Age, Gender, Phone, JobTitle, DepartmentID) VALUES
(1, 'John', 25, 'Male', '123-456-7890', 'Manager', 1),
(2, 'Mary', 30, 'Female', '234-567-8901', 'Developer', 2),
(3, 'Tom', 20, 'Male', '345-678-9012', 'Designer', 3),
(4, 'Alice', 35, 'Female', '456-789-0123', 'Manager', 4),
(5, 'Bob', 28, 'Male', '567-890-1234', 'Developer', 5),
(6, 'Sara', 32, 'Female', '678-901-2345', 'Designer', 6),
(7, 'David', 27, 'Male', '789-012-3456', 'Manager', 7),
(8, 'Julia', 29, 'Female', '890-123-4567', 'Developer', 8),
(9, 'Alex', 24, 'Male', '901-234-5678', 'Designer', 9),
(10, 'Lisa', 31, 'Female', '012-345-6789', 'Manager', 10);
-- 创建外键约束
ALTER TABLE Table4 ADD CONSTRAINT FK_Table4_Table3 FOREIGN KEY (ProductID) REFERENCES Table3(ID);
```
创建一个存储过程并使用:
```
-- 创建存储过程,查询表1中年龄大于指定值的人
CREATE PROCEDURE GetPeopleByAge
@Age INT
AS
BEGIN
SELECT * FROM Table1 WHERE Age > @Age;
END;
-- 执行存储过程
EXEC GetPeopleByAge @Age = 30;
```
在某个表上创建一个插入和更新的 DML 触发器:
```
-- 创建触发器,在表1中插入数据时自动填充 ID 列
CREATE TRIGGER trg_InsertTable1 ON Table1
INSTEAD OF INSERT
AS
BEGIN
DECLARE @MaxID INT;
SELECT @MaxID = MAX(ID) FROM Table1;
SET @MaxID = ISNULL(@MaxID, 0) + 1;
INSERT INTO Table1 (ID, Name, Age, Gender)
SELECT @MaxID + ROW_NUMBER() OVER (ORDER BY Name), Name, Age, Gender
FROM inserted;
END;
-- 测试触发器
INSERT INTO Table1 (Name, Age, Gender) VALUES ('New Person', 25, 'Male');
SELECT * FROM Table1;
```
以上就是针对您的要求的 SQL Server 语句实现。希望对您有所帮助。
阅读全文