What the message tells you
You insert a row and SQL Server refuses it:
Msg 547, Level 16, State 1, Line 7
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Loan_Member_MemberId". The conflict occurred in database "Library", table "dbo.Member", column 'MemberId'.
The statement has been terminated.Msg 547 on an INSERT means the new row names a parent row that doesn’t exist. A foreign key checks every row that goes into the child table: its value has to match a row in the parent table, or the row is refused. That rule is referential integrity; this post is about why your row broke it. The message names three things:
- the constraint,
FK_Loan_Member_MemberId: which foreign key refused the row; - the table,
dbo.Member: the parent, where SQL Server looked for the value and didn’t find it. The table you inserted into isn’t in the message at all; - the column,
MemberId: the parent’s column the value had to match.
The value itself isn’t in the message. You find it in the statement, or with a query, as below.
The library in this post has four tables. The library management system template is the full design, with authors, staff, reservations and fines:
CREATE TABLE dbo.Member ( MemberId int IDENTITY(1, 1) NOT NULL CONSTRAINT PK_Member PRIMARY KEY, CardNumber varchar(12) NOT NULL CONSTRAINT AK_Member_CardNumber UNIQUE, FullName nvarchar(100) NOT NULL ); CREATE TABLE dbo.Book ( BookId int NOT NULL CONSTRAINT PK_Book PRIMARY KEY, Title nvarchar(200) NOT NULL ); CREATE TABLE dbo.BookCopy ( BookCopyId int NOT NULL CONSTRAINT PK_BookCopy PRIMARY KEY, BookId int NOT NULL, Barcode varchar(20) NOT NULL, CONSTRAINT FK_BookCopy_Book_BookId FOREIGN KEY (BookId) REFERENCES dbo.Book (BookId) ); CREATE TABLE dbo.Loan ( LoanId int NOT NULL CONSTRAINT PK_Loan PRIMARY KEY, BookCopyId int NOT NULL, MemberId int NOT NULL, LentOn date NOT NULL, CONSTRAINT FK_Loan_BookCopy_BookCopyId FOREIGN KEY (BookCopyId) REFERENCES dbo.BookCopy (BookCopyId), CONSTRAINT FK_Loan_Member_MemberId FOREIGN KEY (MemberId) REFERENCES dbo.Member (MemberId) );

