所有要求都是用sql sever语句实现 创建一个数据库,至少里面有5个表,没个表至少有十条数据,至少有俩个表具有外键 使用模式匹配查询,使用top查询,in查询,降序查询,使用count集合函数查询,分组统计查询,使用连接条件的多表查询,比较运算符的子查询,使用in的子查询 创建视图并使用 ;创建索引; 创建唯一性约束,检查约束,默认值约束,外键约束; 创建一个存储过程并使用 ;在某个表上创建一个插入和更新的dml触发器
时间: 2024-03-15 13:41:52 浏览: 71
SQL_Sever数据库语句大全.zip
好的,我会根据您的要求来回答问题。以下是针对您的问题的答案:
1. 创建数据库并添加表和数据:
```
CREATE DATABASE MyDatabase;
USE MyDatabase;
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
ContactName VARCHAR(50),
Country VARCHAR(50)
);
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
ShipCity VARCHAR(50),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
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)
);
CREATE TABLE Suppliers (
SupplierID INT PRIMARY KEY,
SupplierName VARCHAR(50),
ContactName VARCHAR(50),
Country VARCHAR(50)
);
CREATE TABLE Categories (
CategoryID INT PRIMARY KEY,
CategoryName VARCHAR(50),
Description VARCHAR(500)
);
INSERT INTO Customers VALUES (1, 'Alfreds Futterkiste', 'Maria Anders', 'Germany');
INSERT INTO Customers VALUES (2, 'Ana Trujillo Emparedados', 'Ana Trujillo', 'Mexico');
INSERT INTO Customers VALUES (3, 'Antonio Moreno Taquería', 'Antonio Moreno', 'Mexico');
INSERT INTO Customers VALUES (4, 'Around the Horn', 'Thomas Hardy', 'UK');
INSERT INTO Customers VALUES (5, 'Berglunds snabbköp', 'Christina Berglund', 'Sweden');
INSERT INTO Customers VALUES (6, 'Blauer See Delikatessen', 'Hanna Moos', 'Germany');
INSERT INTO Customers VALUES (7, 'Blondel père et fils', 'Frédérique Citeaux', 'France');
INSERT INTO Customers VALUES (8, 'Bólido Comidas preparadas', 'Martín Sommer', 'Spain');
INSERT INTO Customers VALUES (9, 'Bon app', 'Laurence Lebihans', 'France');
INSERT INTO Customers VALUES (10, 'Bottom-Dollar Markets', 'Elizabeth Lincoln', 'Canada');
INSERT INTO Orders VALUES (1, 1, '2021-01-01', 'Berlin');
INSERT INTO Orders VALUES (2, 2, '2021-01-02', 'Mexico City');
INSERT INTO Orders VALUES (3, 2, '2021-01-03', 'Mexico City');
INSERT INTO Orders VALUES (4, 3, '2021-01-04', 'Mexico City');
INSERT INTO Orders VALUES (5, 3, '2021-01-05', 'Mexico City');
INSERT INTO Orders VALUES (6, 3, '2021-01-06', 'Mexico City');
INSERT INTO Orders VALUES (7, 4, '2021-01-07', 'London');
INSERT INTO Orders VALUES (8, 5, '2021-01-08', 'Stockholm');
INSERT INTO Orders VALUES (9, 6, '2021-01-09', 'Munich');
INSERT INTO Orders VALUES (10, 7, '2021-01-10', 'Paris');
INSERT INTO Products VALUES (1, 'Chai', 1, 1, 18.00);
INSERT INTO Products VALUES (2, 'Chang', 1, 1, 19.00);
INSERT INTO Products VALUES (3, 'Aniseed Syrup', 1, 2, 10.00);
INSERT INTO Products VALUES (4, 'Chef Anton''s Cajun Seasoning', 2, 2, 22.00);
INSERT INTO Products VALUES (5, 'Chef Anton''s Gumbo Mix', 2, 2, 21.35);
INSERT INTO Products VALUES (6, 'Grandma''s Boysenberry Spread', 3, 2, 25.00);
INSERT INTO Products VALUES (7, 'Uncle Bob''s Organic Dried Pears', 3, 7, 30.00);
INSERT INTO Products VALUES (8, 'Northwoods Cranberry Sauce', 3, 2, 40.00);
INSERT INTO Products VALUES (9, 'Mishi Kobe Niku', 4, 6, 97.00);
INSERT INTO Products VALUES (10, 'Ikura', 4, 8, 31.00);
INSERT INTO Suppliers VALUES (1, 'Exotic Liquids', 'Charlotte Cooper', 'UK');
INSERT INTO Suppliers VALUES (2, 'New Orleans Cajun Delights', 'Shelley Burke', 'USA');
INSERT INTO Suppliers VALUES (3, 'Grandma Kelly''s Homestead', 'Regina Murphy', 'USA');
INSERT INTO Suppliers VALUES (4, 'Tokyo Traders', 'Yoshi Nagase', 'Japan');
INSERT INTO Suppliers VALUES (5, 'Cooperativa de Quesos ''Las Cabras''', 'Antonio del Valle Saavedra', 'Spain');
INSERT INTO Categories VALUES (1, 'Beverages', 'Soft drinks, coffees, teas, beers, and ales');
INSERT INTO Categories VALUES (2, 'Condiments', 'Sweet and savory sauces, relishes, spreads, and seasonings');
INSERT INTO Categories VALUES (3, 'Confections', 'Desserts, candies, and sweet breads');
INSERT INTO Categories VALUES (4, 'Dairy Products', 'Cheeses');
INSERT INTO Categories VALUES (5, 'Grains/Cereals', 'Breads, crackers, pasta, and cereal');
INSERT INTO Categories VALUES (6, 'Meat/Poultry', 'Prepared meats');
INSERT INTO Categories VALUES (7, 'Produce', 'Dried fruit and bean curd');
INSERT INTO Categories VALUES (8, 'Seafood', 'Seaweed and fish');
```
2. 使用不同的查询:
使用模式匹配查询:
```
SELECT * FROM Customers WHERE CustomerName LIKE '%a%';
```
使用TOP查询:
```
SELECT TOP 5 * FROM Customers ORDER BY CustomerID;
```
使用IN查询:
```
SELECT * FROM Customers WHERE Country IN ('Germany', 'Mexico');
```
降序查询:
```
SELECT * FROM Customers ORDER BY CustomerName DESC;
```
使用COUNT集合函数查询:
```
SELECT COUNT(*) FROM Customers;
```
分组统计查询:
```
SELECT Country, COUNT(*) FROM Customers GROUP BY Country;
```
使用连接条件的多表查询:
```
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
```
比较运算符的子查询:
```
SELECT * FROM Customers WHERE CustomerID IN (SELECT CustomerID FROM Orders);
```
使用IN的子查询:
```
SELECT * FROM Customers WHERE Country IN (SELECT Country FROM Customers WHERE CustomerName = 'Alfreds Futterkiste');
```
3. 创建视图:
```
CREATE VIEW OrderDetails AS
SELECT Customers.CustomerName, Orders.OrderDate, Products.ProductName, Products.Price
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID)
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID;
```
4. 创建索引:
```
CREATE INDEX idx_CustomerName ON Customers (CustomerName);
```
5. 创建唯一性约束,检查约束,默认值约束,外键约束:
```
ALTER TABLE Customers
ADD CONSTRAINT uc_CustomerName UNIQUE (CustomerName);
ALTER TABLE Customers
ADD CONSTRAINT chk_Country CHECK (Country IN ('USA', 'UK', 'Mexico', 'Germany', 'France', 'Spain', 'Sweden'));
ALTER TABLE Customers
ADD CONSTRAINT df_ContactName DEFAULT 'N/A' FOR ContactName;
ALTER TABLE Orders
ADD CONSTRAINT fk_CustomerID FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
```
6. 创建存储过程:
```
CREATE PROCEDURE GetOrdersByCustomer
@CustomerID INT
AS
BEGIN
SELECT * FROM Orders WHERE CustomerID = @CustomerID;
END;
```
7. 创建触发器:
```
CREATE TRIGGER tr_UpdateProductPrice
ON Products
FOR INSERT, UPDATE
AS
BEGIN
UPDATE Products
SET Price = Price * 1.1
FROM inserted
WHERE Products.ProductID = inserted.ProductID;
END;
```
阅读全文