Sunday, June 20, 2010

Create Constraints/Delete Foreign Key linked record

Suppose you want to delete the one record that is associate/linked to some other table with foreign key reference then we need to set/alter cascade command on the table then it will delete/update the record which is associated to the same table/column


For Reference:-

1. Create One Table Called tblFirst With Primary Key
2. Create another table called tblSecond with foreign key.make sure your column key column doesn't contain any identity
3. While creating the foreign key you need to set the cascade. This cascade word delete/update the all records that linked to the same row.


Create table tblFirst(Id Int Identity(1,1) Primary Key, Name varchar(50))


Create table tblSecond(Id Int Primary Key, Phone varchar(50), Foreign Key (ID) References tblFirst(Id) On Delete Cascade On Update Cascade)

INsert into tblFirst(Name) values('Murli')
INsert into tblFirst(Name) values('Anup')

INsert into tblSecond(Id,Phone) values(1,'9323399705')
INsert into tblSecond(Id,Phone) values(2,'9000000000')

Delete From tblFirst Where Id = 1

No comments:

Post a Comment