Click here to Skip to main content
15,441,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i'm having some difficulties in creating on delete cascade constraint.

i have 3 tables- lets say table1, table2 & table3
table1 has a primary key "key1"
table2 has a primary key "key2" & a foriegn key "key1" reference table1 with on delete cascade constraint.
now when i try to create a table3 with foreign keys- "key1" & "key2" referred to tables "table1" & "table2" respectively with on delete cascade constraint on both...

it gives this error-
Introducing FOREIGN KEY constraint 'FK__table3__key2__1D114BD1' on table 'table3' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.


this is how i'm doing it...
My code:
SQL
create table table1
(
key1 varchar(20) primary key
)
create table table2
(
key2 integer identity primary key,
key1 varchar(20) references table1 on delete cascade
)
create table table3
(
key1 varchar(20) references table1 on delete cascade,
key2 integer references table2 on delete cascade
)


please suggest something i need to apply on delete cascade constraint on all.
Posted
Updated 15-Jan-12 9:29am
v2

1 solution

hello friend use this syntax instead of yours it will work fine with your on-delete cascade criteria

SQL
create table table1
(
key1 varchar(20) primary key
)


create table table2
(
key2 integer identity primary key,
key1 varchar(20) references table1 on delete cascade
)


create table table3
(
key1 varchar(20) references table1,
key2 integer references table2 on delete cascade
)
 
Share this answer
 
Comments
Sachin gulati 16-Jan-12 1:41am    
yeah i know but i need to apply on delete cascade on all references...
Tejas Vaishnav 16-Jan-12 7:22am     CRLF
this is not possible, because you already done it with the table2 for key1, and if you trying to give it in table3 also then it will ambiguate the on-delete cascade so it will gives you that error.... if you use this syntax then it will work for all on delete cascade no need to apply it for table3 key1, because if you delete the key1 from table1 then it will correspondent delete the record from table2 and here you have also giving the on-delete cascade for key2 in table3 so it will also fire the delete query in table3..... means if the table1 contains record ('11') and table2 has also like (1,'11') and table3 contains record like (1,'11') then if you delete from table1 then it will fire on-delete cascade from table2 and it the record is deleted from table2 then it will automatically fire on-delete cascade for table3 so it will automatically become one chain to delete all record from all table... and even the record from table3 will be deleted if you not provide FK for key1 try this also it will work also fine...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900