MySQL : ERROR 1215 (HY000): Cannot add foreign key constraint

后端 未结 16 1957
误落风尘
误落风尘 2020-11-30 01:37

I have read Database system concepts, 6th edition, Silberschatz. I\'m going to implement the university database system shown in chapter 2 on OS X

16条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 02:05

    foreign key (dept_name) references department
    

    This syntax is not valid for MySQL. It should instead be:

    foreign key (dept_name) references department(dept_name)
    

    MySQL requires dept_name to be used twice. Once to define the foreign column, and once to define the primary column.

    13.1.17.2. Using FOREIGN KEY Constraints

    ... [the] essential syntax for a foreign key constraint definition in a CREATE TABLE or ALTER TABLE statement looks like this:

    [CONSTRAINT [symbol]] FOREIGN KEY
        [index_name] (index_col_name, ...)
        REFERENCES tbl_name (index_col_name, ...)
        [ON DELETE reference_option]
        [ON UPDATE reference_option]
    
    reference_option:
        RESTRICT | CASCADE | SET NULL | NO ACTION
    

提交回复
热议问题