怎么了?
mysql> create table price(
-> p_code char(1) not null,
-> p_description varchar(20),
-> p_rentfee decimal(2,2) not null,
-> p_dylatefee decimal(2,2));
Query OK, 0 rows affected (0.18 sec)
mysql> create table movie(
-> mv_no char(4) not null,
-> mv_name varchar(50) not null,
-> mv_year char(4) not null,
-> mv_cost decimal(2,2) not null,
-> mv_genre varchar(15) not null,
-> p_code char(1) not null,
-> foreign key (p_code) references price(p_code));
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql>
Run Code Online (Sandbox Code Playgroud)
price.p_code不是主要的关键price.尝试:
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));
Run Code Online (Sandbox Code Playgroud)
通常,外键必须引用主/唯一密钥,完整的主/唯一密钥,以及主/唯一密钥.
在一些RDBMS,例如SQL Server,您可以引用列,具有独特的索引(未键)(见我们能有一个外键这是不以任何其他表的主键?),但是这是不规范的行为.
| 归档时间: |
|
| 查看次数: |
21088 次 |
| 最近记录: |