Is there any way to delete records withouta dropping FKs

  • Can someone send the script for : SQL automatically cascade deletes to child data

    Is there any way to delete records without dropping FKs

  • You could disable the foreign keys using something like:

    ALTER TABLE dbo.cnst_example NOCHECK CONSTRAINT fk_salary_cap;

    ALTER TABLE dbo.cnst_example CHECK CONSTRAINT fk_salary_cap;

    CEWII

  • suresh0123456789 (7/1/2009)


    Is there any way to delete records without dropping FKs

    Delete the child records first.

    If you want to make the fk cascade, look up ALTER TABLE in Books online. That'll give the syntax for dropping the fk and recreating with the cascade option.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    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
  • Script file :

  • John Paul (7/1/2009)


    http://www.sqlteam.com/article/performing-a-cascade-delete-in-sql-server-7

    That's written for SQL 7. Sure you want to use that?

    I don't get why you want a specialised proc to do it. If you're got two tables Parent and Child with a foreign key, delete the rows from Child first, then delete the rows from Parent.

    CREATE Table Parent (

    id int identity primary key,

    SomeString varchar (50),

    ToBeDeleted bit default 0

    )

    Create Table Child (

    id int identity primary key,

    ParentID int Foreign key references Parent (id),

    SomeString varchar (50)

    )

    GO

    Insert into Parent (SomeString, ToBeDeleted) Values ('a',0)

    Insert into Parent (SomeString, ToBeDeleted) Values ('b',1)

    Insert into Parent (SomeString, ToBeDeleted) Values ('c',0)

    Insert into Child (ParentID, SomeString) values (1,'aa')

    Insert into Child (ParentID, SomeString) values (1,'ab')

    Insert into Child (ParentID, SomeString) values (1,'ac')

    Insert into Child (ParentID, SomeString) values (2,'ba')

    Insert into Child (ParentID, SomeString) values (2,'bb')

    Insert into Child (ParentID, SomeString) values (3,'cc')

    delete from Parent where ToBeDeleted = 1 /* fails with a fk violation*/

    -- delete from the child table first

    delete from Child where ParentID IN (select ID from Parent where ToBeDeleted = 1)

    -- delete from the parent table after

    delete from Parent where ToBeDeleted = 1

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    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
  • An alternative is to set the foreign key in the child table to NULL before deleting the parent.

    In Gails example this would mean adding the following just before deleting the parent rows:

    update child set ParentID=NULL where ParentID IN (select ID from Parent where ToBeDeleted = 1)

    This would keep the child records, albeit orphaned (aahh, poor little things :-)).

    It all depends on what you want to acheive.

  • suresh0123456789 (7/1/2009)Is there any way to delete records without dropping FKs

    Yes, doing it the right way -please refer to Gail's post.

    I'm shocked and appalled some people have a total disregard for Referential Integrity. RF is set for one reason and one reason only, to protect the integrity of data. Why people insist in taking actions that may lead to trashing a database is something beyond my imagination. :doze: Sad.

    _____________________________________
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at Amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
  • I'm shocked and appalled some people have a total disregard for Referential Integrity.

    Can't say I'm shocked... Appalled, yes.;-)

  • Ian Scarlett (7/3/2009)


    I'm shocked and appalled some people have a total disregard for Referential Integrity.

    Can't say I'm shocked... Appalled, yes.;-)

    Absolutely !!! Downright stupid 😉

  • I'm shocked and appalled some people have a total disregard for Referential Integrity.

    Sometimes a DBA's gotta do what a DBA's gotta do...

    -MarkO

    "You do not really understand something until you can explain it to your grandmother" - Albert Einstein

  • molson (7/6/2009)


    I'm shocked and appalled some people have a total disregard for Referential Integrity.

    Sometimes a DBA's gotta do what a DBA's gotta do...

    :blink: do you mean... trash referential integrity? 😀

    _____________________________________
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at Amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
  • PaulB (7/6/2009)


    molson (7/6/2009)


    I'm shocked and appalled some people have a total disregard for Referential Integrity.

    Sometimes a DBA's gotta do what a DBA's gotta do...

    :blink: do you mean... trash referential integrity? 😀

    or more correctly:

    Sometimes a DBA's gotta do in a non-production environment what a DBA's gotta do in a non-production environment...

    -MarkO

    "You do not really understand something until you can explain it to your grandmother" - Albert Einstein

Viewing 12 posts - 1 through 11 (of 11 total)

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