要求用sql sever语句实现 创建一个数据库,至少里面有5个表,没个表至少有十条数据,至少有俩个表具有外键 使用模式匹配查询,使用top查询,in查询,降序查询,使用count集合函数查询,分组统计查询,使用连接条件的多表查询,比较运算符的子查询,使用in的子查询 创建视图并使用 创建索引 创建唯一性约束,检查约束,默认值约束,外键约束 创建一个存储过程并使用, 在某个表上创建一个插入和更新的dml触发器
时间: 2024-03-14 09:45:16 浏览: 199
好的,以下是 SQL Server 实现以上要求的代码:
创建数据库:
```sql
CREATE DATABASE MyDatabase;
USE MyDatabase;
```
创建5个表:
```sql
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
ContactName VARCHAR(50),
Country VARCHAR(50)
);
INSERT INTO Customers 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 VALUES
(1, 1, '2021-02-01', 100),
(2, 2, '2021-02-02', 200),
(3, 1, '2021-02-03', 150),
(4, 3, '2021-02-04', 300),
(5, 2, '2021-02-05', 250),
(6, 4, '2021-02-06', 400),
(7, 1, '2021-02-07', 120),
(8, 5, '2021-02-08', 180),
(9, 6, '2021-02-09', 220),
(10, 7, '2021-02-10', 280);
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50),
SupplierID INT,
CategoryID INT,
Price DECIMAL(10,2),
FOREIGN KEY (SupplierID) REFERENCES Suppliers(SupplierID),
FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)
);
INSERT INTO Products VALUES
(1, 'Chai', 1, 1, 18.00),
(2, 'Chang', 1, 1, 19.00),
(3, 'Aniseed Syrup', 1, 2, 10.00),
(4, 'Chef Anton\'s Cajun Seasoning', 2, 2, 22.00),
(5, 'Chef Anton\'s Gumbo Mix', 2, 2, 21.35),
(6, 'Grandma\'s Boysenberry Spread', 3, 2, 25.00),
(7, 'Uncle Bob\'s Organic Dried Pears', 3, 7, 30.00),
(8, 'Northwoods Cranberry Sauce', 3, 2, 40.00),
(9, 'Mishi Kobe Niku', 4, 6, 97.00),
(10, 'Ikura', 4, 8, 31.00);
CREATE TABLE Suppliers (
SupplierID INT PRIMARY KEY,
SupplierName VARCHAR(50),
ContactName VARCHAR(50),
Country VARCHAR(50)
);
INSERT INTO Suppliers VALUES
(1, 'Exotic Liquids', 'Charlotte Cooper', 'UK'),
(2, 'New Orleans Cajun Delights', 'Shelley Burke', 'USA'),
(3, 'Grandma Kelly\'s Homestead', 'Regina Murphy', 'USA'),
(4, 'Tokyo Traders', 'Yoshi Nagase', 'Japan'),
(5, 'Cooperativa de Quesos \'Las Cabras\'', 'Antonio del Valle Saavedra', 'Spain'),
(6, 'Mayumi\'s', 'Mayumi Ohno', 'Japan'),
(7, 'Pavlova, Ltd.', 'Ian Devling', 'Australia'),
(8, 'Specialty Biscuits, Ltd.', 'Peter Wilson', 'UK'),
(9, 'PB Knäckebröd AB', 'Lars Peterson', 'Sweden'),
(10, 'Refrescos Americanas LTDA', 'Carlos Diaz', 'Brazil');
CREATE TABLE Categories (
CategoryID INT PRIMARY KEY,
CategoryName VARCHAR(50)
);
INSERT INTO Categories VALUES
(1, 'Beverages'),
(2, 'Condiments'),
(3, 'Confections'),
(4, 'Dairy Products'),
(5, 'Grains/Cereals'),
(6, 'Meat/Poultry'),
(7, 'Produce'),
(8, 'Seafood');
```
模式匹配查询:
```sql
SELECT * FROM Customers WHERE CustomerName LIKE '%a%';
```
top查询:
```sql
SELECT TOP 3 * FROM Orders ORDER BY TotalAmount DESC;
```
in查询:
```sql
SELECT * FROM Customers WHERE Country IN ('UK', 'USA');
```
降序查询:
```sql
SELECT * FROM Products ORDER BY Price DESC;
```
count集合函数查询:
```sql
SELECT COUNT(*) FROM Customers;
```
分组统计查询:
```sql
SELECT Country, COUNT(*) FROM Customers GROUP BY Country;
```
连接条件的多表查询:
```sql
SELECT Customers.CustomerName, Orders.OrderDate, 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;
```
比较运算符的子查询:
```sql
SELECT * FROM Customers WHERE CustomerID IN (SELECT CustomerID FROM Orders WHERE TotalAmount > 200);
```
in的子查询:
```sql
SELECT * FROM Products WHERE SupplierID IN (SELECT SupplierID FROM Suppliers WHERE Country = 'USA');
```
创建视图并使用:
```sql
CREATE VIEW CustomerOrders AS
SELECT Customers.CustomerName, Orders.OrderDate, Orders.TotalAmount
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
SELECT * FROM CustomerOrders;
```
创建索引:
```sql
CREATE INDEX IX_Customers_Country ON Customers (Country);
```
创建唯一性约束:
```sql
ALTER TABLE Customers ADD CONSTRAINT UQ_CustomerName UNIQUE (CustomerName);
```
检查约束:
```sql
ALTER TABLE Products ADD CONSTRAINT CK_Price CHECK (Price > 0);
```
默认值约束:
```sql
ALTER TABLE Customers ADD CONSTRAINT DF_ContactName DEFAULT 'Unknown' FOR ContactName;
```
外键约束:
```sql
ALTER TABLE Orders ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
```
创建一个存储过程并使用:
```sql
CREATE PROCEDURE GetOrdersByCountry
@Country VARCHAR(50)
AS
BEGIN
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, Orders.TotalAmount
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
WHERE Customers.Country = @Country;
END;
EXEC GetOrdersByCountry 'USA';
```
在某个表上创建一个插入和更新的dml触发器:
```sql
CREATE TRIGGER Customers_AfterInsertUpdate
ON Customers
AFTER INSERT, UPDATE
AS
BEGIN
PRINT 'A record has been inserted or updated in the Customers table.';
END;
```
阅读全文