問(wèn)題描述
我的 SQL Server 數(shù)據(jù)庫(kù)中有一個(gè)簡(jiǎn)單的表.該表包含兩列: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)
<小時(shí)>
通過(guò) SQL Management Studio UI 添加:
To add it through SQL Management Studio UI:
- 打開 SQL Server Management Studio.
- 展開要在其中創(chuàng)建約束的數(shù)據(jù)庫(kù)的 Tables 文件夾.
- 右鍵單擊要添加約束的表,然后單擊設(shè)計(jì).
- 在表設(shè)計(jì)器中,點(diǎn)擊索引/鍵.
- 點(diǎn)擊添加.
- 在類型下拉列表中選擇唯一鍵.
- 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.
要處理發(fā)生唯一約束違反的情況,請(qǐng)參閱錯(cuò)誤2601.
To handle a situation where a unique constraint violation occurs, see for error 2601.
這篇關(guān)于如何制作“無(wú)重復(fù)"SQL Server 2008 中的列?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!