I have a table Employees
:
EmpId int not null
EmpName nvarchar
- ...
However this table is referenced by other tables via a foreign key, i.e. the EmpId
is a foreign key in other tables.
This code doesn't work :
USE MyDatabase
ALTER TABLE [MyDatabase].[EmployeesSchema].[Employees]
DROP CONSTRAINT PK_Employees
ALTER TABLE [MyDatabase].[EmployeesSchema].[Employees]
DROP COLUMN EmpId
ALTER TABLE [MyDatabase].[EmployeesSchema].[Employees]
ADD TempColumn INT IDENTITY(1,1)
ALTER TABLE [MyDatabase].[EmployeesSchema].[Employees]
ADD CONSTRAINT PK_Employees PRIMARY KEY (TempColumn)
GO
sp_RENAME '[MyDatabase].[EmployeesSchema].[Employees].TempColumn', 'EmpId' , 'COLUMN'
GO
It produces :
The constraint 'PK_Employees' is being referenced by table 'Clients', foreign key constraint 'Employees_Clients_FK'.
How can I change the EmpId
to auto-increment even though I have the foreign key constraints?