How can I drop all the constraints in a database

  • Hi all,

    Does anyone know of a script I can run that can drop all the constraints in a database kindly share with me. Thanks

    Regards

    Akin

  • ALTER TABLE <yourtablename> DROP CONSTRAINT <constraint_names>


    Kindest Regards,

    karthik

  • Thanks for your prompt response but the script will only drop the constraint one table at a time. I need what can drop all constraints in a database at once...

  • There isn't a canned script that will do what you are asking for.  You must create one using the syntax that karthik suggested.  Depending on what types of constraints you intend to drop, you may be able to generate a good portion of your drop contraints script using EM>>generate sql script.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • select 'ALTER TABLE ' + so.name + ' DROP CONSTRAINT ' + sc.name

    from sysobjects as so

    inner join sysobjects as sc on sc.parent_obj = so.id

    where sc.xtype = 'C'

    order by so.name

  • Similar to Luis brilliant solution, but without the where clause:

    select 'ALTER TABLE ' + so.name + ' DROP CONSTRAINT ' + sc.name

    from sysobjects so

    inner join sysobjects sc on sc.parent_obj = so.id

  • The scripts offered will certainly work (although you may want to add WHERE so.XTYPE = 'U' to filter on tables only), but tell me please... why would you want to do this?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks guys for your fabulous response and I've found a way from your posts which is to drop all constraints with dropping foreign keys first and then other keys accordingly. I need to do this because I want to drop some tables in a database and send the remaining tables to DVD and I don't need the constraints all I require is the data within the tables for export to DVD. So it does not matter if I drop all the constraints as I don't need them anyway...

    Thanks again and you've all made my day

Viewing 8 posts - 1 through 7 (of 7 total)

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