Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete data from table with FKs

Status
Not open for further replies.

tnayfeh

Programmer
Apr 21, 2004
39
CA
I have a scheduled package that is supposed to delete all data in 8 tables as below.

DELETE FROM PPES_DIM_USAGE
DELETE FROM PPES_DISPATCHER
DELETE FROM PPES_LEVEL_USAGE
DELETE FROM PPES_MEASURE_USAGE
DELETE FROM PPES_REQUEST
DELETE FROM PPES_SESSION
DELETE FROM PPES_USER_SOURCE
DELETE FROM PPES_ADMIN_SOURCE

However I get error message when I try to run this since some tables include FK and PK. "Delete statement conflicted with table REFERENCE constraint 'FK...."
Is there a way to run this anyways?

Thanks.
 
You will need to find out all the foreign keys for the tables in question. Delete from the tables in the opposite order that you would insert into them.

For example when a new customer places an order, you would insert into CUSTOMERS first, then into ORDERS, then into ORDER_DETAILS. Therefore, you should delete from ORDER_DETAILS, then ORDERS, then CUSTOMERS.

Execute each DELETE in a Query Analyzer window and see which table is failing. Execute the DELETE from the referencing table before the DELETE in question. Good luck!

--John [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
Or you can set up a cascading relationship so that if you delete from Customers all the orders are automatically deleted. Look up Cascade delete in Book online.

Questions about posting. See faq183-874
Click here to learn Ways to help with Tsunami Relief
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top