本文介紹了如何制作“無重復"SQL Server 2008 中的列?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我的 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 中的列?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!