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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make existing column primary key

Status
Not open for further replies.

dandot

Programmer
Jul 19, 2005
53
CA
I have an existing column that id like to make into a primary key. What is the code for that?

 
In order to make a column be the primary key, you must first make sure you do a couple things.

1. Primary Keys must be unique, so you'll need to make sure there are no duplicates in that column.

2. You need to make sure there are no nulls in the column.

3. You also must make sure that the database doesn't allow nulls.

To make the column not accept NULLS...

Alter Table [!]TableName[/!] Alter Column [!]ColumnName[/!] (datatype) NOT NULL

Of course, you'll need to change the data type to match the data type of the column.

Then....

Alter Table [!]TableName[/!] ADD CONSTRAINT [!]ContraintName[/!] Primary Key Clustered ([!]ColumnName[/!])




-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top