Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force a column to be unique for an entire table in SQL Server 2008 R2?

Tags:

I have a table with a Description field. I want to ensure that no two rows have the same "Description," but I can't make Description into my identity column (my ID column is an int).

Is it safe to set Description as a second primary key (in addition to my ID which is already a primary key)?

like image 511
Giffyguy Avatar asked Dec 20 '10 01:12

Giffyguy


People also ask

How do I make a column unique in an existing table?

Syntax to add UNIQUE to an existing field. alter table yourTableName add UNIQUE(yourColumnName); Applying the above syntax in order to add UNIQUE to column 'name'. Now we cannot insert duplicate records into the table, since we have set the field to be unique.

How do I make a column value unique in SQL Server?

Expand the "General" tab. Make sure you have the column you want to make unique selected in the "columns" box. Change the "Type" box to "Unique Key". Click "Close".

How do you set a columns unique?

First we write ALTER TABLE, then we list the name of the table (in our example: product ), and next we add the clause ADD CONSTRAINT with the name of the unique constraint (in our example: UQ_product_name ). This is followed by the UNIQUE keyword with column/columns (in our example it is column: name ) in parentheses.

How do I make columns unique in SQL Server Management Studio?

Use SQL Server Management StudioOn the Table Designer menu, select Indexes/Keys. In the Indexes/Keys dialog box, select Add. In the grid under General, select Type and choose Unique Key from the drop-down list box to the right of the property, and then select Close.


2 Answers

There is no such thing as a 'secondary primary key'. There is one primary key per table.

Create a UNIQUE constraint on the Description column (highly unusual thing to do, BTW. For example, It is more usual to create a unique index on Product Name rather than a Product description) or if you have null values in the Description column create a Filtered index (SQL Server 2008 onwards)

ALTER TABLE dbo.yourTable    ADD CONSTRAINT UQ_yourTable_Description UNIQUE ([Description]); 
like image 110
Mitch Wheat Avatar answered Oct 02 '22 23:10

Mitch Wheat


Add a Unique index to the Description column.

Using Sql Server Management Studio right click on the table and choose Design. Then right click on a column and choose "Indexes/keys". You will be prompted with the following window

alt text

Click on Add on the bottom left and then specify properties for your index. If you want to use a DDL script then use something like this

CREATE UNIQUE NONCLUSTERED INDEX [IX_INDEXNAME] ON [dbo].[TABLENAME]  (     [Description] ASC ) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] GO 
like image 45
Lorenzo Avatar answered Oct 02 '22 21:10

Lorenzo