请你创建一个数据库,完成下面的要求并且用sq|语句实现1.至少包含5个表,每个表中至少有10条数据,至少2个表有外键.2.使用模式匹配查询,使用top查询,in查询,降序查询,使用count集合函数查询,分组统计查询,使用连接条件的多表查询,比较运算符的子查询,使用in的子查询.3.创建视图并使用4.创建索引5.创建唯一性约束,检查约束,默认值约束,外键约束.6.创建一个存储过程并使用7.在某个表中创建一个插入和更新的dml触发器
时间: 2024-03-14 09:47:53 浏览: 69
好的,下面是对您的问题的回答:
1. 创建数据库和表,并插入数据:
```sql
CREATE DATABASE mydatabase;
USE mydatabase;
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50),
customer_email VARCHAR(50),
customer_address VARCHAR(100)
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50),
product_price DECIMAL(10, 2)
);
CREATE TABLE order_items (
order_id INT,
product_id INT,
quantity INT,
PRIMARY KEY (order_id, product_id),
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
employee_email VARCHAR(50),
employee_department VARCHAR(50),
employee_salary DECIMAL(10, 2)
);
INSERT INTO customers VALUES (1, 'John Smith', 'john.smith@example.com', '123 Main St');
INSERT INTO customers VALUES (2, 'Jane Doe', 'jane.doe@example.com', '456 Oak Ave');
INSERT INTO customers VALUES (3, 'Bob Johnson', 'bob.johnson@example.com', '789 Elm St');
-- 插入更多数据
INSERT INTO orders VALUES (1, '2020-01-01', 1);
INSERT INTO orders VALUES (2, '2020-01-02', 2);
INSERT INTO orders VALUES (3, '2020-01-03', 3);
-- 插入更多数据
INSERT INTO products VALUES (1, 'Product 1', 19.99);
INSERT INTO products VALUES (2, 'Product 2', 9.99);
INSERT INTO products VALUES (3, 'Product 3', 14.99);
-- 插入更多数据
INSERT INTO order_items VALUES (1, 1, 2);
INSERT INTO order_items VALUES (1, 2, 1);
INSERT INTO order_items VALUES (2, 1, 1);
INSERT INTO order_items VALUES (3, 3, 3);
-- 插入更多数据
INSERT INTO employees VALUES (1, 'Tom Smith', 'tom.smith@example.com', 'Sales', 50000.00);
INSERT INTO employees VALUES (2, 'Sara Johnson', 'sara.johnson@example.com', 'Marketing', 60000.00);
INSERT INTO employees VALUES (3, 'Mike Brown', 'mike.brown@example.com', 'IT', 70000.00);
-- 插入更多数据
```
2. 使用不同查询语句进行查询:
```sql
-- 模式匹配查询
SELECT * FROM customers WHERE customer_name LIKE '%John%';
-- Top 查询
SELECT TOP 5 * FROM orders ORDER BY order_date DESC;
-- In 查询
SELECT * FROM products WHERE product_id IN (1, 2, 3);
-- 降序查询
SELECT * FROM employees ORDER BY employee_salary DESC;
-- Count 集合函数查询
SELECT COUNT(*) FROM customers;
-- 分组统计查询
SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id;
-- 连接条件的多表查询
SELECT customers.customer_name, orders.order_date, products.product_name, order_items.quantity
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id
INNER JOIN order_items ON orders.order_id = order_items.order_id
INNER JOIN products ON order_items.product_id = products.product_id;
-- 比较运算符的子查询
SELECT * FROM employees WHERE employee_salary > (SELECT AVG(employee_salary) FROM employees);
-- In 的子查询
SELECT * FROM customers WHERE customer_id IN (SELECT customer_id FROM orders WHERE order_date > '2020-01-01');
```
3. 创建视图并使用:
```sql
CREATE VIEW order_details AS
SELECT customers.customer_name, orders.order_date, products.product_name, order_items.quantity
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id
INNER JOIN order_items ON orders.order_id = order_items.order_id
INNER JOIN products ON order_items.product_id = products.product_id;
SELECT * FROM order_details;
```
4. 创建索引:
```sql
CREATE INDEX idx_customer_name ON customers(customer_name);
```
5. 创建约束:
```sql
-- 唯一性约束
ALTER TABLE customers ADD CONSTRAINT uc_customer_email UNIQUE (customer_email);
-- 检查约束
ALTER TABLE employees ADD CONSTRAINT ck_employee_salary CHECK (employee_salary > 0);
-- 默认值约束
ALTER TABLE orders ALTER COLUMN order_date SET DEFAULT GETDATE();
-- 外键约束
ALTER TABLE order_items ADD CONSTRAINT fk_order_items_order_id FOREIGN KEY (order_id) REFERENCES orders(order_id);
```
6. 创建存储过程并使用:
```sql
CREATE PROCEDURE GetOrderDetails @customerId INT
AS
BEGIN
SELECT customers.customer_name, orders.order_date, products.product_name, order_items.quantity
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id
INNER JOIN order_items ON orders.order_id = order_items.order_id
INNER JOIN products ON order_items.product_id = products.product_id
WHERE customers.customer_id = @customerId;
END;
EXEC GetOrderDetails 1;
```
7. 创建插入和更新的 DML 触发器:
```sql
CREATE TRIGGER tr_insert_order_items ON order_items
AFTER INSERT
AS
BEGIN
UPDATE orders
SET total_amount = total_amount + (SELECT product_price * quantity FROM inserted INNER JOIN products ON inserted.product_id = products.product_id)
WHERE orders.order_id = (SELECT order_id FROM inserted);
END;
CREATE TRIGGER tr_update_order_items ON order_items
AFTER UPDATE
AS
BEGIN
UPDATE orders
SET total_amount = total_amount - (SELECT product_price * deleted.quantity FROM deleted INNER JOIN products ON deleted.product_id = products.product_id)
+ (SELECT product_price * inserted.quantity FROM inserted INNER JOIN products ON inserted.product_id = products.product_id)
WHERE orders.order_id = (SELECT order_id FROM deleted);
END;
```
以上就是对您提出的问题的回答,希望能够帮助您。
阅读全文