帮我用sql sever语句实现 创建一个数据库,至少里面有5个表,没个表至少有十条数据,至少有两个表具有外键 ;使用模式匹配查询,使用top查询,in查询,降序查询,使用count集合函数查询,分组统计查询,使用连接条件的多表查询,比较运算符的子查询,使用in的子查询 创建视图并使用 ;创建索引; 创建唯一性约束,检查约束,默认值约束,外键约束; 创建一个存储过程并使用 ;在某个表上创建一个插入和更新的dml触发器
时间: 2024-03-14 18:49:42 浏览: 144
好的,以下是实现要求的 SQL Server 语句:
创建一个数据库:
```
CREATE DATABASE MyDatabase;
```
创建五个表并插入数据:
```
USE MyDatabase;
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
ContactName VARCHAR(50),
Country VARCHAR(50)
);
INSERT INTO Customers (CustomerID, CustomerName, ContactName, Country)
VALUES (1, 'Alfreds Futterkiste', 'Maria Anders', 'Germany'),
(2, 'Ana Trujillo Emparedados', 'Ana Trujillo', 'Mexico'),
(3, 'Antonio Moreno Taquería', 'Antonio Moreno', 'Mexico'),
(4, 'Around the Horn', 'Thomas Hardy', 'UK'),
(5, 'Berglunds snabbköp', 'Christina Berglund', 'Sweden'),
(6, 'Blauer See Delikatessen', 'Hanna Moos', 'Germany'),
(7, 'Blondel père et fils', 'Frédérique Citeaux', 'France'),
(8, 'Bólido Comidas preparadas', 'Martín Sommer', 'Spain'),
(9, 'Bon app\'', 'Laurence Lebihans', 'France'),
(10, 'Bottom-Dollar Markets', 'Elizabeth Lincoln', 'Canada');
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
TotalAmount DECIMAL(10, 2),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount)
VALUES (1, 1, '2020-01-01', 100.00),
(2, 1, '2020-02-01', 200.00),
(3, 2, '2020-03-01', 150.00),
(4, 3, '2020-04-01', 75.00),
(5, 4, '2020-05-01', 300.00),
(6, 4, '2020-06-01', 250.00),
(7, 5, '2020-07-01', 175.00),
(8, 6, '2020-08-01', 125.00),
(9, 7, '2020-09-01', 50.00),
(10, 8, '2020-10-01', 225.00);
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50),
Category VARCHAR(50),
Price DECIMAL(10, 2)
);
INSERT INTO Products (ProductID, ProductName, Category, Price)
VALUES (1, 'Chai', 'Beverages', 10.00),
(2, 'Chang', 'Beverages', 20.00),
(3, 'Aniseed Syrup', 'Condiments', 30.00),
(4, 'Chef Anton\'s Cajun Seasoning', 'Condiments', 40.00),
(5, 'Chef Anton\'s Gumbo Mix', 'Condiments', 50.00),
(6, 'Grandma\'s Boysenberry Spread', 'Spread', 60.00),
(7, 'Uncle Bob\'s Organic Dried Pears', 'Dried Fruit', 70.00),
(8, 'Northwoods Cranberry Sauce', 'Spread', 80.00),
(9, 'Mishi Kobe Niku', 'Meat', 90.00),
(10, 'Ikura', 'Seafood', 100.00);
CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
Quantity INT,
Price DECIMAL(10, 2),
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
INSERT INTO OrderDetails (OrderID, ProductID, Quantity, Price)
VALUES (1, 2, 3, 60.00),
(1, 3, 5, 150.00),
(2, 1, 2, 20.00),
(3, 5, 1, 50.00),
(4, 3, 2, 60.00),
(4, 4, 3, 120.00),
(5, 9, 1, 90.00),
(6, 7, 2, 140.00),
(7, 6, 3, 180.00),
(8, 10, 2, 200.00);
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(50),
BirthDate DATE,
HireDate DATE,
Country VARCHAR(50)
);
INSERT INTO Employees (EmployeeID, EmployeeName, BirthDate, HireDate, Country)
VALUES (1, 'Nancy Davolio', '1968-12-08', '1992-05-01', 'USA'),
(2, 'Andrew Fuller', '1972-02-19', '1992-08-14', 'USA'),
(3, 'Janet Leverling', '1985-08-30', '1992-04-01', 'USA'),
(4, 'Margaret Peacock', '1973-09-19', '1993-05-03', 'USA'),
(5, 'Steven Buchanan', '1975-03-04', '1993-10-17', 'UK'),
(6, 'Michael Suyama', '1983-07-02', '1993-10-17', 'UK'),
(7, 'Robert King', '1970-05-29', '1994-01-02', 'UK'),
(8, 'Laura Callahan', '1985-01-09', '1994-03-05', 'UK'),
(9, 'Anne Dodsworth', '1971-01-27', '1994-11-15', 'Australia'),
(10, 'Sally Hansen', '1974-09-03', '1995-12-01', 'Australia');
```
使用模式匹配查询:
```
SELECT * FROM Customers WHERE CustomerName LIKE '%a%';
```
使用 TOP 查询:
```
SELECT TOP 5 * FROM Customers ORDER BY CustomerID;
```
使用 IN 查询:
```
SELECT * FROM Orders WHERE CustomerID IN (1, 2);
```
降序查询:
```
SELECT * FROM Products ORDER BY Price DESC;
```
使用 COUNT 聚合函数查询:
```
SELECT COUNT(*) FROM Customers;
```
分组统计查询:
```
SELECT Country, COUNT(*) FROM Customers GROUP BY Country;
```
使用连接条件的多表查询:
```
SELECT Customers.CustomerName, Orders.OrderDate, OrderDetails.Quantity, Products.ProductName
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 Customers WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders);
```
使用 IN 的子查询:
```
SELECT * FROM Orders WHERE CustomerID IN (SELECT CustomerID FROM Customers WHERE Country = 'Mexico');
```
创建视图并使用:
```
CREATE VIEW CustomerOrders AS
SELECT Customers.CustomerName, Orders.OrderDate, Orders.TotalAmount
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
SELECT * FROM CustomerOrders;
```
创建索引:
```
CREATE INDEX idx_CustomerName ON Customers(CustomerName);
```
创建唯一性约束:
```
ALTER TABLE Orders ADD CONSTRAINT uc_OrderID UNIQUE(OrderID);
```
检查约束:
```
ALTER TABLE Products ADD CONSTRAINT ck_Price CHECK(Price > 0);
```
默认值约束:
```
ALTER TABLE Employees ADD CONSTRAINT df_Country DEFAULT('USA') FOR Country;
```
外键约束:
```
ALTER TABLE OrderDetails ADD CONSTRAINT fk_OrderID FOREIGN KEY(OrderID) REFERENCES Orders(OrderID);
```
创建一个存储过程并使用:
```
CREATE PROCEDURE GetOrdersByCustomerID
@CustomerID INT
AS
BEGIN
SELECT * FROM Orders WHERE CustomerID = @CustomerID;
END;
EXEC GetOrdersByCustomerID 1;
```
在某个表上创建一个插入和更新的 DML 触发器:
```
CREATE TRIGGER tr_Customers ON Customers
INSTEAD OF INSERT, UPDATE
AS
BEGIN
IF EXISTS(SELECT * FROM inserted WHERE CustomerName LIKE '%test%')
BEGIN
RAISERROR('Customer name cannot contain "test"', 16, 1);
ROLLBACK TRANSACTION;
END
ELSE
BEGIN
IF EXISTS(SELECT * FROM inserted WHERE CustomerID < 0)
BEGIN
RAISERROR('CustomerID cannot be negative', 16, 1);
ROLLBACK TRANSACTION;
END
ELSE
BEGIN
UPDATE Customers
SET CustomerName = i.CustomerName, ContactName = i.ContactName, Country = i.Country
FROM Customers c
INNER JOIN inserted i ON c.CustomerID = i.CustomerID;
INSERT INTO Customers (CustomerID, CustomerName, ContactName, Country)
SELECT CustomerID, CustomerName, ContactName, Country
FROM inserted
WHERE NOT EXISTS(SELECT * FROM Customers WHERE CustomerID = inserted.CustomerID);
END
END
END;
```
阅读全文