Create primary key in a table that already exists in sqlserver

4

I made the import of a txt file with six million rows into a SqlServer table.

Now I want to create a primary key in it, the problem is that no column guarantees integrity, so I want to create another column with auto increment.

How would I do this?

    
asked by anonymous 28.05.2015 / 04:49

1 answer

4

To generate the column simply do the following:

First create a column with auto increment:

alter table TABELA
add id [bigint] IDENTITY(1,1)

Then turn this column into a primary key.

ALTER TABLE TABELA
ADD CONSTRAINT pk_NOME PRIMARY KEY (id)
    
28.05.2015 / 05:06