Adding a unique identifier and PC to existing tables

I have several tables created on the mssql 2005 server. Due to my inexperience at that time, the tables were created without a primary key or a unique identification column.

how can I add a column with a unique identifier (auto incrememnt?) to a table with existing data (about 600 thousand rows)?

I need this to be the primary key so that I can start using SQL Analysis Services.

Many thanks,

Adam

+6
sql database tsql sql-server-2005 ssis
source share
4 answers

In T-SQL:

ALTER TABLE table_name ADD id INTEGER IDENTITY PRIMARY KEY NOT NULL 

But, as Jacob says, this is also very easy to do in SSMS.

+7
source share

In T-SQL ...

 alter table TableName add ID int identity(1,1) primary key not null 
+5
source share

Just add the column through Sql Managemenent studio, set the value to int, auto-increment (1,1), not null and PK. When you save the table, the column will be automatically added and pre-populated with data.

+2
source share

Add a column named ID and specify the type UniqueIdentifier , make it invalid and put the default value as newid()

0
source share

All Articles