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
 Transact-SQL (2008)
 Truncating table with foreign key

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-09-19 : 22:01:38
I am trying to clean out a table and normaly I would just do "truncate table" but this time I can't because of a FOREIGN KEY CONSTRAINT. With out droping all the relationships is ther a way to clean out the table? (No there is no other data in ANY of the tables taht relate to the one I am trying to clean out)

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-19 : 22:14:06
You will need to run a DELETE statement.

DELETE FROM Table1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-09-20 : 08:28:56
quote:
Originally posted by tkizer

You will need to run a DELETE statement.

DELETE FROM Table1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog


Haha, yea not sure why I did not think of that. Ok it was officialy a stupid question. thanks

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-20 : 08:40:43
"Ok it was officialy a stupid question"

maybe not

If there is a lot of data in a table then DELETE will log all the deletions, which can fill up your transaction log file and cause it to be extended. Best to avoid that happening ...

You can DELETE in a loop (say 10,000 rows per iteration), and backup the transaction log frequently so that it doesn't get extended ...

Or you could drop the Foreign Key constraint, Truncate the table and re-Create the Foreign Key. (This method is very skinny on the Transaction log.)

The last one is probably the best way, although it involves having to create a script to Drop / Re-Create the Fkey. If you will do this often it will be worth doing - Truncate will be much faster than Delete (if more than a few rows in the table)

Beware that whilst the FKey is dropped anyone adding data won't have if "checked" against the FKey ...

Plenty to think about really
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-09-20 : 15:00:43
I have over 100 foreign key constraints on the table so droping is not really a solution. Since it is only 3 rows right now to delete I will just use the delete command.

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-20 : 15:36:44
"Since it is only 3 rows right now to delete I will just use the delete command"

Fair enough

Its easy enough to generate a script to DROP/re-CREATE them though, if you need to do it in future when there are more rows in the table.
Go to Top of Page
   

- Advertisement -