Dno和Dnumber的相同属性中的简单外键

时间:2013-11-18 17:50:34

标签: mysql

!mysql的代码] CLICK

我想知道为什么我不能添加外键约束。 Dno和Dnumber具有相同的属性,但结果有问题。无法添加外键约束。

创建表employee04 (Dno int not null默认值为1,);

创建表格department04 (Dnumber int not null);

alter table employee04添加约束EMPFK外键(Dno)引用department04(Dnumber);

1 个答案:

答案 0 :(得分:0)

两列都必须具有相同的默认值。员工中的Dno是默认值1,部门中的Dnumber默认为null。

ALTER TABLE employee04
ALTER Dno SET DEFAULT NULL
  

虽然MySQL服务器允许SET DEFAULT,但它被拒绝为   InnoDB无效。使用此CREATE TABLE和ALTER TABLE语句   InnoDB表不允许使用子句。

Doc

也许试试ON DELETE SET NULL