Skip to content
Microsoft SQL Server

SQL Server Error 547

Statement conflicted with a FOREIGN KEY or CHECK constraint

Applies to: All supported SQL Server versions (2016 to 2025), Azure SQL Database, Azure SQL Managed Instance
Last technically reviewed 23 October 2026 on SQL Server 2025 (17.0.1145.4)
The full message
Msg 547, Level 16, State 1, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Orders_Customers". The conflict occurred in database "AppDb", table "dbo.Customers", column 'id'.
Other forms of this message
DELETE of a parent that still has children (REFERENCE)
Msg 547, Level 16, State 1, Line 1
The DELETE statement conflicted with the REFERENCE constraint "FK_Orders_Customers". The conflict occurred in database "AppDb", table "dbo.Orders", column 'customer_id'.
Value breaks a CHECK constraint
Msg 547, Level 16, State 1, Line 1
The INSERT statement conflicted with the CHECK constraint "CK_Orders_Total". The conflict occurred in database "AppDb", table "dbo.Orders", column 'total'.
UPDATE of a child to a missing parent (FOREIGN KEY)
Msg 547, Level 16, State 1, Line 1
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Orders_Customers". The conflict occurred in database "AppDb", table "dbo.Customers", column 'id'.
UPDATE of a parent key that children use (REFERENCE)
Msg 547, Level 16, State 1, Line 1
The UPDATE statement conflicted with the REFERENCE constraint "FK_Orders_Customers". The conflict occurred in database "AppDb", table "dbo.Orders", column 'customer_id'.
Adding a foreign key over existing orphan rows
Msg 547, Level 16, State 1, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Orders_Customers". The conflict occurred in database "AppDb", table "dbo.Customers", column 'id'.
Followed by (INSERT, UPDATE, DELETE)
The statement has been terminated.
Error number
547
Severity
16 (user-correctable error)
State
1
Product
Microsoft SQL Server Database Engine
Versions
All supported SQL Server versions (2016 to 2025), Azure SQL Database, Azure SQL Managed Instance
Last reviewed
23 October 2026

What it means

Error 547 means a statement would break a FOREIGN KEY or CHECK constraint, so SQL Server rejected it: a child row points at a parent that does not exist, a parent still has children, or a value fails a rule.

Read the message in three parts. The first word is the statement that failed (INSERT, UPDATE, DELETE, MERGE or ALTER TABLE). Next comes the kind of constraint: FOREIGN KEY means the row you are writing points at a parent key that does not exist; REFERENCE means you are deleting or changing a parent key that child rows still use; CHECK means a value fails a rule on the table. Last, the database, table and column tell you where to look: for FOREIGN KEY it names the parent table, for REFERENCE the child table.

Severity 16 is a normal, catchable error. The statement is rolled back as a whole (no rows of a multi-row statement are kept), but an open transaction around it stays open unless XACT_ABORT is on. When the error comes from ALTER TABLE adding a constraint, it means rows that already exist break the new rule. See primary key vs foreign key for how the two sides relate.

Common causes

  • Child rows are inserted before their parent, for example orders loaded before customers, or an id typed or mapped wrongly.
  • A parent row is deleted, or its key changed, while child rows still reference it and the foreign key has no ON DELETE or ON UPDATE action.
  • A value breaks a CHECK rule: a negative amount, an end date before the start date, a status outside the allowed list.
  • A foreign key or CHECK constraint is added (or re-enabled WITH CHECK) on a table that already holds rows that break it.
  • Data is copied between environments where the parent tables hold different keys.

Diagnose it

Read-only These queries only read metadata and dynamic management views. They do not change data, permissions or configuration, so they are safe to run first.

See what the foreign key connects and what it does on delete

SELECT fk.name AS constraint_name,
       OBJECT_NAME(fk.parent_object_id) AS child_table, pc.name AS child_column,
       OBJECT_NAME(fk.referenced_object_id) AS parent_table, rc.name AS parent_column,
       fk.delete_referential_action_desc, fk.update_referential_action_desc,
       fk.is_disabled, fk.is_not_trusted
