使用 DDL 语句分别创建客户表、商家表,产品表、区域表和订单表,产品类型表,订单明细表(字段参考教材)。 使用 DML 语句分别新增一条创建客户表、商家表,产品表、区域表和订单表,产品类型表,订单明细表。
时间: 2024-01-21 20:02:06 浏览: 111
创建客户表:
```
CREATE TABLE Customer (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
address VARCHAR(100) NOT NULL,
phone VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL
);
```
创建商家表:
```
CREATE TABLE Merchant (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
address VARCHAR(100) NOT NULL,
phone VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL
);
```
创建产品表:
```
CREATE TABLE Product (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
description VARCHAR(200) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
stock INT NOT NULL,
merchant_id INT NOT NULL REFERENCES Merchant(id)
);
```
创建区域表:
```
CREATE TABLE Region (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
```
创建订单表:
```
CREATE TABLE Order (
id INT PRIMARY KEY,
customer_id INT NOT NULL REFERENCES Customer(id),
region_id INT NOT NULL REFERENCES Region(id),
order_date DATE NOT NULL,
total_price DECIMAL(10, 2) NOT NULL
);
```
创建产品类型表:
```
CREATE TABLE ProductType (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
```
创建订单明细表:
```
CREATE TABLE OrderDetail (
order_id INT NOT NULL REFERENCES Order(id),
product_id INT NOT NULL REFERENCES Product(id),
product_type_id INT NOT NULL REFERENCES ProductType(id),
quantity INT NOT NULL,
price DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
```
新增一条客户数据:
```
INSERT INTO Customer (id, name, address, phone, email)
VALUES (1, '张三', '北京市朝阳区', '13888888888', 'zhangsan@example.com');
```
新增一条商家数据:
```
INSERT INTO Merchant (id, name, address, phone, email)
VALUES (1, '苹果专卖店', '北京市海淀区', '13999999999', 'apple@example.com');
```
新增一条产品数据:
```
INSERT INTO Product (id, name, description, price, stock, merchant_id)
VALUES (1, 'iPhone 12', '苹果 iPhone 新品', 6999.00, 100, 1);
```
新增一条区域数据:
```
INSERT INTO Region (id, name)
VALUES (1, '北京市');
```
新增一条订单数据:
```
INSERT INTO Order (id, customer_id, region_id, order_date, total_price)
VALUES (1, 1, 1, '2021-01-01', 6999.00);
```
新增一条产品类型数据:
```
INSERT INTO ProductType (id, name)
VALUES (1, '手机');
```
新增一条订单明细数据:
```
INSERT INTO OrderDetail (order_id, product_id, product_type_id, quantity, price)
VALUES (1, 1, 1, 1, 6999.00);
```
阅读全文