Solved

truncate tables with foreign key reference

Posted on 2008-10-17
9
1,267 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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
 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

777 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