These rows raised the message. The library has members 1 and 2, and the loan names member 7:
INSERT INTO dbo.Book (BookId, Title) VALUES (1, N'Dune'), (2, N'Emma'); INSERT INTO dbo.BookCopy (BookCopyId, BookId, Barcode) VALUES (10, 1, '30001'), (11, 1, '30002'), (20, 2, '30003'); INSERT INTO dbo.Member (CardNumber, FullName) VALUES ('C-1001', N'Ana Silva'), ('C-1002', N'Ben Okafor'); INSERT INTO dbo.Loan (LoanId, BookCopyId, MemberId, LentOn) VALUES (500, 10, 7, '2026-03-02');
“The statement has been terminated” refers to that one INSERT. Msg 547 ends the statement, and the statements after it in the same batch still run, as the next script shows.
Load parent tables first
The most common cause is order. A load script inserts loans for a new member before the member:
INSERT INTO dbo.Loan (LoanId, BookCopyId, MemberId, LentOn) VALUES (501, 11, 1, '2026-03-03'), (502, 20, 3, '2026-03-03'); INSERT INTO dbo.Member (CardNumber, FullName) VALUES ('C-1003', N'Cy Tran'); INSERT INTO dbo.Loan (LoanId, BookCopyId, MemberId, LentOn) VALUES (501, 11, 1, '2026-03-03'), (502, 20, 3, '2026-03-03');
The first INSERT fails with the same Msg 547, for member 3. Loan 501, for Ana, is fine, but it doesn’t go in either: one statement inserts all its rows or none. The member INSERT after it runs, Cy Tran becomes member 3, and the same two loans then go in.
The order follows the lines on the diagram. A table loads after every table it has a foreign key to: dbo.Book and dbo.Member first, then dbo.BookCopy, then dbo.Loan. Deletes run the other way round, unless ON DELETE CASCADE takes the children along.
A parent can also be in the table under another key than the one you typed. MemberId is an identity, so SQL Server picks the number. This script adds Dev Patel, first with Ana’s card number by mistake:
INSERT INTO dbo.Member (CardNumber, FullName) VALUES ('C-1001', N'Dev Patel'); INSERT INTO dbo.Member (CardNumber, FullName) VALUES ('C-1004', N'Dev Patel'); INSERT INTO dbo.Loan (LoanId, BookCopyId, MemberId, LentOn) VALUES (503, 10, 4, '2026-03-04');
Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'AK_Member_CardNumber'. Cannot insert duplicate key in object 'dbo.Member'. The duplicate key value is (C-1001).
The statement has been terminated.
Msg 547, Level 16, State 1, Line 5
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Loan_Member_MemberId". The conflict occurred in database "Library", table "dbo.Member", column 'MemberId'.
The statement has been terminated.The second INSERT adds Dev, and the script expects him to be member 4. He is member 5: the failed INSERT used up 4, and an identity doesn’t hand a burned number out again. So the loan names a member who doesn’t exist, while Dev sits in the table. Don’t type an identity value you expect; look the parent up by a value you know:
INSERT INTO dbo.Loan (LoanId, BookCopyId, MemberId, LentOn) SELECT 503, 10, m.MemberId, '2026-03-04' FROM dbo.Member AS m WHERE m.CardNumber = 'C-1004';
Loan 503 goes in for member 5. Right after the parent’s own INSERT, SCOPE_IDENTITY() returns the number it got.
A value that looks right but isn’t
Copies now go on shelves, and a shelf has a code people type:
CREATE TABLE dbo.Shelf ( ShelfCode varchar(10) NOT NULL CONSTRAINT PK_Shelf PRIMARY KEY, Room nvarchar(40) NOT NULL ); ALTER TABLE dbo.BookCopy ADD ShelfCode varchar(10) NULL, CONSTRAINT FK_BookCopy_Shelf_ShelfCode FOREIGN KEY (ShelfCode) REFERENCES dbo.Shelf (ShelfCode);

Five copies arrive from a spreadsheet, each with a code that reads as A-12 or B-03:
INSERT INTO dbo.Shelf (ShelfCode, Room) VALUES ('A-12', N'Fiction'), ('B-03', N'History'); INSERT INTO dbo.BookCopy (BookCopyId, BookId, Barcode, ShelfCode) VALUES (12, 1, '30004', 'A-12 '); INSERT INTO dbo.BookCopy (BookCopyId, BookId, Barcode, ShelfCode) VALUES (13, 1, '30005', 'a-12'); INSERT INTO dbo.BookCopy (BookCopyId, BookId, Barcode, ShelfCode) VALUES (14, 2, '30006', ' B-03'); INSERT INTO dbo.BookCopy (BookCopyId, BookId, Barcode, ShelfCode) VALUES (15, 2, '30007', 'B-03' + CHAR(9)); INSERT INTO dbo.BookCopy (BookCopyId, BookId, Barcode, ShelfCode) VALUES (16, 2, '30008', 'B-03' + CHAR(160));
Msg 547, Level 16, State 1, Line 9
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_BookCopy_Shelf_ShelfCode". The conflict occurred in database "Library", table "dbo.Shelf", column 'ShelfCode'.
The statement has been terminated.
Msg 547, Level 16, State 1, Line 12
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_BookCopy_Shelf_ShelfCode". The conflict occurred in database "Library", table "dbo.Shelf", column 'ShelfCode'.
The statement has been terminated.
Msg 547, Level 16, State 1, Line 15
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_BookCopy_Shelf_ShelfCode". The conflict occurred in database "Library", table "dbo.Shelf", column 'ShelfCode'.
The statement has been terminated.Two go in and three fail, and the three messages are identical. In this run:
'A-12 ', with a trailing space, goes in. SQL Server ignores trailing spaces when it compares strings, so it matches'A-12', but the space is stored:DATALENGTHis 5.'a-12'goes in, because the database’s collation,SQL_Latin1_General_CP1_CI_AS, ignores case. In a case-sensitive column it fails.' B-03', with a leading space, fails.'B-03'with a tab after it fails. Only spaces are ignored at the end.'B-03'with a non-breaking space,CHAR(160), the kind a spreadsheet or a web page copies along, fails too.
To see what is really in a value, look at its bytes: CAST(@code AS varbinary(10)) returns 0x422D303309 for the tab case, and the 09 at the end is the tab. Trim or clean the codes before they reach the table.
A type mismatch isn’t on the list, because it can’t exist on a foreign key. SQL Server refuses to create one whose child column has another type than the parent’s (Msg 1778), or another collation (Msg 1757), so both sides always compare the same way.
When the constraint points at the wrong table
Sometimes the message names a parent that makes no sense. This runs in a fresh database, LibraryWrongKey, on the four tables of the first script. Here is a mistake in a script that adds staff to loans. It adds the StaffId column, but its foreign key is written on MemberId:
CREATE TABLE dbo.Staff ( StaffId int NOT NULL CONSTRAINT PK_Staff PRIMARY KEY, FullName nvarchar(100) NOT NULL ); ALTER TABLE dbo.Loan ADD StaffId int NULL, CONSTRAINT FK_Loan_Staff_StaffId FOREIGN KEY (MemberId) REFERENCES dbo.Staff (StaffId);