FROM sys.foreign_keys AS fk
JOIN sys.foreign_key_columns AS fkc ON fkc.constraint_object_id = fk.object_id
JOIN sys.columns AS pc ON pc.object_id = fkc.parent_object_id AND pc.column_id = fkc.parent_column_id
JOIN sys.columns AS rc ON rc.object_id = fkc.referenced_object_id AND rc.column_id = fkc.referenced_column_id
WHERE fk.name = N'FK_Orders_Customers';
What to look for

The child and parent columns, and NO_ACTION, CASCADE, SET_NULL or SET_DEFAULT for deletes and updates. NO_ACTION is why a DELETE of a parent is blocked.

FOREIGN KEY: does the parent key exist?

SELECT id, name
FROM dbo.Customers
WHERE id = 99;  -- the value you tried to store in Orders.customer_id
What to look for

No row means the parent is missing: insert it first or correct the value.

REFERENCE: which child rows still point at the parent?

SELECT id, customer_id, total
FROM dbo.Orders
WHERE customer_id = 1;  -- the parent key you tried to delete or change
What to look for

The rows that block the DELETE. Decide whether they should be deleted, moved to another parent, or whether the parent should stay.

CHECK: read the rule

SELECT name, definition, is_disabled, is_not_trusted
FROM sys.check_constraints
WHERE parent_object_id = OBJECT_ID(N'dbo.Orders');
What to look for

The exact expression each value must satisfy, for example ([total]>=(0)).

ALTER TABLE: find the orphan rows before adding the key

SELECT o.id, o.customer_id
FROM dbo.Orders AS o
WHERE o.customer_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM dbo.Customers AS c WHERE c.id = o.customer_id);
What to look for

Every row returned would make ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY fail with 547.

How to fix it

Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.

Write the parent first (FOREIGN KEY)

Changes data

Insert the parent row before the child, in the same transaction if they belong together, or point the child at a key that really exists. For loads, load reference tables first.

BEGIN TRANSACTION;
INSERT INTO dbo.Customers (id, name) VALUES (99, N'Sara');
INSERT INTO dbo.Orders (id, customer_id, total) VALUES (1, 99, 50.00);
COMMIT TRANSACTION;

Deal with the child rows before deleting the parent (REFERENCE)

Changes data

Move the children to another parent, or delete them first if they really should go with the parent. Do both steps in one transaction so nothing is left half done.

Caution: Deleting child rows removes real data (orders, payments, history). Check the rows found by the diagnostic with the data owner first; often the right answer is to keep the parent, or mark it inactive, rather than delete.

BEGIN TRANSACTION;
DELETE FROM dbo.Orders    WHERE customer_id = 1;
DELETE FROM dbo.Customers WHERE id = 1;
COMMIT TRANSACTION;

Send a value that passes the rule (CHECK)

Changes data

Validate input against the CHECK definition found above, in the application and in the procedure that writes the row. Change the rule itself only if the business rule has changed.

INSERT INTO dbo.Orders (id, customer_id, total) VALUES (2, 1, 5.00);  -- total >= 0

Make deletes cascade, if that is the intended behaviour

Changes schema

If child rows have no meaning without the parent (order lines without an order), recreate the foreign key with ON DELETE CASCADE. Several cascading paths to one table are refused with error 1785.

Caution: With ON DELETE CASCADE, one DELETE of a parent silently removes every child row. Do not use it where children are records you must keep.

ALTER TABLE dbo.OrderLines DROP CONSTRAINT FK_OrderLines_Orders;
ALTER TABLE dbo.OrderLines ADD CONSTRAINT FK_OrderLines_Orders
    FOREIGN KEY (order_id) REFERENCES dbo.Orders (id)
    ON DELETE CASCADE;

Fix orphan rows, then add the constraint WITH CHECK

Changes data

For the ALTER TABLE form, correct or remove the rows found by the orphan query (or create the missing parents), then add the key so SQL Server validates every row and trusts the constraint. Adding it WITH NOCHECK leaves bad rows in place and the constraint untrusted, so avoid that as a shortcut.

