Generation 5 » Dropping Unique Constraints in SQL Server

 Subscribe to our RSS Feed | About Us

Dropping Unique Constraints in SQL Server

Search for:

The problem

Search

I got started with relational databases with mysql,  so I’m in the habit of making
database changes with SQL scripts,  rather than using a GUI.  Microsoft SQL Server
requires that we specify the name of a unique constraint when we want to drop it.  If
you’re thinking ahead,  you can specify a name when you create the constraint;  if
you don’t,  SQL Server will make up an unpredictable name,  so you can’t write a
simple script to drop the constraint.

A Solution
In the spirit of “How To Drop A Primary Key in SQL Server“,  here’s a stored
procedure that queries the data dictionary to find the names of any unique constraint
on a specific table and column and drop them:
CREATE PROCEDURE [dbo].[DropUniqueConstraint]
    @tableName NVarchar(255),
    @columnName NVarchar(255)
AS
    DECLARE @IdxNames CURSOR
   
    SET @IdxNames = CURSOR FOR
        select sysindexes.name from sysindexkeys,syscolumns,sysindexes
            WHERE
                syscolumns.[id] = OBJECT_ID(N'[dbo].['+@tableName+N']')
                AND sysindexkeys.[id] = OBJECT_ID(N'[dbo].['+@tableName+N']')
                AND sysindexes.[id] = OBJECT_ID(N'[dbo].['+@tableName+N']')
                AND syscolumns.name=@columnName
                AND sysindexkeys.colid=syscolumns.colid
                AND sysindexes.[indid]=sysindexkeys.[indid]
                AND (
                    SELECT COUNT(*) FROM sysindexkeys AS si2
                    WHERE si2.id=sysindexes.id
                    AND si2.indid=sysindexes.indid
                )=1
    OPEN @IdxNames
    DECLARE @IdxName Nvarchar(255)
    FETCH NEXT FROM @IdxNames INTO @IdxName   
    WHILE @@FETCH_STATUS = 0 BEGIN
        DECLARE @dropSql Nvarchar(4000)
        SET @dropSql=
            N'ALTER TABLE  ['+@tableName+ N']
                DROP CONSTRAINT ['+@IdxName+ N']'
        EXEC(@dropSql)
           
        FETCH NEXT FROM @IdxNames
        INTO @IdxName
    END
CLOSE @IdxNames
DEALLOCATE @IdxNames

Usage is straightforward:
EXEC [dbo].[DropUniqueConstraint]
    @tableName='TargetTable',
    @columnName='TargetColumn'

This script has a limitation:  it only drops unique constraints that act on a single
column,  not constraints that act on multiple columns.   It is smart enough,  however, 
to not drop multiple-column constraints in case one of them involves @columnName .
Feedback from SQL stored procedure wizards would be mostly certainly welcome.

Paul Houle on June 26th 2008 in SQL

Comments
http://gen5.info/q/2008/06/26/dropping-unique-constraints-in-sql-server/[1/16/2014 3:58:30 PM]

Comments (0)
Login

Archives

June 2012 (1)
August 2010 (1)
May 2010 (1)
June 2009 (2)
April 2009 (1)
March 2009 (1)
February 2009 (3)
January 2009 (3)
November 2008 (1)
August 2008 (2)
July 2008 (5)
June 2008 (5)
May 2008 (2)
April 2008 (6)
March 2008 (8)
June 2006 (1)
February 2006 (1)

Categories

AJAX (2)
Asynchronous Communications (16)
Biology (1)
Books (1)
Design (1)
Distributed (1)
Exceptions (2)
Functional Programming (1)
GIS (1)
Ithaca (1)
Japan (1)
Math (1)
Media (3)
Nature (1)
Semantic Web (3)
Tools (28)
CRUD (1)
Dot Net (17)
Freebase (2)
GWT (9)
Java (7)
Linq (2)
PHP (6)
Server Frameworks (1)
Silverlight (12)
SQL (5)
Uncategorized (1)
Web (2)
Analytics (1)
Generation 5 » Dropping Unique Constraints in SQL Server

There are no comments posted yet. Be the first one!

Post a new comment
Enter text right here!

Comment as a Guest, or login:
Name

Email

Website (optional)

Displayed next to your comments.

Not displayed publicly.

If you have a website, link to it here.

None
Subscribe to None

Submit Comment

Copyright © 2013 Generation 5.
WordPress Theme design.

