?
Solved

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

Posted on 2013-06-01
7
Medium Priority
?
833 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 1000 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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 1000 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

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

719 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