为什么order_id这里报错了
时间: 2023-10-10 18:15:02 浏览: 79
很抱歉,我之前的回答中存在一个错误。在创建订单表时,order_id 列应该是一个自增的主键,因此不需要手动插入值。下面是已修正的代码:
```sql
-- 创建顾客表
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50),
customer_email VARCHAR(50)
);
-- 创建菜品表
CREATE TABLE dishes (
dish_id INT PRIMARY KEY,
dish_name VARCHAR(50),
dish_price DECIMAL(10, 2)
);
-- 创建订单表
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT,
dish_id INT,
quantity INT,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (dish_id) REFERENCES dishes(dish_id)
);
-- 插入顾客信息
INSERT INTO customers (customer_id, customer_name, customer_email)
VALUES
(1001, 'John Doe', 'john.doe@example.com'),
(1002, 'Jane Smith', 'jane.smith@example.com'),
(1003, 'Mike Johnson', 'mike.johnson@example.com'),
(1004, 'Emily Brown', 'emily.brown@example.com'),
(1005, 'David Lee', 'david.lee@example.com');
-- 插入菜品信息
INSERT INTO dishes (dish_id, dish_name, dish_price)
VALUES
(2001, 'Steak', 35.99),
(2002, 'Salmon', 28.50),
(2003, 'Pasta', 18.75),
(2004, 'Pizza', 12.99),
(2005, 'Burger', 15.50);
-- 插入订单信息
INSERT INTO orders (customer_id, dish_id, quantity)
VALUES
(1001, 2001, 2),
(1002, 2004, 1),
(1003, 2002, 3),
(1004, 2003, 2),
(1005, 2005, 1);
```
请注意,我在订单表的 `order_id` 列上添加了 `AUTO_INCREMENT` 属性,这样就可以自动生成唯一的订单ID,而无需手动插入值。再次对之前的错误表示抱歉,并希望这次能够帮助到您。如果还有其他问题,请随时提问。
阅读全文