SQL Server Performance

Get rid of all foreign key constraints on table

Discussion in 'Contribute Your SQL Server Scripts' started by satya, May 25, 2004.

  1. satya Moderator

    select 'alter table ' + t2.name + ' drop constraint ' + t3.name
    from sysreferences a,
    sysobjects t2,
    sysobjects t3
    where
    a.fkeyid = t2.id
    and
    a.constid = t3.id
    and t2.name like '%myttable%'


    Here is one for the check-constraints:

    select 'alter table ' + t2.name + ' drop constraint ' + t3.name
    from sysconstraints a,
    sysobjects t2,
    sysobjects t3
    where
    a.id = t2.id
    and
    a.constid = t3.id
    and t3.type ='C'
    and t2.name like '%mytable%'


    Satya SKJ
    Moderator
    http://www.SQL-Server-Performance.Com/forum
    This posting is provided “AS IS” with no rights for the sake of knowledge sharing.
  2. Argyle New Member

    A script to enable would be nice too <img src='/community/emoticons/emotion-5.gif' alt=';)' />

Share This Page