所有要求都是用sql sever语句实现 创建一个数据库,至少里面有5个表,没个表至少有十条数据,至少有俩个表具有外键 使用模式匹配查询,使用top查询,in查询,降序查询,使用count集合函数查询,分组统计查询,使用连接条件的多表查询,比较运算符的子查询,使用in的子查询 创建视图并使用 ;创建索引; 创建唯一性约束,检查约束,默认值约束,外键约束; 创建一个存储过程并使用 ;在某个表上创建一个插入和更新的dml触发器
时间: 2024-03-14 11:50:05 浏览: 172
好的,以下是实现你要求的 SQL Server 代码。
创建数据库:
```
CREATE DATABASE MyDatabase
```
创建5个表,每个表至少有10条数据,其中至少有2个表具有外键:
```
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(50),
Email VARCHAR(50),
Phone VARCHAR(20)
)
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT FOREIGN KEY REFERENCES Customers(CustomerID),
OrderDate DATE,
TotalAmount DECIMAL(10,2)
)
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
Name VARCHAR(50),
Price DECIMAL(10,2),
Description VARCHAR(500)
)
CREATE TABLE OrderDetails (
OrderID INT FOREIGN KEY REFERENCES Orders(OrderID),
ProductID INT FOREIGN KEY REFERENCES Products(ProductID),
Quantity INT,
Price DECIMAL(10,2)
)
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
HireDate DATE,
Salary DECIMAL(10,2)
)
-- Insert data into the tables
INSERT INTO Customers VALUES (1, 'John', 'john@example.com', '555-1234')
INSERT INTO Customers VALUES (2, 'Jane', 'jane@example.com', '555-5678')
INSERT INTO Customers VALUES (3, 'Bob', 'bob@example.com', '555-9012')
INSERT INTO Orders VALUES (1, 1, '2021-01-01', 100.00)
INSERT INTO Orders VALUES (2, 2, '2021-01-05', 200.00)
INSERT INTO Orders VALUES (3, 3, '2021-01-10', 300.00)
INSERT INTO Products VALUES (1, 'Product A', 10.00, 'Description for Product A')
INSERT INTO Products VALUES (2, 'Product B', 20.00, 'Description for Product B')
INSERT INTO Products VALUES (3, 'Product C', 30.00, 'Description for Product C')
INSERT INTO OrderDetails VALUES (1, 1, 2, 20.00)
INSERT INTO OrderDetails VALUES (1, 2, 3, 30.00)
INSERT INTO OrderDetails VALUES (2, 3, 1, 30.00)
INSERT INTO OrderDetails VALUES (2, 1, 4, 40.00)
INSERT INTO Employees VALUES (1, 'Alice', '2020-01-01', 50000.00)
INSERT INTO Employees VALUES (2, 'Bob', '2020-01-02', 60000.00)
INSERT INTO Employees VALUES (3, 'Charlie', '2020-01-03', 70000.00)
```
使用模式匹配查询:
```
SELECT * FROM Customers WHERE Email LIKE '%example.com'
```
使用TOP查询:
```
SELECT TOP 2 * FROM Orders ORDER BY OrderDate DESC
```
使用IN查询:
```
SELECT * FROM Products WHERE ProductID IN (1, 2)
```
降序查询:
```
SELECT * FROM Employees ORDER BY Salary DESC
```
使用COUNT集合函数查询:
```
SELECT COUNT(*) FROM Customers
```
分组统计查询:
```
SELECT CustomerID, COUNT(*) FROM Orders GROUP BY CustomerID
```
使用连接条件的多表查询:
```
SELECT Customers.Name, Orders.OrderDate, Products.Name, OrderDetails.Quantity
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID
```
比较运算符的子查询:
```
SELECT * FROM Employees WHERE Salary > (SELECT AVG(Salary) FROM Employees)
```
使用IN的子查询:
```
SELECT * FROM Customers WHERE CustomerID IN (SELECT CustomerID FROM Orders)
```
创建视图并使用:
```
CREATE VIEW CustomerOrders AS
SELECT Customers.Name, Orders.OrderDate, Orders.TotalAmount
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
SELECT * FROM CustomerOrders
```
创建索引:
```
CREATE INDEX idx_Products_Name ON Products(Name)
```
创建唯一性约束、检查约束、默认值约束、外键约束:
```
ALTER TABLE Customers ADD CONSTRAINT uc_Customers_Email UNIQUE (Email)
ALTER TABLE Employees ADD CONSTRAINT ck_Employees_Salary CHECK (Salary > 0)
ALTER TABLE Employees ADD CONSTRAINT df_Employees_HireDate DEFAULT GETDATE() FOR HireDate
ALTER TABLE Orders ADD CONSTRAINT fk_Orders_Customers FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
```
创建一个存储过程并使用:
```
CREATE PROCEDURE GetOrdersByCustomerID
@CustomerID INT
AS
BEGIN
SELECT * FROM Orders WHERE CustomerID = @CustomerID
END
EXEC GetOrdersByCustomerID @CustomerID = 1
```
在某个表上创建一个插入和更新的DML触发器:
```
CREATE TRIGGER tr_Customers_InsertUpdate
ON Customers
AFTER INSERT, UPDATE
AS
BEGIN
IF EXISTS (SELECT * FROM inserted WHERE Email LIKE '%example.com')
BEGIN
RAISERROR('Email domain example.com is not allowed.', 16, 1)
ROLLBACK TRANSACTION
END
END
```
阅读全文