使用外键约束(Cascade)在create table上获取错误

时间:2014-03-24 08:47:52

标签: sql-server tsql

create table employee
(
  eid int primary key,
  ename varchar(50),
  cid int,
  sid int,
  constraint fk_hello1 foreign key(cid) references country(cid)on delete 
  cascade on update cascade,         
  constraint fk_hello2 foreign key(sid) references state(sid) on delete
  cascade on update cascade,
)

我一直在尝试应用此代码,但我不断收到错误消息.........

ERROR MESSAGE
Msg 1785, Level 16, State 0, Line 1
Introducing FOREIGN KEY constraint 'fk_hello2' on table 'employee' may 
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or 
ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

1 个答案:

答案 0 :(得分:2)

在数据库结构中的关系中有循环引用时,不能使用on update cascade

enter image description here

仅允许NO ACTION(请参阅错误Specify ON DELETE NO ACTION or ON UPDATE NO ACTION

来自国家的州是否具有与员工不同的含义?

正确的解决方案:

enter image description here

和表[Employee]必须在多列上有一个外键 - FK(CID,SID)