Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I make an existing column have a unique constraint

Installation

How can I make an existing column have a unique constraint

by  cDevX99  Posted    (Edited  )
1) Make sure that the column only conains unique values
lets assume we are working with a table called t5. it has two columns. id ( the primary key ) and name. To find out if the column contains any duplicate values make the following statements.

select name from t5 ;
select distinct name from t5 ;


IF the row counts are the same then the column contains unique values only.

2) Create a unique index as follows..

alter table t5 add unique ( name ) ;

If you want to give the index a name do it this way...

alter table t5 add unique namedex ( name )

3) Drink a beer

That's it!

* You may optionally drink beer between and during steps one and two.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top