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)?
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.
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".
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.
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.
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]);
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With