Solved

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

Posted on 2013-06-01
7
819 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to setup several different housekeeping processes for a 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…

726 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