简体   繁体   English

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

[英]SQL Server 2005 How Create a Unique Constraint?

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以及如何在数据库图表中完成它。

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 给它一个合适的名字

In SQL Server Management Studio Express: 在SQL Server Management Studio Express中:

  • Right-click table, choose Modify or Design(For Later Versions) 右键单击表,选择“ 修改”或“ 设计”(对于以后的版本)
  • Right-click field, choose Indexes/Keys... 右键单击字段,选择“ 索引/键”...
  • Click Add 单击添加
  • For Columns , select the field name you want to be unique. 对于“ 列” ,请选择要唯一的字段名称
  • For Type , choose Unique Key . 对于Type ,选择Unique Key
  • Click Close , Save the table. 单击关闭保存表格。
ALTER TABLE [TableName] ADD CONSTRAINT  [constraintName] UNIQUE ([columns])

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 NULL for a range of answers. 请参阅字段值必须是唯一的,除非它对于一系列答案为NULL

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

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. 单击关闭并保存图表,然后将其添加到表中。

You are looking for something like the following 您正在寻找以下内容

ALTER TABLE dbo.doc_exz
ADD CONSTRAINT col_b_def
UNIQUE column_b

MSDN Docs MSDN文档

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支持的查询。

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

In some situations, it could be desirable to ensure the Unique key does not exists before create it. 在某些情况下,可能需要确保在创建Unique键之前不存在该唯一键。 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

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

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