Solved

truncate tables with foreign key reference

Posted on 2008-10-17
9
1,261 Views
Last Modified: 2012-05-05
I have three tables in sql server database. Table1 as:
 CREATE TABLE [IA_report] (
      [report_id] [int] IDENTITY (1, 1) NOT NULL ,
      [report_yr] [smallint] NOT NULL ,
      [report_nbr] [smallint] NOT NULL ,
      [finalized_dt] [datetime] NOT NULL ,
      [committee_dt] [datetime] NOT NULL ,
      [is_confidential_ind] [bit] NOT NULL ,
      [type] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [keyword] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [title] [varchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      CONSTRAINT [pk_IA_report] PRIMARY KEY  NONCLUSTERED
      (
            [report_id]
      )  ON [PRIMARY] ,
      CONSTRAINT [fk1_IA_report] FOREIGN KEY
      (
            [type]
      ) REFERENCES [IA_type_ref] (
            [type]
      ),
      CONSTRAINT [fk2_IA_report] FOREIGN KEY
      (
            [keyword]
      ) REFERENCES [IA_keyword_ref] (
            [keyword]
      )
) ON [PRIMARY]


Table 2 as:

CREATE TABLE [IA_recommendation] (
      [recommendation_id] [int] IDENTITY (1, 1) NOT NULL ,
      [report_id] [int] NOT NULL ,
      [recommendation_nbr] [smallint] NOT NULL ,
      [completion_dt] [datetime] NULL ,
      [status] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [risk] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [division] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [recommendation] [varchar] (2000) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      CONSTRAINT [pk_IA_recommendation] PRIMARY KEY  NONCLUSTERED
      (
            [recommendation_id]
      )  ON [PRIMARY] ,
      CONSTRAINT [fk1_IA_recommendation] FOREIGN KEY
      (
            [report_id]
      ) REFERENCES [IA_report] (
            [report_id]
      ),
      CONSTRAINT [fk2_IA_recommendation] FOREIGN KEY
      (
            [status]
      ) REFERENCES [IA_status_ref] (
            [status]
      ),
      CONSTRAINT [fk3_IA_recommendation] FOREIGN KEY
      (
            [risk]
      ) REFERENCES [IA_risk_ref] (
            [risk]
      ),
      CONSTRAINT [fk4_IA_recommendation] FOREIGN KEY
      (
            [division]
      ) REFERENCES [IA_division_ref] (
            [division]
      )
) ON [PRIMARY]

How do I truncate (not delete)  these tables?
0
Comment
Question by:nup
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 65 total points
ID: 22742986
drop the foriegn keys first, then truncate them.  aftewards, add the FKeys back.
0
 

Author Comment

by:nup
ID: 22743058
No permission to drop the foreign keys or any constraints. I also do not have permisson to run DBCC CHECKIDENT. Any other way to truncate so that I can reseed to 0?
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 65 total points
ID: 22743138
...which means you also likely do not have permissions to truncate.  :)


The other way would be you using the DELETE command and then reseeding.
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 40 total points
ID: 22743181
no points please: just reaffirming that what chapmandew said is correct. http:#22743138
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:nup
ID: 22743192
Yes, but delete will not reset the index. So, how do I reseed?
Do I need to have permission to reseed?
0
 
LVL 1

Assisted Solution

by:EzEApostle
EzEApostle earned 20 total points
ID: 22743194
The Delete Statement will work in exactly the same way as the Truncate, if you cannot drop constraints you will not be able to truncate the data. You will need to work out how the data in the tables are constrainted e.g. table 3 depends on the data in table 2, if you find the start of the link delete the data which doesn't have the constraint.

Delete statement will be the easiest thing to do :)
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 65 total points
ID: 22743314
you can't reseed w/o dbcc checkident
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 40 total points
ID: 22743356
ezeapostle:
the delete and the truncate will not work exactly the same.

Some of the major differences are:

* Delete logs each row deletion whereas the truncate logs just the truncate (truncate much faster).
* Delete does not reseed the identity value if one exists whereas delete does not without explicitly doing a dbcc checkident and issuing the reseed option (as chap points out).
* delete can be executed against a table with foreign keys as long as the data you are attempting to delete does not violate any of those foreign keys whereas the truncate CANNOT be issued against a table with foreign keys.  They must be dropped and readded (again, as chap points out).

But it doesn't matter because if they don't have permissions to drop the constraints, deleting is the only thing they can do, not just the easiest.

0
 
LVL 5

Expert Comment

by:nicolasdiogo
ID: 22884949
have you tried this script from here
http://sql-server-performance.com/Community/forums/p/20593/114949.aspx

it seems to recursevly truncate your tables

Nicolas
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now