0
以下查詢已成功完成,但是當我將空值輸入到訂單標識和項目標識的不同值(列中不存在的那些值)時,訂單標識仍爲越來越多。如何保留相同的訂單ID?外鍵的使用方式有問題嗎?兩列主鍵,自動遞增和外鍵
CREATE TABLE orders(
orderid int not null auto_increment,
itemid int not null,
quantity int not null,
tot_price int not null,
cid int not null,
code varchar(10),
order_time time,
order_date date ,
constraint order_pk primary key (orderid, itemid),
foreign key (itemid) references items(itemid),
foreign key (cid) references customer(cid),
foreign key (code) references coupon(code)
);
您需要更好地標準化。你應該有第三個包含你的訂單項目的表格,否則你會在每一行重複很多不必要的值。 –
你正在使用哪種引擎類型? –
作爲一個附註,在不同的字段中有order_time和order_date是奇數。單個[DATETIME字段](https://dev.mysql.com/doc/refman/5.6/en/datetime.html)如何?這將使得範圍計算變得更簡單。索引會使事情變得更快。 – Schwern