我正在尝试使用具有删除/更新约束的多个外键创建数据库,但是使用以下SQL脚本得到了错误代码1005:
CREATE TABLE Worker ( WorkerID smallint auto_increment, WorkerType varchar(45) NOT NULL, WorkerName varchar(45) NOT NULL, Position varchar(45) NOT NULL, TaxFileNumber int NOT NULL, Address varchar(100) , Phone varchar(20) , SupervisorID smallint , PRIMARY KEY (WorkerID), FOREIGN KEY (SupervisorID) REFERENCES Worker(WorkerID) ON DELETE SET NULL ON UPDATE CASCADE )Engine=InnoDB; CREATE TABLE Grape ( GrapeID smallint NOT NULL, GrapeType varchar(45) NOT NULL, JuiceConversionRatio int, StorageContainer ENUM('Stainless Steel Tank','Oak Barrel'), AgingRequirement int, PRIMARY KEY (GrapeID) )Engine=InnoDB; CREATE TABLE Vineyard ( VineyardID smallint auto_increment, VineyardName VARCHAR(45) NOT NULL, FarmerID smallint NOT NULL, GrapeID smallint NOT NULL, ComeFrom varchar(45) NOT NULL, HarvestedAmount int, RipenessPercent int, PRIMARY KEY (VineyardID), FOREIGN KEY (FarmerID) REFERENCES Worker(WorkerID) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (GrapeID) REFERENCES Grape(GrapeID) ON DELETE SET NULL ON UPDATE CASCADE )Engine=InnoDB;
错误代码表示无法创建Vineyard表,我只想知道使用Delete / Update控件创建多个外键的正确格式。
您的外键规则为,ON DELETE SET NULL但列定义为NOT NULL。
ON DELETE SET NULL
NOT NULL
更改列定义并删除NOT NULL零件或过分考虑外键规则。那个有效:
CREATE TABLE Vineyard ( VineyardID smallint auto_increment, VineyardName VARCHAR(45) NOT NULL, FarmerID smallint, GrapeID smallint, ComeFrom varchar(45) NOT NULL, HarvestedAmount int, RipenessPercent int, PRIMARY KEY (VineyardID), FOREIGN KEY (FarmerID) REFERENCES Worker(WorkerID) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (GrapeID) REFERENCES Grape(GrapeID) ON DELETE SET NULL ON UPDATE CASCADE )Engine=InnoDB;
SQLFiddle演示