Mar 2, 2007 #1 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?
Mar 2, 2007 #2 gmmastros Programmer Feb 15, 2005 14,901 US 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 Upvote 0 Downvote
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