简体   繁体   English

如何从表列中删除唯一约束?

[英]How to drop a unique constraint from table column?

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.现在我想使用 SQL 脚本删除这个唯一的约束/索引。 I found its name UQ_ users _7D78A4E7 in my local database but I suppose it has a different name on another database.我在本地数据库中找到了它的名字UQ_ users _7D78A4E7但我想它在另一个数据库上有一个不同的名字。

What is the best way to drop this unique constraint?删除此唯一约束的最佳方法是什么? Or at least any...或者至少任何...

Thanks.谢谢。

ALTER TABLE users
DROP CONSTRAINT 'constraints_name'

SKINDER, your code does not use column name. SKINDER,你的代码没有使用列名。 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)

This works mostly.这主要有效。

drop index IX_dbo_YourTableName__YourColumnName on dbo.YourTableName
GO

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.要删除 UNIQUE 约束,您不需要约束的名称,只需要包含在约束中的列的列表。

The syntax would be:语法是:

ALTER TABLE table_name DROP UNIQUE (column1, column2, . . . )

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

Use this SQL command to drop a unique constraint:使用此 SQL 命令删除唯一约束:

ALTER TABLE tbl_name
DROP INDEX column_name

这个声明对我有用

  ALTER TABLE table_name DROP UNIQUE (column_name);

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.这里UQ__test__3213E83EB607700F是在测试表的特定列上创建的唯一键的名称。

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.首先,约束总是在其名称中使用Hash值构建。 So problem is this HASH is varies in different Machine or Database.所以问题是这个HASH在不同的机器或数据库中是不同的。 For example DF__Companies__IsGlo__6AB17FE4 here 6AB17FE4 is the hash value(8 bit).例如DF__Companies__IsGlo__6AB17FE4此处6AB17FE4是哈希值(8 位)。 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.最后,只需简单地运行一个DROP命令来删除列。

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.谢谢。

I had the same problem.我有同样的问题。 I'm using DB2.我正在使用 DB2。 What I have done is a bit not too professional solution, but it works in every DBMS:我所做的是有点不太专业的解决方案,但它适用于每个 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)删除原始列(因此无论名称是什么,DBMS 也会删除约束)
  4. And finally rename the new one to the original最后将新的重命名为原来的
  5. And a reorg at the end (only in DB2)最后进行重组(仅在 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 ALTER 命令的语法在其他 DBMS 中可能有所不同

FOR SQL to drop a constraint FOR SQL 删除约束

ALTER TABLE [dbo].[tablename] DROP CONSTRAINT [unique key created by sql] GO ALTER TABLE [dbo].[tablename] DROP CONSTRAINT [由 sql 创建的唯一键] GO

alternatively: go to the keys -- right click on unique key and click on drop constraint in new sql editor window.或者:转到键 - 右键单击​​唯一键,然后单击新 sql 编辑器窗口中的删除约束。 The program writes the code for you.该程序为您编写代码。

Hope this helps.希望这可以帮助。 Avanish.消失。

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:您可以添加 where 子句以将其限制为一张表:

    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
   ALTER TABLE dbo.table
DROP CONSTRAINT uq_testConstrain

constraint name uq_testConstrain can be found under database->table->keys folder约束名称uq_testConstrain可以在 database-> uq_testConstrain >keys 文件夹下找到

for MSSQL use following codde对于 MSSQL 使用以下代码

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

If you know the name of your constraint then you can directly use the command like如果您知道约束的名称,则可以直接使用如下命令

alter table users drop constraint constraint_name;改变表用户删除约束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'; select constraint_name,constraint_type from user_constraints where table_name = 'YOUR TABLE NAME';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM