开发者

t-sql unique constraints over 7 columns by sql management studio

开发者 https://www.devze.com 2023-01-15 09:49 出处:网络
i want to create unique constraints in ta开发者_运维百科ble for 7 columns, so when someone want insert data in this table this colums together are unique. In oracle this is very simple, but here ....

i want to create unique constraints in ta开发者_运维百科ble for 7 columns, so when someone want insert data in this table this colums together are unique. In oracle this is very simple, but here ....

I can do this by sql code:

CREATE TABLE Example
(Col1 int NOT NULL,
Col2 int NOT NULL,
CONSTRAINT CK_Col1_Col2 UNIQUE NONCLUSTERED (Col1, Col2)
)

Anyone know how to make this in sql management studio?


You are much better off if you create tables and other objects via scripts than in SSMS. It is even better if you save those scripts in source control.


in sql server 2005 this works

CREATE TABLE Example
(Col1 int NOT NULL,
Col2 int NOT NULL,
Col3 int NOT NULL,
Col4 int NOT NULL,
Col5 int NOT NULL,
Col6 int NOT NULL,
Col7 int NOT NULL,
CONSTRAINT CK_Col1_7 UNIQUE NONCLUSTERED (Col1, Col2, Col3, Col4, Col5, Col6, Col7)
)


CREATE TABLE Example
(
  Col1 int NOT NULL,
  Col2 int NOT NULL,
  Col3 int NOT NULL,
  Col4 int NOT NULL,
  Col5 int NOT NULL,
  Col6 int NOT NULL,
  Col7 int NOT NULL
)

CREATE INDEX CREATE UNIQUE NONCLUSTERED INDEX UI_IndexName ON Table (Col1, Col2...)


In SSMS go into the table design screen (right click table - design). Go to Table Designer menu and choose Indexes/Keys. Click the add button. In the properties window choose your columns (all 7 of them) ignoring the ascending/descending option and hit OK. Change the type property to unique key (which should automatically change the unique property to true). Give it a name, and a description if you want and click close button. Then click the save button to save your changes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号