SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Adding Primary key column to existing table


Adding Primary key column to existing table

Author
Message
phareesh
phareesh
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 18
I have a table with 10000 records. The table does not have any primary key constraint.
Now i want to add a primary column.How can i add a column with primary key constraint to the existing table? Can somebody provide the query to alter the column
jmuldoon
jmuldoon
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 50
Hi.

It would go something like this.

Step 1
First, add your new column.

Alter table TableName add NewCol int default(0) not null

I think that you must have a not nullable column. If I remember correctly, MSSql Server doesn;t like making primary keys from nullable columns.
You can accomodate this in an alter table by adding a default constraint then declare it as not null.

Step 2. Load the values into the new column. since the table had 1000 rows in it, now those are all set to the value of the default constraint. Since you are ablout to make this column a primary key, there must be unique values for each row in the new column.


Step 3. Create the primary key constraint

alter table TableName add constraint New_primary_key primary key (NewCol)

Optionally, you could remove the default constraint on the new column.
Francisco Lopez
Francisco Lopez
Mr or Mrs. 500
Mr or Mrs. 500 (507 reputation)Mr or Mrs. 500 (507 reputation)Mr or Mrs. 500 (507 reputation)Mr or Mrs. 500 (507 reputation)Mr or Mrs. 500 (507 reputation)Mr or Mrs. 500 (507 reputation)Mr or Mrs. 500 (507 reputation)Mr or Mrs. 500 (507 reputation)

Group: General Forum Members
Points: 507 Visits: 45
Using SQL Server 10.0.2531 Management Studio, I was able to skip the updating values part.

So,
1. Design table, Add [table].[Id] column as Identity seed 1 increment 1, save.
2. Right click [Id] column, set as primary key, save. Done.

SQL server added the incrementing values automatically! Awesome product.


{Francisco}
CoolCodeShare
CoolCodeShare
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1298 Visits: 311
Pls refer this link:

http://msdn.microsoft.com/en-us/library/ms190273.aspx
CoolCodeShare
CoolCodeShare
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1298 Visits: 311
You may use code like this:

 
ALTER TABLE [TableName] WITH NOCHECK
ADD CONSTRAINT [PrimaryKeyName] PRIMARY KEY CLUSTERED ([ExistingColumn])
WITH (FILLFACTOR = 75, ONLINE = ON, PAD_INDEX = ON);


jemish.dave
jemish.dave
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 53
Go to Sql Server Mgmt Studio.
Navigate to the table.
Right click --> Edit/Modify Table
DO NOT SAVE AFTER DOING BELOW CHANGES
Add New Column.
[ select identity,primary key, foreign key, etc....]
Right Click newly created column --> select Generate Change Script
Done.
A new dialogue box will appear goving you the script for all the changes done above.
Copy the script and use as required.
Verify the script provided by Microsoft and do necessary changes.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search