Solved

truncate tables with foreign key reference

Posted on 2008-10-17
9
1,273 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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…

690 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