Solved

truncate tables with foreign key reference

Posted on 2008-10-17
9
1,272 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
[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
  • 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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Independent Software Vendors: 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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

738 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