Solved

Cannot truncate table because it is being referenced by a FOREIGN KEY constraint

Posted on 2013-06-01
7
812 Views
Last Modified: 2013-06-08
Hello there,

How can I truncate a table which has foreign key constraints. I am in development and I need to truncate the tables.is there some easy way to this.

cheers
Zolf
0
Comment
Question by:zolf
7 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 39213883
Use Delete FROM... instead. To use Truncate the FK has to be dropped. DELETE will allow a delete providing the records being deleted do not cause any RI issue.


Kelvin
0
 

Author Comment

by:zolf
ID: 39213889
but the issue is that that delete will not start the pk from 1. I want the pk to be reset too which truncate does it for you. by the way what do you mean "the records being deleted do not cause any RI issue."
0
 
LVL 22

Assisted Solution

by:Kelvin Sparks
Kelvin Sparks earned 250 total points
ID: 39213892
True,

But you could after the delete reset the identity seed.

DBCC CHECKIDENT (tablename, RESEED, 0)

would allow 1 to be the value of the identity seed for the next record in table 'tablename'

Kelvin
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 39213894
If you want to persist with truncate, then you have to drop each FK, perform the truncate, then recreate the FK


Kelvin
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 39214173
Note that the process to drop/recreate of constraint can entirely be scripted as well as the truncate itself.  You can generate entirely the drop/recreate of constraints.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 250 total points
ID: 39214544
DBCC CHECKIDENT (tablename, RESEED, 0)
Just be aware that in order to use this command you have to "be a member of the sysadmin fixed server role, the db_owner fixed database role, or the db_ddladmin fixed database role."
That is not always an option.
0
 

Author Closing Comment

by:zolf
ID: 39232551
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now