views:

87

answers:

3

hi all i am getting a problem while i attenpting to delete a row from parent table, actuall i have created 2 table custmer and account.i make cutomer id(primary key) in customer and customer id (forigen key ) in account.after created it i have filled data inside both table. at this point while i am trying to delete a row from first table (customer ) then it give failure message is that it can't be deleted bcs it is refrenced as forigen key some thing like that............but while we delete row from account table then it's delete sucess fully. .......i want to function like that if i delete a row from parent table(customer) then its in child table that row which has same customer id (account table) is delete automatically............

+1  A: 

You need to set up the foreign key with on delete cascade to achieve this.

For SQL Server 2008 see the article Cascading Referential Integrity Constraints

Edit Just to add a somewhat redundant health warning you should be aware that adding on delete cascade will mean that when you delete the row from the parent table associated rows from the child table will be deleted. However as this is exactly the behaviour you state that you want I can't see that would be an issue.

Martin Smith
sql server 2008
Nishant
thank u sir it's working......
Nishant
I would advise caution on cascading deletes, especially if you are new to them. You can end up deleting more than you expected. This is something worth reviewing with people who understand the context of your application.
dwarFish
@downvoter - Care to give a reason? Cascading deletes exactly answer the `OP`'s requirements. You can't assume that all question askers are idiots!
Martin Smith
+3  A: 

watch out on the cascade deletes! a user will accidentally click on the application's little trash can icon and delete the customer, and then all the cascades will remove every trace of that customer, orders, invoices, payments, history, etc from your database. After the user call you to tell you about their little mistake, you'll have to restore a backup and try to pull the info back into the database.

I would look into "soft deletes" where you only change the customer's status from "active" to "inactive". the rows is not deleted, preserving all foreign key data. This allows reports to run on the data, because it still exists, as well as easy an "undo".

Soft deletes are not the end all only way to go, it is a business decision on how to handle this, purge the data or mark it inactive. That is only something you can decide, because I don't know your application or business logic. I just thought that I would offer it as an alternative.

KM
+1. For someone (apparently) being confronted with the concept of foreign key constraints for the first time, this is a *VERY* important point. To extend KM's last paragraph: Think carefully about whether you want to make a piece of data *invisible to the user interface* (mark inactive) or *eliminated from the entire system* (deleted)!
djacobson
A: 

sorry bhai ? nishant

Viren
this is not chating side ok ?
Nishant
what u want to say ?
Nishant