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

发布于 2024-10-28 18:39:53 字数 255 浏览 4 评论 0原文

我有一个表“users”,其“login”列定义为:

[login] VARCHAR(50) UNIQUE NOT NULL

现在我想使用 SQL 脚本删除此唯一约束/索引。我在本地数据库中找到了它的名称 UQ_users_7D78A4E7,但我认为它在另一个数据库上有不同的名称。

放弃这个独特约束的最佳方法是什么?或者至少任何...

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

不如归去 2024-11-04 18:39:53
ALTER TABLE users
DROP CONSTRAINT 'constraints_name'

如果没有提供早期的constraint_name,它将有一些默认的constraint_name,在pgAdmin 4(pSql)中,尝试违反约束,您可以在收到的错误中看到constraint_name被违反,很可能与其他平台或那里的情况相同网络上有一些文章,其中constraint_name是从存储它们的某些表中提取的,但不确定这一点。
PS:也可以参考评论

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

彼岸花似海 2024-11-04 18:39:53

SKINDER,您的代码不使用列名称。正确的脚本是:

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)

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)
往事风中埋 2024-11-04 18:39:53

这大部分有效。

drop index IX_dbo_YourTableName__YourColumnName on dbo.YourTableName
GO

This works mostly.

drop index IX_dbo_YourTableName__YourColumnName on dbo.YourTableName
GO
何时共饮酒 2024-11-04 18:39:53

要删除 UNIQUE 约束,您不需要约束的名称,只需要
约束中包含的列的列表。

语法是:

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

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, . . . )
揽月 2024-11-04 18:39:53

您可以使用以下脚本:

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

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
平安喜乐 2024-11-04 18:39:53

使用以下 SQL 命令删除唯一约束:

ALTER TABLE tbl_name
DROP INDEX column_name

Use this SQL command to drop a unique constraint:

ALTER TABLE tbl_name
DROP INDEX column_name
溺孤伤于心 2024-11-04 18:39:53

这个声明对我有用

  ALTER TABLE table_name DROP UNIQUE (column_name);

This statement works for me

  ALTER TABLE table_name DROP UNIQUE (column_name);
苍白女子 2024-11-04 18:39:53

对于 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

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
我很坚强 2024-11-04 18:39:53

我已经停止了像下面这样的脚本(因为我在这个表中只有一个非聚集唯一索引):

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)

如果这个解决方案可以接受,有人评论吗?有什么优点和缺点吗?

谢谢。

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.

信愁 2024-11-04 18:39:53

我想参考之前的问题,因为我遇到了同样的问题并通过此解决了 解决方案
首先,约束总是在其名称中使用 Hash 值构建。所以问题是这个HASH在不同的机器或数据库中是不同的。例如 DF__Companies__IsGlo__6AB17FE4 这里 6AB17FE4 是哈希值(8 位)。所以我指的是一个对所有人都富有成效的脚本,

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

它会放弃你的默认约束。但是,如果您想再次创建它,您可以简单地尝试这个

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

最后,只需运行 DROP 命令即可删除该列。

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.

爺獨霸怡葒院 2024-11-04 18:39:53

我也有同样的问题。我正在使用 DB2。我所做的是有点不太专业的解决方案,但它适用于每个 DBMS:

  1. 添加具有相同定义的列,而无需唯一约束。
  2. 将值从原始列复制到新列
  3. 删除原始列(因此 DBMS 也会删除约束,无论其名称是什么)
  4. 最后将新列重命名为原始列
  5. 最后进行重组(仅在 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');

ALTER 命令的语法在其他 DBMS 中可能有所不同

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

蓝梦月影 2024-11-04 18:39:53

在任何数据库上查找所有系统生成的唯一约束名称以及与其相关的其他信息。

您可以使用以下查询并根据您的需要对其进行增强:

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

最终查询通过数据库删除所有唯一约束。您可以添加 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

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
魂牵梦绕锁你心扉 2024-11-04 18:39:53

对于 Sql server 2014,您可以使用以下查询删除唯一键

首先使用以下命令查找唯一键名称

执行 sys.sp_helpindex @objname = N't_Party'

DROP INDEX [KEY_NAME] ON [dbo].[TABLE_NAME]

--In my case there ys t_Party table name and 'IX_t_Party' this is unique key name

DROP INDEX [IX_t_Party] ON [dbo].[t_Party]

For Sql server 2014 you can remove unique key using below query

First find unique key name using

EXEC sys.sp_helpindex @objname = N't_Party'

DROP INDEX [KEY_NAME] ON [dbo].[TABLE_NAME]

--In my case there ys t_Party table name and 'IX_t_Party' this is unique key name

DROP INDEX [IX_t_Party] ON [dbo].[t_Party]
孤独陪着我 2024-11-04 18:39:53

展开至数据库名称>>展开至表格 >>展开至键 >>复制键的名称,然后执行以下命令:

ALTER TABLE Test DROP UQ__test__3213E83EB607700F;

这里 UQ__test__3213E83EB607700F 是在测试表上的特定列上创建的唯一键的名称。

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.

自我难过 2024-11-04 18:39:53

FOR SQL 删除约束

ALTER TABLE [dbo].[tablename] DROP CONSTRAINT [sql 创建的唯一键]
或者去


转到键 - 右键单击​​唯一键,然后在新的 sql 编辑器窗口中单击删除约束。
该程序会为您编写代码。

希望这有帮助。
阿尼什。

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.

再见回来 2024-11-04 18:39:53
   ALTER TABLE dbo.table
DROP CONSTRAINT uq_testConstrain

约束名称uq_testConstrain可以在database->table->keys文件夹下找到

   ALTER TABLE dbo.table
DROP CONSTRAINT uq_testConstrain

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

梦一生花开无言 2024-11-04 18:39:53

如果你知道约束的名称,那么你可以直接使用命令

alter table users drop constrain_name;

如果你不知道约束的名称,你可以使用这个命令获取约束

<强>选择约束名称,约束类型
来自用户约束
其中 table_name = '您的表名称';

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';

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文