Solved

truncate tables with foreign key reference

Posted on 2008-10-17
9
1,258 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

759 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

13 Experts available now in Live!

Get 1:1 Help Now