sql >> Base de Datos >  >> RDS >> Sqlserver

SQL Server agrega clave principal de incremento automático a la tabla existente

No, debe hacerlo al revés:agréguelo desde el principio como INT IDENTITY - se llenará con valores de identidad cuando hagas esto:

ALTER TABLE dbo.YourTable
   ADD ID INT IDENTITY

y luego puede convertirla en la clave principal:

ALTER TABLE dbo.YourTable
   ADD CONSTRAINT PK_YourTable
   PRIMARY KEY(ID)

o si prefieres hacerlo todo en un solo paso:

ALTER TABLE dbo.YourTable
   ADD ID INT IDENTITY
       CONSTRAINT PK_YourTable PRIMARY KEY CLUSTERED