Truncate

  • Comments posted to this topic are about the item Truncate

  • 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

  • Nice and smart - thanks!

  • Awesome.

    Learnt something today 🙂

    M&M

  • 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/

  • a learning for today...!!!

    very good question 🙂

  • Excellent question to get the lazy Monday going .. 😉

  • Learned something new. Thanks for the question.

  • thanks for the question....

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

    🙂

  • 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.

  • 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

  • Thanks for the question!



    Everything is awesome!

  • 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;

  • nice question.. Thanks.

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

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