Mysql create table with multiple foreign key on delete set null

后端 未结 4 1791
粉色の甜心
粉色の甜心 2021-01-12 05:59

I am trying to create a database with multiple foreign keys with delete/ update constraints, but I got a error code 1005 with following sql scripts:

CREATE T         


        
4条回答
  •  北海茫月
    2021-01-12 06:24

    Your foreign key rule is ON DELETE SET NULL but your column definition is NOT NULL.

    Either change your column definition and remove the NOT NULL part or overthink your foreign key rule. That works:

    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 demo

提交回复
热议问题