Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Problem in foreign key with on delete no action

Author  Topic 

mirza21
Starting Member

11 Posts

Posted - 2008-10-29 : 04:30:07
Hi friends i am facing problem with using foreign key with on delete no action.
-I created two tables one for department and another for employees
- created a foreign key refrences departments

when i try to delete a departement from departments table then it gives a error

Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "zz". The conflict occurred in database "study", table "dbo.tbdep", column 'depdcod'.

Plz check my code where i am wrong
----------------------------------------------------------------
create table tbdep (depdcod int primary key not null, depname varchar(50))
insert tbdep values(10,'sales')
insert tbdep values(20,'MARKETIN')
insert tbdep values(30,'acounts')


select * from tbdep


create table tbemp (ename varchar(50),depcod int)

insert tbemp values('kdjfkjd',10)
insert tbemp values('SD',20)
insert tbemp values('FD',20)
insert tbemp values('GGF',10)
insert tbemp values('FGFG',30)


alter table tbemp
add constraint zz foreign key(depcod) references tbdep(depdcod) on delete no action
delete from tbdep where depdcod=10

select * from tbdep
select * from tbemp
-------------------------------------------------------------------

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-29 : 05:15:07
thats because the id value that you're trying to delete is refered in other table. first delete or set the refered records field value to null before deleting department record.
Go to Top of Page
   

- Advertisement -