2
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,
)

I have been trying to apply this code but am constantly getting the error msg.........

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.
10
  • Delete the records from employee table.
    – bgs
    Mar 24, 2014 at 8:49
  • @BGS there are no records in the employee table, i m creating the table. Mar 24, 2014 at 8:50
  • Well - it seems you have multiple cascade paths as per the error description - you properly have between country and state or similar Mar 24, 2014 at 8:52
  • Is there any FK relation between country and state tables/ Mar 24, 2014 at 8:53
  • Could u check State table values whether sid is primary key or not..?
    – bgs
    Mar 24, 2014 at 8:53

1 Answer 1

3

You can not use on update cascade when you have cycle references in you relations in database structure:

enter image description here

Only NO ACTION allowed (see you error Specify ON DELETE NO ACTION or ON UPDATE NO ACTION)

Is State from country has a different meaning then state in employee ?

Correct solution:

enter image description here

and table [Employee] must have one foreign key on multiple columns - FK(CID, SID)

8
  • In that case, i must refer to only country(cid) via my employee table? or else how can i approach the problem?? Mar 24, 2014 at 9:25
  • You must refer to only state table (see schema above) Mar 24, 2014 at 9:37
  • The illustration is beautiful and explanatory but can you actually put the schema into code so that i can understand how the above can be accomplished. The concept of 1FK for 2 fields is not what i know of. So plz explain. Mar 24, 2014 at 9:45
  • First table States must have primary key on columns (Cid, Sid) and then you must declare your foreign key constraint as CONSTRAINT [FK_Employees_States] FOREIGN KEY([Cid], [Sid]) REFERENCES [dbo].[State] ([Cid], [Sid]) Mar 24, 2014 at 11:10
  • Thanks for taking out time to answer me but i still have another table i.e. "country" having fields(cid(PK),cnames) which needs to be connected to "states" as well as "employees" tables. Mar 24, 2014 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.