SQL Server 2005 如何创建唯一约束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/64981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:22:56  来源:igfitidea点击:

SQL Server 2005 How Create a Unique Constraint?

sqlsql-serverconstraints

提问by David Basarab

How do I create a unique constraint on an existing table in SQL Server 2005?

如何在 SQL Server 2005 中的现有表上创建唯一约束?

I am looking for both the TSQL and how to do it in the Database Diagram.

我正在寻找 TSQL 以及如何在数据库图中进行操作。

回答by Rory

The SQL command is:

SQL命令是:

ALTER TABLE <tablename> ADD CONSTRAINT
            <constraintname> UNIQUE NONCLUSTERED
    (
                <columnname>
    )

See the full syntax here.

请参阅此处的完整语法。

If you want to do it from a Database Diagram:

如果您想从数据库图中执行此操作:

  • right-click on the table and select 'Indexes/Keys'
  • click the Add button to add a new index
  • enter the necessary info in the Properties on the right hand side:
    • the columns you want (click the ellipsis button to select)
    • set Is Unique to Yes
    • give it an appropriate name
  • 右键单击表格并选择“索引/键”
  • 单击添加按钮添加新索引
  • 在右侧的属性中输入必要的信息:
    • 您想要的列(单击省略号按钮进行选择)
    • 设置唯一为是
    • 给它一个合适的名字

回答by James Lawruk

In SQL Server Management Studio Express:

在 SQL Server Management Studio Express 中:

  • Right-click table, choose Modifyor Design(For Later Versions)
  • Right-click field, choose Indexes/Keys...
  • Click Add
  • For Columns, select the field nameyou want to be unique.
  • For Type, choose Unique Key.
  • Click Close, Savethe table.
  • 右键单击表格,选择修改设计(适用于更高版本)
  • 右键单击字段,选择索引/键...
  • 点击添加
  • 对于Columns,选择您希望唯一的字段名称
  • 对于类型,选择唯一键
  • 单击关闭保存表。

回答by WildJoe

ALTER TABLE [TableName] ADD CONSTRAINT  [constraintName] UNIQUE ([columns])

回答by Squirrel

Warning: Only one null row can be in the column you've set to be unique.

警告:在您设置为唯一的列中只能有一个空行。

You can do this with a filtered index in SQL 2008:

您可以使用 SQL 2008 中的过滤索引执行此操作:

CREATE UNIQUE NONCLUSTERED INDEX idx_col1
ON dbo.MyTable(col1)
WHERE col1 IS NOT NULL;

See Field value must be unique unless it is NULLfor a range of answers.

请参阅字段值必须是唯一的,除非它为一系列答案为 NULL

回答by Ivan Bosnic

ALTER TABLE dbo.<tablename> ADD CONSTRAINT
            <namingconventionconstraint> UNIQUE NONCLUSTERED
    (
                <columnname>
    ) ON [PRIMARY]

回答by David Basarab

I also found you can do this via, the database diagrams.

我还发现你可以通过数据库图表来做到这一点。

By right clicking the table and selecting Indexes/Keys...

通过右键单击表格并选择索引/键...

Click the 'Add' button, and change the columns to the column(s) you wish make unique.

单击“添加”按钮,然后将列更改为您希望使其唯一的列。

Change Is Unique to Yes.

变化是独一无二的。

Click close and save the diagram, and it will add it to the table.

单击关闭并保存图表,它将添加到表格中。

回答by Thunder3

You are looking for something like the following

您正在寻找类似以下内容的内容

ALTER TABLE dbo.doc_exz
ADD CONSTRAINT col_b_def
UNIQUE column_b

MSDN Docs

MSDN 文档

回答by Rafiq

To create a UNIQUE constraint on one or multiple columns when the table is already created, use the following SQL:

要在已创建表的情况下对一列或多列创建 UNIQUE 约束,请使用以下 SQL:

ALTER TABLE TableName ADd UNIQUE (ColumnName1,ColumnName2, ColumnName3, ...)

To allow naming of a UNIQUE constraint for above query

允许为上述查询命名 UNIQUE 约束

ALTER TABLE TableName ADD CONSTRAINT un_constaint_name UNIQUE (ColumnName1,ColumnName2, ColumnName3, ...)

The query supported by MySQL / SQL Server / Oracle / MS Access.

MySQL / SQL Server / Oracle / MS Access 支持的查询。

回答by Gibbons

In the management studio diagram choose the table, right click to add new column if desired, right-click on the column and choose "Check Constraints", there you can add one.

在管理工作室图表中选择表格,如果需要,右键单击以添加新列,右键单击该列并选择“检查约束”,您可以添加一个。

回答by Mario Vázquez

In some situations, it could be desirable to ensure the Unique key does not exists before create it. In such cases, the script below might help:

在某些情况下,可能需要在创建之前确保唯一键不存在。在这种情况下,下面的脚本可能会有所帮助:

IF Exists(SELECT * FROM sys.indexes WHERE name Like '<index_name>')
    ALTER TABLE dbo.<target_table_name> DROP CONSTRAINT <index_name> 
GO

ALTER TABLE dbo.<target_table_name> ADD CONSTRAINT <index_name> UNIQUE NONCLUSTERED (<col_1>, <col_2>, ..., <col_n>) 
GO