Re: Strange behavior with identity columns...any advice?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Itzik Ben-Gan (itzik_at_REMOVETHIS.SolidQualityLearning.com)
Date: 10/17/04


Date: Sun, 17 Oct 2004 11:09:42 +0200


> I have to disagree. TRUNCATE does not work on referenced tables and it
> does not fire TRIGGERs. If I am using FK or TRIGGERs for some of my
> data integrity, I can be screwed by this vendor extension.

That's true.
Regarding referencing tables, you cannot use DELETE either to clear a table
if there are referencing rows, unless you're cascading or clearing the
referencing tables first. If we're talking about clearing both referencing
and referenced tables, you can drop the FKs, truncate, then create them back
in the same transaction.
As for triggers, you do need to explicitly substitute their intended
activity.
Usually, because TRUNCATE is so much faster, it's worthwhile.
I do agree that the DBCC CHECKIDENT inconsistency generates confusion here,
and complicates things.

> As I recall all it did in early versions of T-SQL was move an EOF
> pointer to the start of the physical storage for a table to wipe out the
> contigous file stucture. That made it very fast, but thre were no
> safety checks. Has it gotten smarter under the covers?

TRUNCATE simply deallocates extents, and logs only the extent addresses in
the transaction log (for rollback purposes), while a DELETE is fully logged.
That's the reason for the performance difference.
What do you mean by no safety checks?

-- 
BG, SQL Server MVP
www.SolidQualityLearning.com
"Joe Celko" <jcelko212@earthlink.net> wrote in message 
news:OE7$pg9sEHA.3556@TK2MSFTNGP10.phx.gbl...
>>> Since TRUNCATE is the way to go when you want to empty a table, by
> using it exclusively, you can ensure consistency. <<
>
> I have to disagree.  TRUNCATE does not work on referenced tables and it
> does not fire TRIGGERs.  If I am using FK or TRIGGERs for some of my
> data integrity, I can be screwed by this vendor extension.
>
> As I recall all it did in early versions of T-SQL was move an EOF
> pointer to the start of the physical storage for a table to wipe out the
> contigous file stucture.  That made it very fast, but thre were no
> safety checks.  Has it gotten smarter under the covers?
>
> --CELKO--
> Please post DDL, so that people do not have to guess what the keys,
> constraints, Declarative Referential Integrity, datatypes, etc. in your
> schema are. Sample data is also a good idea, along with clear
> specifications.
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it! 


Relevant Pages

  • Re: truncate all tables
    ... You can't truncate a table if it is referenced by a foreign key. ... Disabling the foreign key constraint or emptying the referencing ... > Thanks for any help Mikey ...
    (microsoft.public.sqlserver.programming)
  • Re: Truncate table being referenced by a foriegn key
    ... The error message pretty much speaks for itself. ... to a table, you cannot use TRUNCATE TABLE, even if the referencing table has ... "Abhishek Srivastava" wrote in message ...
    (microsoft.public.sqlserver.programming)
  • Re: Truncate one table
    ... > hi Jaygo, ... > its because the table that you are trying to truncate is being referenced ... > In this case you will have to remove referencing rows from child table ... To see which tables are referenced by parent table make use ...
    (microsoft.public.sqlserver.programming)
  • Re: Truncate one table
    ... its because the table that you are trying to truncate is being referenced by some other table. ... In this case you will have to remove referencing rows from child table first and then truncate ... To see which tables are referenced by parent table make use of system stored ...
    (microsoft.public.sqlserver.programming)
  • Re: Enforce uniqueness of a key referenced in multiple tables
    ... >How can I enforce that the key 'InspectieVatID' is only used once ... >accross the two referencing tables? ... >I know I could create triggers on the two tables to do the check but ...
    (microsoft.public.sqlserver.programming)