Truncate

  • sqlnaive

    SSCoach

    Points: 17435

    Comments posted to this topic are about the item Truncate

  • Koen Verbeeck

    SSC Guru

    Points: 258907

    Interesting, very interesting. Thanks for the question.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Revenant

    SSC-Forever

    Points: 42467

    Nice and smart - thanks!

  • M&M

    SSC-Insane

    Points: 21679

    Awesome.

    Learnt something today 🙂

    M&M

  • kapil_kk

    SSC-Insane

    Points: 21316

    Koen Verbeeck (9/29/2013)


    Interesting, very interesting. Thanks for the question.

    +1

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Mr. Kapsicum

    SSCertifiable

    Points: 6128

    a learning for today...!!!

    very good question 🙂

  • twin.devil

    SSC-Insane

    Points: 22208

    Excellent question to get the lazy Monday going .. 😉

  • ksatpute123

    Hall of Fame

    Points: 3325

    Learned something new. Thanks for the question.

  • balde

    SSCommitted

    Points: 1594

    thanks for the question....

    and for anyone else asking the question "why would you?":

    🙂

  • Dscheypie

    Default port

    Points: 1484

    While DELETE is a pure DML statement which has no side effects on an IDENTITY, TRUNCATE resets the IDENTITY and needs ALTER TABLE permission, which appearently means that the TRUNCATE command counts to the DDL statements.

    Interesting!

    ________________________________________________________
    If you set out to do something, something else must be done first.

  • sqlnaive

    SSCoach

    Points: 17435

    balde (9/30/2013)


    thanks for the question....

    and for anyone else asking the question "why would you?":

    🙂

    Thanks balde for posting the link. Definitely there might be some rare cases where such implementation is required. 🙂

  • This was removed by the editor as SPAM

  • Dana Medley

    SSCertifiable

    Points: 6764

    Thanks for the question!



    Everything is awesome!

  • sknox

    SSChampion

    Points: 12216

    Good question, but the answer isn't quite right.

    You cannot use TRUNCATE TABLE on tables that are referenced by a FOREIGN KEY constraint UNLESS that table has a foreign key that references itself.

    implies that it's okay to truncate a table with foreign keys as long as there's a self-referential foreign key involved. A more accurate explanation would be

    You cannot use TRUNCATE TABLE on tables that are referenced by a FOREIGN KEY constraint UNLESS all foreign key references on the table are also from the table (i.e, self-referential.)

    Try

    create table PKFK (

    PK int not null primary key,

    FK int null foreign key references PKFK(PK)

    );

    insert PKFK values (1,null), (2,1), (3,2);

    truncate table PKFK;

    create table FK (

    FK int null foreign key references PKFK(PK)

    );

    insert PKFK values (1,null), (2,1), (3,2);

    insert FK values (1), (2);

    truncate table PKFK;

    drop table FK;

    drop table PKFK;

  • PChiragS

    SSCarpal Tunnel

    Points: 4965

    nice question.. Thanks.

Viewing 15 posts - 1 through 15 (of 22 total)

You must be logged in to reply to this topic. Login to reply