UPDATE o SET customer_id = NULL
FROM dbo.Orders AS o
WHERE NOT EXISTS (SELECT 1 FROM dbo.Customers AS c WHERE c.id = o.customer_id);

ALTER TABLE dbo.Orders WITH CHECK
    ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (customer_id) REFERENCES dbo.Customers (id);

Examples

FOREIGN KEY: inserting an order for a customer that does not exist

CREATE TABLE dbo.Customers (
    id   INT CONSTRAINT PK_Customers PRIMARY KEY,
    name NVARCHAR(100)
);
CREATE TABLE dbo.Orders (
    id          INT CONSTRAINT PK_Orders PRIMARY KEY,
    customer_id INT CONSTRAINT FK_Orders_Customers REFERENCES dbo.Customers (id),
    total       DECIMAL(10,2)
);

INSERT INTO dbo.Orders (id, customer_id, total) VALUES (1, 99, 50.00);  -- no customer 99
Result
Msg 547, Level 16, State 1, Line 11
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Orders_Customers". The conflict occurred in database "AppDb", table "dbo.Customers", column 'id'.
The statement has been terminated.

Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.

REFERENCE: deleting a customer who still has orders

CREATE TABLE dbo.Customers (
    id   INT CONSTRAINT PK_Customers PRIMARY KEY,
    name NVARCHAR(100)
);
CREATE TABLE dbo.Orders (
    id          INT CONSTRAINT PK_Orders PRIMARY KEY,
    customer_id INT CONSTRAINT FK_Orders_Customers REFERENCES dbo.Customers (id),
    total       DECIMAL(10,2)
);
INSERT INTO dbo.Customers (id, name) VALUES (1, N'Sara');
INSERT INTO dbo.Orders (id, customer_id, total) VALUES (1, 1, 50.00);

DELETE FROM dbo.Customers WHERE id = 1;
Result
Msg 547, Level 16, State 1, Line 13
The DELETE statement conflicted with the REFERENCE constraint "FK_Orders_Customers". The conflict occurred in database "AppDb", table "dbo.Orders", column 'customer_id'.
The statement has been terminated.

Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.

CHECK: a negative total

CREATE TABLE dbo.Orders (
    id    INT CONSTRAINT PK_Orders PRIMARY KEY,
    total DECIMAL(10,2) NOT NULL CONSTRAINT CK_Orders_Total CHECK (total >= 0)
);

INSERT INTO dbo.Orders (id, total) VALUES (1, -5.00);
Result
Msg 547, Level 16, State 1, Line 6
The INSERT statement conflicted with the CHECK constraint "CK_Orders_Total". The conflict occurred in database "AppDb", table "dbo.Orders", column 'total'.
The statement has been terminated.

Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.

How to prevent it

Load and insert in dependency order (parents, then children), and wrap a parent and its children in one transaction. Decide for each relationship whether deleting a parent should be blocked, cascade, or set the child key to NULL, and write that into the foreign key instead of leaving it to chance.

Validate values against CHECK rules before they reach the database, and run the orphan query before adding constraints to existing tables. Keep constraints enabled and trusted: they are what stop bad data, and the optimiser relies on trusted keys. See the constraints guide.

Common questions

Which table does error 547 point at?

For a FOREIGN KEY conflict the message names the parent table and its key column (the row that is missing). For a REFERENCE conflict it names the child table and its foreign key column (the rows still using the parent). For a CHECK conflict it names the table and column with the rule.

Should I disable the constraint to get my data in?

No. Disabling it or adding it WITH NOCHECK lets invalid rows in and leaves the constraint untrusted. Fix or remove the bad rows and keep the constraint checked.

Does error 547 roll back my transaction?

It rolls back the failing statement only. An explicit transaction stays open, so check @@TRANCOUNT or XACT_STATE() in your CATCH block, or use SET XACT_ABORT ON to roll back the whole transaction on any error.

Learn more on CodeWithSQL

Official documentation

Last technically reviewed 23 October 2026; examples and diagnostics run on SQL Server 2025 (17.0.1145.4). Message text checked against the engine's sys.messages catalogue.

Still stuck on a SQL Server error?

Browse the full SQL Server error library, or get hands-on help from an experienced SQL professional.