MySQL error cannot add foreign key constraint

后端 未结 6 1644
既然无缘
既然无缘 2021-01-14 01:52

what is wrong?

mysql> create table price(
    -> p_code char(1) not null,
    -> p_description varchar(20),
    -> p_rentfee decimal(2,2) not nul         


        
6条回答
  •  遥遥无期
    2021-01-14 02:28

    price.p_code is not the primary key for price. Try:

    create table price(
    p_code char(1) not null PRIMARY KEY,
    p_description varchar(20),
    p_rentfee decimal(2,2) not null,
    p_dylatefee decimal(2,2));
    

    In general, foreign keys must reference a primary/unique key, a whole primary/unique key, and nothing but a primary/unique key.

    In some RDBMS, for example SQL Server, you can reference a column with a unique index (not key) (see can we have a foreign key which is not a primary key in any other table?), but this is non-standard behavior.

提交回复
热议问题