Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

truncate table Expand / Collapse
Author
Message
Posted Thursday, October 18, 2012 1:37 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 24, 2014 8:36 AM
Points: 30, Visits: 291
to the reply from igor to my post earlier. I didnt say delete the table. I was talking about removing the referenced data e.g

Table A
TableAID int PRIMARY Key IDENTITY(1,1),
data varchar(100)

Table B
TableBID INT PRIMARY Key,
TableAID INT FOREIGN KEY CONSTRAINT,
TableCID INT FOREIGN KEY CONSTRAINT

Table C
TableCID int PRIMARY Key IDENTITY(1,1),
Data Varchar(100)

In the past I've removed linked data that is referenced within TableB and removed them from TableB using deletes before then removing them from TableA or in my case there was more than one place with referenced foreign key data removing it before deleting from the main table did the job. especially as I was using Ids using Identity e.g ID = 1, and referencing Table B using this as a foreignKey.

I was expecting kapils example to be the same as above or similar in which case my advise was to also remove data from any table referencing data as best practice.

of course if data is not being referenced then fine go ahead use the delete and remove data from TableA. I've never had to remove constraints, seems pointless then having to recreate them.

If I misunderstood no worries. Looks like kapils resolved.
Post #1374176
Posted Thursday, October 18, 2012 1:49 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, July 28, 2014 4:35 AM
Points: 2,901, Visits: 2,927
beebkhan (10/18/2012)
to the reply from igor to my post earlier. I didnt say delete the table. I was talking about removing the referenced data e.g

Table A
TableAID int PRIMARY Key IDENTITY(1,1),
data varchar(100)

Table B
TableBID INT PRIMARY Key,
TableAID INT FOREIGN KEY CONSTRAINT,
TableCID INT FOREIGN KEY CONSTRAINT

Table C
TableCID int PRIMARY Key IDENTITY(1,1),
Data Varchar(100)

In the past I've removed linked data that is referenced within TableB and removed them from TableB using deletes before then removing them from TableA or in my case there was more than one place with referenced foreign key data removing it before deleting from the main table did the job. especially as I was using Ids using Identity e.g ID = 1, and referencing Table B using this as a foreignKey.

I was expecting kapils example to be the same as above or similar in which case my advise was to also remove data from any table referencing data as best practice.

of course if data is not being referenced then fine go ahead use the delete and remove data from TableA. I've never had to remove constraints, seems pointless then having to recreate them.

If I misunderstood no worries. Looks like kapils resolved.



Hi,
Nice to reply.
I thought even not to remove the data as well as not to delete the table. You can retain the data with a default or nullable key for the FK using the ON DELETE adds to the FK definition.
Thanks
IgorMi






Igor Micev,
SQL Server developer at Seavus
www.seavus.com
Post #1374178
Posted Thursday, October 18, 2012 2:00 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 12:11 PM
Points: 42,470, Visits: 35,541
kapil_kk (10/18/2012)
I think after disabling constraint of both tables truncate should work..


No, it shouldn't. Disabled constraint is still a constraint. For a truncate the table must not be referenced by a foreign key constraint at all.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1374186
Posted Thursday, October 18, 2012 4:05 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 25, 2014 12:34 AM
Points: 1,888, Visits: 2,326
thanks for the info Gila


_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1374240
Posted Thursday, October 18, 2012 4:06 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 25, 2014 12:34 AM
Points: 1,888, Visits: 2,326
thanks beeb...
I am able to migrate the data after deleting data through delete statement rather than using Truncate statement



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1374241
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse