Truncate Table

  • EZ PZ

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • I have to say that learning that a self-referencing Foreign Key doesn't prevent Truncate while interesting is probably not of that much practical value.

    But I will say thanks for highlighting the very abbreviated form of Foreign Key definition is interesting and I didn't know that it could be reduced that much.

  • good question. but for the life of me, i cannot come up with a reason to use this (nor anyone else where I work)- anybody?

    jg

  • BOL - Restrictions

    You cannot use TRUNCATE TABLE on tables that:

    •Are referenced by a FOREIGN KEY constraint. (You can truncate a table that has a foreign key that references itself.)

    Do we have any chance to use a table that has a foreign key that references itself?

    --------------------------------------
    ;-)“Everything has beauty, but not everyone sees it.” ― Confucius

  • This was removed by the editor as SPAM

  • Thank you for the posting.

    We have many different ways to approach it. Is it a good idea or a bad idea?

    --------------------------------------
    ;-)“Everything has beauty, but not everyone sees it.” ― Confucius

  • gmby (3/28/2013)


    good question. but for the life of me, i cannot come up with a reason to use this (nor anyone else where I work)- anybody?

    jg

    This scenario is often used for hierarchy. Stewart provided an example above.

    --------------------------------------------------------------------------------------
    Hai Ton
    My Db4Breakfast blog.

  • Outside of a hierarchy example, I can't think of when you would have a table foreign key reference to itself.

  • Thanks.

  • Nice one..

    --
    Dineshbabu
    Desire to learn new things..

  • KWymore (3/29/2013)


    Outside of a hierarchy example, I can't think of when you would have a table foreign key reference to itself.

    A piece of machinery that is no longer available because it has been superseded by a different piece of machinery, something like

    create table MachineryParts(

    PartID int identity not null primary key,

    PartDescription nvarchar(100) not null,

    IsAvailable bit not null,

    ReplacedByPartID int constraint fkMachineryPartsReplacement foreign key references MachineryParts

    )

  • Strange situation, but informative.

    Thanks for the question.

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • +1 🙂

    Angad

    --Angad Singh
    If I Stop Learning, I Cease to Be A SIKH !

  • nice and straightforwad - instinct worked for me ..:w00t:;-)

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

  • I got it correct. However assuming some other logic but anyways got corrected by the explanation and got one point (to my bonus) 🙂

Viewing 15 posts - 16 through 30 (of 34 total)

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