24

I have a table 'users' with 'login' column defined as:

[login] VARCHAR(50) UNIQUE NOT NULL

Now I want to remove this unique constraint/index using SQL script. I found its name UQ_users_7D78A4E7 in my local database but I suppose it has a different name on another database.

What is the best way to drop this unique constraint? Or at least any...

Thanks.

SKINDER
  • 950
  • 3
  • 17
  • 39
  • 4
    http://stackoverflow.com/questions/1430456/how-to-drop-sql-default-constraint-without-knowing-its-name – Mitch Wheat Mar 31 '11 at 12:11
  • @Mitch Wheat: I found my unique constraint in sys.indexes table but I have not found any links to table which contains this index. Any help? – SKINDER Mar 31 '11 at 13:01
  • @Mitch Wheat: I have found a link to my table - it is object_id, but I cannot find a link to necessary column... – SKINDER Mar 31 '11 at 13:15
  • I think it is not possible in full SQL (but OK in script as proposed by @Rail). The best way is to copy the column to a new one, to set your constraints in the new column, to destroy the old one and then to rename the new column with the old column name. – philnext Mar 31 '19 at 15:18

16 Answers16

36
ALTER TABLE users
DROP CONSTRAINT 'constraints_name'

if earlier constraints_name is not provided, it will have some default constraint_name, in pgAdmin 4 (pSql), try violating the constraint and you can see the constraint_name being violated in the error received, most probably same must be the case with other platforms or there are some articles available over web where constraint_name is extracted from certain tables where they are stored, not sure about this though. P.S : Can take reference from comments also

Avadhesh
  • 4,519
  • 5
  • 33
  • 44
26

SKINDER, your code does not use column name. Correct script is:

declare @table_name nvarchar(256)  
declare @col_name nvarchar(256)  
declare @Command  nvarchar(1000)  

set @table_name = N'users'
set @col_name = N'login'

select @Command = 'ALTER TABLE ' + @table_name + ' drop constraint ' + d.name
    from sys.tables t 
    join sys.indexes d on d.object_id = t.object_id  and d.type=2 and d.is_unique=1
    join sys.index_columns ic on d.index_id=ic.index_id and ic.object_id=t.object_id
    join sys.columns c on ic.column_id = c.column_id  and c.object_id=t.object_id
    where t.name = @table_name and c.name=@col_name

print @Command

--execute (@Command)
Rail
  • 700
  • 8
  • 12
  • 2
    This would be broken if there's more than one unique constraint on the column. Unique constraints can span more than one column, and it's possible for a single column to be part of more than one unique constraint. – Jim Mar 20 '14 at 15:26
  • does not works on sql server, it keeps saying that my constraint is not a constraint – GuidoG Mar 13 '19 at 08:03
  • 1
    This is the script updated for SQL Server 2017: https://pastebin.com/FerWr612 – Dawid Sibiński Feb 04 '20 at 12:19
10

This works mostly.

drop index IX_dbo_YourTableName__YourColumnName on dbo.YourTableName
GO
fluidguid
  • 1,511
  • 14
  • 25
8

To drop a UNIQUE constraint, you don’t need the name of the constraint, just the list of columns that are included in the constraint.

The syntax would be:

ALTER TABLE table_name DROP UNIQUE (column1, column2, . . . )
GJK
  • 37,023
  • 8
  • 55
  • 74
juliG
  • 129
  • 1
  • 1
3

You can use following script :

Declare @Cons_Name NVARCHAR(100)
Declare @Str NVARCHAR(500)

SELECT @Cons_Name=name
FROM sys.objects
WHERE type='UQ' AND OBJECT_NAME(parent_object_id) = N'TableName';

---- Delete the unique constraint.
SET @Str='ALTER TABLE TableName DROP CONSTRAINT ' + @Cons_Name;
Exec (@Str)
GO
3

Use this SQL command to drop a unique constraint:

ALTER TABLE tbl_name
DROP INDEX column_name
beginner
  • 2,024
  • 5
  • 43
  • 76
  • Msg 1018, Level 15, State 1, Line 2 Incorrect syntax near 'INDEX'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax. – Jamie Dec 28 '15 at 20:37
  • Error Executing Database Query. 'column_name' is not a constraint. – Pradeep Kumar Nov 30 '16 at 08:35
  • @PradeepKumarPrabaharan that `column_name` should be replaced by your column name – beginner Dec 08 '16 at 01:17
  • haha @beginner i am not a beginner.. don't know why the error came.. i created duplicate table and changed it.. thank you.. – Pradeep Kumar Dec 08 '16 at 04:52
2

This statement works for me

  ALTER TABLE table_name DROP UNIQUE (column_name);
Vaseph
  • 704
  • 1
  • 8
  • 20
0

Expand to database name >> expand to table >> expand to keys >> copy the name of key then execute the below command:

ALTER TABLE Test DROP UQ__test__3213E83EB607700F;

Here UQ__test__3213E83EB607700F is the name of unique key which was created on a particular column on test table.

Jason Clark
  • 1,307
  • 6
  • 26
  • 51
0

