tags:

views:

44

answers:

4

i want to create unique constraints in table 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?

+2  A: 

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)
)
Gnomo
A: 
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...)
Vidar Nordnes
If you post code or XML, **please** highlight those lines in the text editor and click on the "code" button (101 010) on the editor toolbar to nicely format and syntax highlight it!
marc_s
A: 

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.

ktharsis
+4  A: 

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.

AlexKuznetsov
I tried up voting this eleven times, but all it did was toggle my vote on and off :( One vote for scripts instead of SSMS, ten votes for source control.
Shannon Severance
The two things are not mutually exclusive though. SSMS can be used to do things through a GUI and then the "Generate Change Script" option can be used rather than just saving the changes directly.
Martin Smith