INSERT INTO dbo.Book (BookId, Title) VALUES (1, N'Dune'); INSERT INTO dbo.BookCopy (BookCopyId, BookId, Barcode) VALUES (10, 1, '30001'), (11, 1, '30002'); INSERT INTO dbo.Member (CardNumber, FullName) VALUES ('C-1001', N'Ana Silva'), ('C-1002', N'Ben Okafor'); INSERT INTO dbo.Staff (StaffId, FullName) VALUES (1, N'Rosa Diaz'); INSERT INTO dbo.Loan (LoanId, BookCopyId, MemberId, StaffId, LentOn) VALUES (500, 10, 1, 1, '2026-03-02'); INSERT INTO dbo.Loan (LoanId, BookCopyId, MemberId, StaffId, LentOn) VALUES (501, 11, 2, 1, '2026-03-02');
Msg 547, Level 16, State 1, Line 11
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Loan_Staff_StaffId". The conflict occurred in database "LibraryWrongKey", table "dbo.Staff", column 'StaffId'.
The statement has been terminated.Ana’s loan goes in, because she is member 1 and Rosa Diaz is staff 1: the wrong key passes when the numbers happen to match. Ben is member 2, there is no staff 2, and his loan fails on a check against dbo.Staff. When the table in the message surprises you, list what each of the child’s foreign keys compares:
SELECT fk.name, COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ChildColumn, OBJECT_NAME(fc.referenced_object_id) AS ParentTable, COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ParentColumn FROM sys.foreign_keys AS fk JOIN sys.foreign_key_columns AS fc ON fc.constraint_object_id = fk.object_id WHERE fk.parent_object_id = OBJECT_ID(N'dbo.Loan');
It returns three keys, and FK_Loan_Staff_StaffId maps child column MemberId to Staff.StaffId. Drop the key and add it on the right column:
ALTER TABLE dbo.Loan DROP CONSTRAINT FK_Loan_Staff_StaffId; ALTER TABLE dbo.Loan ADD CONSTRAINT FK_Loan_Staff_StaffId FOREIGN KEY (StaffId) REFERENCES dbo.Staff (StaffId);

Ben’s loan then goes in.
Checking a staging table before the move
Back in the first database, Library, a batch load often lands in a staging table first, with no foreign keys, so every row gets in:
CREATE TABLE dbo.LoanImport ( LoanId int NOT NULL, BookCopyId int NOT NULL, MemberId int NOT NULL, LentOn date NOT NULL );