I would like to refer a previous question, Because I have faced same problem and solved by this solution. First of all a constraint is always built with a Hash value in it's name. So problem is this HASH is varies in different Machine or Database. For example DF__Companies__IsGlo__6AB17FE4 here 6AB17FE4 is the hash value(8 bit). So I am referring a single script which will be fruitful to all

DECLARE @Command NVARCHAR(MAX)
     declare @table_name nvarchar(256)
     declare @col_name nvarchar(256)
     set @table_name = N'ProcedureAlerts'
     set @col_name = N'EmailSent'

     select @Command ='Alter Table dbo.ProcedureAlerts Drop Constraint [' + ( select d.name
     from 
         sys.tables t
         join sys.default_constraints d on d.parent_object_id = t.object_id
         join sys.columns c on c.object_id = t.object_id
                               and c.column_id = d.parent_column_id
     where 
         t.name = @table_name
         and c.name = @col_name) + ']'

    --print @Command
    exec sp_executesql @Command

It will drop your default constraint. However if you want to create it again you can simply try this

ALTER TABLE [dbo].[ProcedureAlerts] ADD DEFAULT((0)) FOR [EmailSent]

Finally, just simply run a DROP command to drop the column.

Ananda G
  • 2,389
  • 23
  • 39
0

I have stopped on the script like below (as I have only one non-clustered unique index in this table):

declare @table_name nvarchar(256)  
declare @col_name nvarchar(256)  
declare @Command  nvarchar(1000)  

set @table_name = N'users'
set @col_name = N'login'

select @Command = 'ALTER TABLE ' + @table_name + ' drop constraint ' + d.name
    from sys.tables t join sys.indexes d on d.object_id = t.object_id  
    where t.name = @table_name and d.type=2 and d.is_unique=1

--print @Command

execute (@Command)

Has anyone comments if this solution is acceptable? Any pros and cons?

Thanks.

SKINDER
  • 950
  • 3
  • 17
  • 39
  • If it works then it's a good script :) don't labour over the problem if this has solved your problem. It's not as though you will be executing a script like this every minute. – Tony Apr 01 '11 at 07:38
0

I had the same problem. I'm using DB2. What I have done is a bit not too professional solution, but it works in every DBMS:

  1. Add a column with the same definition without the unique contraint.
  2. Copy the values from the original column to the new
  3. Drop the original column (so DBMS will remove the constraint as well no matter what its name was)
  4. And finally rename the new one to the original
  5. And a reorg at the end (only in DB2)
ALTER TABLE USERS ADD COLUMN LOGIN_OLD VARCHAR(50) NOT NULL DEFAULT '';
UPDATE USERS SET LOGIN_OLD=LOGIN;
ALTER TABLE USERS DROP COLUMN LOGIN;
ALTER TABLE USERS RENAME COLUMN LOGIN_OLD TO LOGIN;

CALL SYSPROC.ADMIN_CMD('REORG TABLE USERS');

The syntax of the ALTER commands may be different in other DBMS

PowR
  • 800
  • 9
  • 13
0

FOR SQL to drop a constraint

ALTER TABLE [dbo].[tablename] DROP CONSTRAINT [unique key created by sql] GO

alternatively: go to the keys -- right click on unique key and click on drop constraint in new sql editor window. The program writes the code for you.

Hope this helps. Avanish.

0

To find all system generated unique constraint names and other information related to it on any database.

You may use below query and enhance it as per your need:

SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE WHERE CONSTRAINT_NAME LIKE '%UQ%'

Final query to drop all unique constraint through database. You may add where clause to restrict it to one table:

    DECLARE @uqQuery NVARCHAR(MAX)
    SET @uqQuery = SUBSTRING( (SELECT '; ' + 'ALTER TABLE [' + Table_Schema+'].['+Table_Name
        +']  DROP CONSTRAINT ['+CONSTRAINT_NAME+']'

    FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE WHERE CONSTRAINT_NAME LIKE '%UQ%'
    FOR XML PATH('')), 2,  2000000)
    SELECT @uqQuery
0
   ALTER TABLE dbo.table
DROP CONSTRAINT uq_testConstrain

constraint name uq_testConstrain can be found under database->table->keys folder

Jaydeep Shil
  • 1,894
  • 22
  • 21
0

for MSSQL use following codde

IF  EXISTS(SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu WHERE TABLE_NAME = 'tableName' and COLUMN_NAME = 'colName')
BEGIN 
Declare @con varchar(50);
Declare @sqlStatement varchar(500);

select @con = CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu WHERE TABLE_NAME = 'tableName' and COLUMN_NAME = 'colName'
SET @sqlStatement = 'ALTER TABLE tableName DROP CONSTRAINT ' + @con;
    
EXECUTE (@sqlStatement)
END
GO
Manoj H U
  • 37
  • 9
-1

If you know the name of your constraint then you can directly use the command like

alter table users drop constraint constraint_name;

If you don't know the constraint name, you can get the constraint by using this command

select constraint_name,constraint_type from user_constraints where table_name = 'YOUR TABLE NAME';

Ram Thota
  • 539
  • 5
  • 9