How to make quot;No Duplicatesquot; column in SQL Server 2008?(如何制作“无重复SQL Server 2008 中的列?)
问题描述
我的 SQL Server 数据库中有一个简单的表.该表包含两列:ID int, Name nvarchar(50).ID 列是我的表的主键.
I have a simple table in my SQL Server database. This table contains two columns: ID int, Name nvarchar(50). The ID column is the primary key for my table.
我希望Name"列是(No Duplicates)",就像在 Microsoft Access 中一样,但此列不是主列.我怎么能这样做?
I want the "Name" column to be "(No Duplicates)", like in Microsoft Access, But this column isn't the primary column. How could I do this?
推荐答案
添加 该列的唯一约束:
ALTER TABLE Foo ADD CONSTRAINT UQ_Name UNIQUE (Name)
<小时>
通过 SQL Management Studio UI 添加:
To add it through SQL Management Studio UI:
- 打开 SQL Server Management Studio.
- 展开要在其中创建约束的数据库的 Tables 文件夹.
- 右键单击要添加约束的表,然后单击设计.
- 在表设计器中,点击索引/键.
- 点击添加.
- 在类型下拉列表中选择唯一键.
- Open SQL Server Management Studio.
- Expand the Tables folder of the database where you wish to create the constraint.
- Right-click the table where you wish to add the constraint and click Design.
- In Table Designer, click on Indexes/Keys.
- Click Add.
- Choose Unique Key in the Type drop-down list.
要处理发生唯一约束违反的情况,请参阅错误2601.
To handle a situation where a unique constraint violation occurs, see for error 2601.
这篇关于如何制作“无重复"SQL Server 2008 中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何制作“无重复"SQL Server 2008 中的列?
基础教程推荐
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
