Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all Visitors
I have problem with deleting the record from multi tables
I have 3 tables.Table Employee(empcode,empname,address)
Table userAccount(ucode,name,empcode) and table sale(scode,empcode,amount)

When i delete one record from table Employee,it will error.

Anybody know about this,Please help me?

Best Regards,
Posted
Comments
walterhevedeich 30-Mar-11 5:12am    
The question is unclear. What is the error you are getting?

Without further information, I'm guessing you're seeing a referential integrity error.

You're trying to delete an 'Employee' record and there could be records in the other tables that rely on 'Employee' and have Foreign Keys being enforced.

You can fix this in a couple of ways.

1) Delete all data from related tables first, before deleting from your Employee Table.

2) On the Index of the primary table (Employees), make sure it's set to 'Cascade Delete Related Records'


Personally, I prefer the first approach
 
Share this answer
 
Comments
Manfred Rudolf Bihy 30-Mar-11 5:27am    
Exactly! Good answer 5+
Аslam Iqbal 30-Mar-11 5:47am    
yeah its good. my 5 too
Most likely caused by a foreign key relationship. I guess that tables userAccount and sale both have a foreignkey relationship to table Employee. The entries in table userAccount and sales that have a reference to Employee via empcode would have to be deleted first.
Another option is to set CASCADE ON DELETE so that when an Employee gets deleted all entries in userAccount and Sale that have a reference to that employee will also get deleted.

This has to do with referential integrity.

Hope that helps you!
 
Share this answer
 
v2
Comments
Аslam Iqbal 30-Mar-11 5:47am    
my 5
As Dylan said you have two ways of doing it.

No offcence, but i personally don't suggest you to use the 1st method. As it will require you to write more queries and you need to execute more number of commands on the database reducing the performance. And the job is tedious too. But The method is rock solid and will not cause any trouble as you will be executing the delete command on all the related tables.

But If you are using database like MySql, SQL server or MS Access, there is another way of doing it which make life easier. ;)

i.e. Using Relation. Like the option ON DELETE CASCADE in MySQL, SQL Server etc.

ON DELETE CASCADE[^]

And Create Relation[^]option provided by MS Access might help you to tacle with the prob.
 
Share this answer
 

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