Hello people,
Good Morning!
Introduction
In this post I will demonstrate to you how to identify, delete and recreate Foreign Keys (FK) from a table in SQL Server. We often need to delete a table or just change a column, but the bank sends us an error message stating that there is a foreign key constraint for that column, making it impossible for the DBA to make the requested change.
Msg 5074, Level 16, State 1, Line 57
The object 'PK__Clientes__3214EC07F3BD01EC' is dependent on column 'Id'.
Msg 5074, Level 16, State 1, Line 57
The object 'FK_Cliente_Telephone' is dependent on column 'Id'.
Msg 5074, Level 16, State 1, Line 57
The object 'FK_Cliente_Endereco' is dependent on column 'Id'.
Msg 4922, Level 16, State 9, Line 57
ALTER TABLE ALTER COLUMN Id failed because one or more objects access this column.
To resolve this issue, you must remove the FK contraints, make the change to the table, and then create again. When the table has many references, this task becomes a little laborious, especially if multiple tables are involved.
To facilitate this activity, which is very common on a daily basis, I decided to create and make available to you, a Stored Procedure that checks the FK's in the SQL Server catalog views and brings the complete listing.
Important: For technical reasons, SQL Server allows you to create Foreign Keys only between objects in the same database.
Creating the test mass
To demonstrate the use of this procedure, I created this simple script so that you generate a mass of tests and can test the SP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
IF (OBJECT_ID('dbo.Clientes') IS NOT NULL) DROP TABLE dbo.Clientes CREATE TABLE dbo.Clientes ( Id INT IDENTITY(1,1) PRIMARY KEY, Nome VARCHAR(50) ) INSERT INTO dbo.Clientes SELECT 'Dirceu' UNION ALL SELECT 'Patricia' UNION ALL SELECT 'Lucas' UNION ALL SELECT 'Leandro' UNION ALL SELECT 'Richardson' IF (OBJECT_ID('dbo.Enderecos') IS NOT NULL) DROP TABLE dbo.Enderecos CREATE TABLE dbo.Enderecos ( Id INT IDENTITY(1,1) PRIMARY KEY, Id_Cliente INT, Ds_Endereco VARCHAR(50) ) ALTER TABLE dbo.Enderecos ADD CONSTRAINT FK_Cliente_Endereco FOREIGN KEY (Id_Cliente) REFERENCES dbo.Clientes(Id) INSERT INTO dbo.Enderecos (Id_Cliente, Ds_Endereco) SELECT 1, 'Endereço Teste Cliente 1' UNION ALL SELECT 2, 'Endereço Teste Cliente 2' UNION ALL SELECT 3, 'Endereço Teste Cliente 3' IF (OBJECT_ID('tempdb..dbo.Telefones') IS NOT NULL) DROP TABLE dbo.Telefones CREATE TABLE dbo.Telefones ( Id INT IDENTITY(1,1) PRIMARY KEY, Id_Cliente INT, Nr_Telefone VARCHAR(11) ) ALTER TABLE dbo.Telefones ADD CONSTRAINT FK_Cliente_Telefone FOREIGN KEY (Id_Cliente) REFERENCES dbo.Clientes(Id) INSERT INTO dbo.Telefones (Id_Cliente, Nr_Telefone) SELECT 1, '27999998888' UNION ALL SELECT 4, '27999997777' UNION ALL SELECT 5, '27999996666' IF (OBJECT_ID('dbo.Teste1') IS NOT NULL) DROP TABLE dbo.Teste1 CREATE TABLE dbo.Teste1 ( Cod1 INT NOT NULL, Cod2 int NOT NULL, Cod3 int NOT NULL, Nome VARCHAR(200) ) IF (OBJECT_ID('dbo.Teste2') IS NOT NULL) DROP TABLE dbo.Teste2 CREATE TABLE dbo.Teste2 ( Codigo1 INT NOT NULL, Codigo2 int NOT NULL, Codigo3 int NOT NULL, Nome VARCHAR(200) ) ALTER TABLE dbo.Teste2 ADD CONSTRAINT [PK_Teste2] PRIMARY KEY (Codigo1, Codigo2, Codigo3) ALTER TABLE dbo.Teste1 ADD CONSTRAINT [FK_Teste1] FOREIGN KEY (Cod1, Cod2, Cod3) REFERENCES dbo.Teste2(Codigo1, Codigo2, Codigo3) |
How to Remove FK's Referencing a Table
With the code below, you can create the SP mentioned in the topic, which allows easy viewing of the FK's that reference a certain table, and already informs the script for removing and creating that FK:
Now let's take a look at the result:
Remembering that when you are going to delete the FK's, copy the scripts BEFORE generating them again. Once the FK's are removed, you will not be able to regenerate these scripts using the SP.
How to Remove FK's that reference a table (composite FK)
With the procedure below, you can easily identify and remove / recreate the FK's of a given table, which have foreign key references for other objects, even if they use a composite key (more than one column forming the FK).
View sourceHow to Remove FK's from a Table
With the procedure below, you can easily identify and remove / recreate the FK's of a given table, which have foreign key references for other objects.
View sourceThat's it folks!
To the next!
FK script composed with problem executing that does not return query. Solved by editing and placing select to set to @query and not @cmd.
Thanks for the tip Nehemiah, I already made the correction in the script. Thanks 🙂
Very useful script especially for those who have to load data on a daily basis. Very grateful for this excellent post.