gpt4 book ai didi

MYSQL 1215 无法添加外部约束

转载 作者:行者123 更新时间:2023-11-29 06:02:14 25 4
gpt4 key购买 nike

所以我不知道为什么我不能在表 ORDER_LINE 中添加外部约束,我确保所有类型都是正确的。请帮忙,它一直给我错误

Error Code: 1215. Cannot add foreign key constraint

CREATE TABLE IF NOT EXISTS Customer (
Customer_ID int NOT NULL AUTO_INCREMENT,
Customer_Name VARCHAR(50) NOT NULL,
Customer_Age INT UNIQUE,
Customer_Address VARCHAR(255),
Customer_City VARCHAR(255),
Customer_State VARCHAR(50),
Customer_Zip VARCHAR(20),
PRIMARY KEY(Customer_ID)
);

CREATE TABLE IF NOT EXISTS Sales_order (
Order_ID int AUTO_INCREMENT,
Order_date DATE,
Customer_ID int,
PRIMARY KEY(Order_ID),
FOREIGN KEY(Customer_ID) REFERENCES Customer(Customer_ID) ON DELETE
CASCADE
);

CREATE TABLE IF NOT EXISTS Products (
Product_ID int AUTO_INCREMENT,
Product_Description VARCHAR(255),
Product_Finish VARCHAR(50),
Standard_Price DECIMAL,
Product_Line_ID INT,
PRIMARY KEY(Product_ID)
);

CREATE TABLE IF NOT EXISTS ORDER_LINE (
Order_ID int,
Product_ID int,
Ordered_Quantity int,
PRIMARY KEY(Order_ID, Product_ID),
FOREIGN KEY(Order_ID) REFERENCES Sales_order(Order_ID) ON DELETE SET NULL,
FOREIGN KEY(Product_ID) REFERENCES Products(Product_ID)
);

最佳答案

这可能是因为 primary key columns are made NOT NULL ,因此您的 ON DELETE SET NULL 正在尝试设置无效值。

编辑:事实上,我刚刚测试过,这似乎是问题所在——如果我删除 SET NULL 或主键,它会创建表。

关于MYSQL 1215 无法添加外部约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44055309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com