Three loans arrive, and moving them into dbo.Loan fails:
INSERT INTO dbo.LoanImport (LoanId, BookCopyId, MemberId, LentOn) VALUES (510, 12, 2, '2026-03-05'), (511, 13, 9, '2026-03-05'), (512, 30, 3, '2026-03-05'); INSERT INTO dbo.Loan (LoanId, BookCopyId, MemberId, LentOn) SELECT LoanId, BookCopyId, MemberId, LentOn FROM dbo.LoanImport;
Msg 547, Level 16, State 1, Line 6
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Loan_Member_MemberId". The conflict occurred in database "Library", table "dbo.Member", column 'MemberId'.
The statement has been terminated.No loan goes in, and the message names only the member key, although loan 512 names copy 30, which doesn’t exist either. One query per foreign key finds the rows that have no parent:
SELECT i.LoanId, i.MemberId AS MissingMemberId FROM dbo.LoanImport AS i WHERE NOT EXISTS ( SELECT 1 FROM dbo.Member AS m WHERE m.MemberId = i.MemberId ); SELECT i.LoanId, i.BookCopyId AS MissingBookCopyId FROM dbo.LoanImport AS i WHERE NOT EXISTS ( SELECT 1 FROM dbo.BookCopy AS c WHERE c.BookCopyId = i.BookCopyId );
The first returns loan 511 with member 9, the second loan 512 with copy 30. Now you can decide per row: add the missing member and copy, correct the numbers, or load the rows that match and keep the rest for someone to check:
INSERT INTO dbo.Loan (LoanId, BookCopyId, MemberId, LentOn) SELECT i.LoanId, i.BookCopyId, i.MemberId, i.LentOn FROM dbo.LoanImport AS i WHERE EXISTS (SELECT 1 FROM dbo.Member AS m WHERE m.MemberId = i.MemberId) AND EXISTS (SELECT 1 FROM dbo.BookCopy AS c WHERE c.BookCopyId = i.BookCopyId);
That inserts loan 510 alone.
The fixes, by cause:
- Insert the missing parent, or load the parent tables first.
- Correct the value: look the key up by a value you know, and clean typed codes.
- Correct a foreign key that compares the wrong columns.
- Use NULL only where the relationship is optional. A copy with a NULL
ShelfCodegoes in with no shelf, because a NULL isn’t checked. Zero or an empty string isn’t NULL: a copy withShelfCode''fails with the same Msg 547, and so, in LibraryWrongKey after the fix, does a loan withStaffId0 from an application that sends 0 for “no staff”.
Switching the key off to get the rows in moves the error to later. After NOCHECK CONSTRAINT, member 9’s loan goes in, and turning the key back on WITH CHECK fails with “The ALTER TABLE statement conflicted with the FOREIGN KEY constraint”, the same Msg 547, leaving the key disabled. Finding and fixing those orphans is in the referential integrity post linked above.
How ERDsmith helps
Msg 547 on INSERT is about rows, and ERDsmith holds no rows, so it can’t tell you that member 9 is missing. It keeps the keys right at design time, which leaves the data as the only thing to look at when the message comes:
- A relationship is drawn between two tables, and the child’s foreign key columns come from it: one per column of the parent’s key, in key order, named by the foreign key template or a role name. A key from
dbo.Staffgivesdbo.Loanits ownStaffId; there is no step where it can land onMemberId. - The key from
dbo.Loantodbo.Memberpoints atPK_Member, or at another unique index you pick, such as the one onCardNumber: never at columns the parent doesn’t keep unique. - A child column takes its parent column’s type, length, precision and scale and keeps them, so a foreign key SQL Server refuses with Msg 1778 or Msg 1753 is never built. A collation that differs from the parent’s is drawn and marked as an error, the mismatch SQL Server refuses with Msg 1757.
- The lines show which tables are parents, so the load order of the second section reads off the diagram.