所有要求都是用sql sever语句实现创建一个数据库,至少里面有5个表,每个表至少有十条数据,至少有两个表具有外键,使用模式匹配查询,使用top查询,in查询,降序查询,使用count集合函数查询,分组统计查询,使用连接条件的多表查询,比较运算符的子查询,使用in的子查询创建视图并使用创建索引创建唯一性约束,检查约束,默认值约束,外键约束创建一个存储过程并使用在某个表上创建一个插入和更新的dml触发器
时间: 2023-12-12 22:03:14 浏览: 38
好的,您的问题是如何使用 SQL Server 实现创建数据库并进行基本操作。
首先,我们来创建一个名为 "MyDatabase" 的数据库:
```
CREATE DATABASE MyDatabase;
```
接下来,我们创建五个表,每个表都至少有十条数据,其中至少有两个表具有外键:
```
CREATE TABLE Users (
UserId INT PRIMARY KEY,
UserName VARCHAR(50),
Email VARCHAR(50)
);
CREATE TABLE Orders (
OrderId INT PRIMARY KEY,
UserId INT FOREIGN KEY REFERENCES Users(UserId),
OrderDate DATE,
TotalAmount DECIMAL(10,2)
);
CREATE TABLE Products (
ProductId INT PRIMARY KEY,
ProductName VARCHAR(50),
Price DECIMAL(10,2),
CategoryId INT FOREIGN KEY REFERENCES Categories(CategoryId)
);
CREATE TABLE Categories (
CategoryId INT PRIMARY KEY,
CategoryName VARCHAR(50)
);
CREATE TABLE OrderDetails (
OrderDetailId INT PRIMARY KEY,
OrderId INT FOREIGN KEY REFERENCES Orders(OrderId),
ProductId INT FOREIGN KEY REFERENCES Products(ProductId),
Quantity INT,
Price DECIMAL(10,2)
);
-- 插入数据
INSERT INTO Users VALUES (1, 'Alice', 'alice@gmail.com');
INSERT INTO Users VALUES (2, 'Bob', 'bob@yahoo.com');
INSERT INTO Users VALUES (3, 'Charlie', 'charlie@hotmail.com');
INSERT INTO Users VALUES (4, 'David', 'david@gmail.com');
INSERT INTO Users VALUES (5, 'Emily', 'emily@yahoo.com');
INSERT INTO Users VALUES (6, 'Frank', 'frank@hotmail.com');
INSERT INTO Orders VALUES (1, 1, '2021-01-01', 100.00);
INSERT INTO Orders VALUES (2, 2, '2021-01-05', 50.00);
INSERT INTO Orders VALUES (3, 3, '2021-01-10', 200.00);
INSERT INTO Orders VALUES (4, 1, '2021-01-15', 75.00);
INSERT INTO Orders VALUES (5, 4, '2021-01-20', 150.00);
INSERT INTO Orders VALUES (6, 5, '2021-01-25', 80.00);
INSERT INTO Orders VALUES (7, 6, '2021-01-30', 90.00);
INSERT INTO Products VALUES (1, 'Product A', 10.00, 1);
INSERT INTO Products VALUES (2, 'Product B', 20.00, 1);
INSERT INTO Products VALUES (3, 'Product C', 30.00, 2);
INSERT INTO Products VALUES (4, 'Product D', 40.00, 2);
INSERT INTO Products VALUES (5, 'Product E', 50.00, 3);
INSERT INTO Products VALUES (6, 'Product F', 60.00, 3);
INSERT INTO Categories VALUES (1, 'Category 1');
INSERT INTO Categories VALUES (2, 'Category 2');
INSERT INTO Categories VALUES (3, 'Category 3');
INSERT INTO OrderDetails VALUES (1, 1, 1, 2, 20.00);
INSERT INTO OrderDetails VALUES (2, 1, 2, 3, 60.00);
INSERT INTO OrderDetails VALUES (3, 2, 3, 1, 30.00);
INSERT INTO OrderDetails VALUES (4, 3, 4, 4, 160.00);
INSERT INTO OrderDetails VALUES (5, 4, 5, 2, 100.00);
INSERT INTO OrderDetails VALUES (6, 5, 6, 3, 180.00);
INSERT INTO OrderDetails VALUES (7, 6, 1, 1, 10.00);
INSERT INTO OrderDetails VALUES (8, 6, 4, 2, 80.00);
INSERT INTO OrderDetails VALUES (9, 7, 3, 3, 90.00);
INSERT INTO OrderDetails VALUES (10, 7, 5, 1, 50.00);
```
现在我们来演示一些查询操作:
使用模式匹配查询:
```
SELECT * FROM Users WHERE UserName LIKE 'A%';
```
使用 TOP 查询:
```
SELECT TOP 3 * FROM Orders ORDER BY OrderDate DESC;
```
使用 IN 查询:
```
SELECT * FROM Products WHERE CategoryId IN (1, 2);
```
降序查询:
```
SELECT * FROM Orders ORDER BY TotalAmount DESC;
```
使用 COUNT 集合函数查询:
```
SELECT COUNT(*) FROM Users;
```
分组统计查询:
```
SELECT CategoryName, SUM(Price) FROM Products
INNER JOIN Categories ON Products.CategoryId = Categories.CategoryId
GROUP BY CategoryName;
```
使用连接条件的多表查询:
```
SELECT UserName, OrderDate, TotalAmount FROM Users
INNER JOIN Orders ON Users.UserId = Orders.UserId
WHERE OrderDate BETWEEN '2021-01-01' AND '2021-01-15';
```
比较运算符的子查询:
```
SELECT * FROM Orders WHERE TotalAmount > (SELECT AVG(TotalAmount) FROM Orders);
```
使用 IN 的子查询:
```
SELECT * FROM Products WHERE CategoryId IN (SELECT CategoryId FROM Categories WHERE CategoryName = 'Category 1');
```
创建视图并使用:
```
CREATE VIEW OrderDetailsView AS
SELECT Orders.OrderId, Products.ProductName, OrderDetails.Quantity, OrderDetails.Price
FROM Orders
INNER JOIN OrderDetails ON Orders.OrderId = OrderDetails.OrderId
INNER JOIN Products ON OrderDetails.ProductId = Products.ProductId;
SELECT * FROM OrderDetailsView WHERE OrderId = 1;
```
创建索引:
```
CREATE INDEX UserIdIndex ON Users(UserId);
```
创建唯一性约束:
```
ALTER TABLE Users ADD CONSTRAINT UC_Email UNIQUE (Email);
```
检查约束:
```
ALTER TABLE Orders ADD CONSTRAINT CK_TotalAmount CHECK (TotalAmount >= 0);
```
默认值约束:
```
ALTER TABLE Users ADD CONSTRAINT DF_UserName DEFAULT 'Unknown' FOR UserName;
```
外键约束:
已经在创建表的时候添加了。
创建一个存储过程并使用:
```
CREATE PROCEDURE GetOrdersByUser
@UserId INT
AS
BEGIN
SELECT * FROM Orders WHERE UserId = @UserId;
END;
EXEC GetOrdersByUser 1;
```
在某个表上创建一个插入和更新的 DML 触发器:
```
CREATE TRIGGER InsertUserTrigger
ON Users
AFTER INSERT
AS
BEGIN
INSERT INTO AuditLog (Action, TableName, RecordId, Timestamp)
VALUES ('INSERT', 'Users', (SELECT UserId FROM inserted), GETDATE());
END;
CREATE TRIGGER UpdateUserTrigger
ON Users
AFTER UPDATE
AS
BEGIN
INSERT INTO AuditLog (Action, TableName, RecordId, Timestamp)
VALUES ('UPDATE', 'Users', (SELECT UserId FROM inserted), GETDATE());
END;
```
以上就是一些 SQL Server 常用的操作。
阅读全文