http://gen5.info/q/2008/06/26/dropping-unique-constraints-in-sql-server/[1/16/2014 3:58:30 PM]

Dropping unique constraints in sql server

  • 1.
    Generation 5 »Dropping Unique Constraints in SQL Server  Subscribe to our RSS Feed | About Us Dropping Unique Constraints in SQL Server Search for: The problem Search I got started with relational databases with mysql,  so I’m in the habit of making database changes with SQL scripts,  rather than using a GUI.  Microsoft SQL Server requires that we specify the name of a unique constraint when we want to drop it.  If you’re thinking ahead,  you can specify a name when you create the constraint;  if you don’t,  SQL Server will make up an unpredictable name,  so you can’t write a simple script to drop the constraint. A Solution In the spirit of “How To Drop A Primary Key in SQL Server“,  here’s a stored procedure that queries the data dictionary to find the names of any unique constraint on a specific table and column and drop them: CREATE PROCEDURE [dbo].[DropUniqueConstraint]     @tableName NVarchar(255),     @columnName NVarchar(255) AS     DECLARE @IdxNames CURSOR         SET @IdxNames = CURSOR FOR         select sysindexes.name from sysindexkeys,syscolumns,sysindexes             WHERE                 syscolumns.[id] = OBJECT_ID(N'[dbo].['+@tableName+N']')                 AND sysindexkeys.[id] = OBJECT_ID(N'[dbo].['+@tableName+N']')                 AND sysindexes.[id] = OBJECT_ID(N'[dbo].['+@tableName+N']')                 AND syscolumns.name=@columnName                 AND sysindexkeys.colid=syscolumns.colid                 AND sysindexes.[indid]=sysindexkeys.[indid]                 AND (                     SELECT COUNT(*) FROM sysindexkeys AS si2                     WHERE si2.id=sysindexes.id                     AND si2.indid=sysindexes.indid                 )=1     OPEN @IdxNames     DECLARE @IdxName Nvarchar(255)     FETCH NEXT FROM @IdxNames INTO @IdxName        WHILE @@FETCH_STATUS = 0 BEGIN         DECLARE @dropSql Nvarchar(4000)         SET @dropSql=             N'ALTER TABLE  ['+@tableName+ N']                 DROP CONSTRAINT ['+@IdxName+ N']'         EXEC(@dropSql)                     FETCH NEXT FROM @IdxNames         INTO @IdxName     END CLOSE @IdxNames DEALLOCATE @IdxNames Usage is straightforward: EXEC [dbo].[DropUniqueConstraint]     @tableName='TargetTable',     @columnName='TargetColumn' This script has a limitation:  it only drops unique constraints that act on a single column,  not constraints that act on multiple columns.   It is smart enough,  however,  to not drop multiple-column constraints in case one of them involves @columnName . Feedback from SQL stored procedure wizards would be mostly certainly welcome. Paul Houle on June 26th 2008 in SQL Comments http://gen5.info/q/2008/06/26/dropping-unique-constraints-in-sql-server/[1/16/2014 3:58:30 PM] Comments (0) Login Archives June 2012 (1) August 2010 (1) May 2010 (1) June 2009 (2) April 2009 (1) March 2009 (1) February 2009 (3) January 2009 (3) November 2008 (1) August 2008 (2) July 2008 (5) June 2008 (5) May 2008 (2) April 2008 (6) March 2008 (8) June 2006 (1) February 2006 (1) Categories AJAX (2) Asynchronous Communications (16) Biology (1) Books (1) Design (1) Distributed (1) Exceptions (2) Functional Programming (1) GIS (1) Ithaca (1) Japan (1) Math (1) Media (3) Nature (1) Semantic Web (3) Tools (28) CRUD (1) Dot Net (17) Freebase (2) GWT (9) Java (7) Linq (2) PHP (6) Server Frameworks (1) Silverlight (12) SQL (5) Uncategorized (1) Web (2) Analytics (1)
  • 2.
    Generation 5 »Dropping Unique Constraints in SQL Server There are no comments posted yet. Be the first one! Post a new comment Enter text right here! Comment as a Guest, or login: Name Email Website (optional) Displayed next to your comments. Not displayed publicly. If you have a website, link to it here. None Subscribe to None Submit Comment Copyright © 2013 Generation 5. WordPress Theme design. http://gen5.info/q/2008/06/26/dropping-unique-constraints-in-sql-server/[1/16/2014 3:58:30 PM]