sql server how to assign a primary key to a table containing data

0

I have a table that is or is called Invoices which at the time of creation was created without a PK this table already has several records and now I want to add a column with a PK because the records I need from this table I want to make it a unique record.

    
asked by Daniel Soto 11.01.2018 в 21:13
source

1 answer

0

Before adding the Pk you need to add the field "id" that you want to use in your case you can not call it invoiceId and with the instruction IDENTITY we will make the field be auto incremental, as follows:

 alter table Facturas add IdFacturas int NOT NULL IDENTITY(1, 1);

Then you have to add the PK

 alter table Facturas add primary key (IdFacturas);

Search in google the following alter table primary key sql server

and voilà add-primary-key-to-existing-table

PS: you should put your table as additional information so that others understand you, in this case it is easier because what you need is something known.

    
answered by 11.01.2018 / 21:21
source