SQL Error Riddles: Parent Keys Not Found and Other Classics

User group friends have passed these SQL error riddles around for years. You ask a question about life, and the answer is a database error. I’ve cleaned them up a little, and for each one I’ll show the real SQL Server error behind the punchline.

A wooden mannequin hunts under sofa cushions while a small mannequin outside holds up the red car keys

Why SQL Error Riddles Stick

A riddle is easy to remember, and so is the error inside it. Once you have laughed at parent keys not found, you will never forget what a foreign key does. That is the best kind of learning: you did not notice it happening.

Parent Keys Not Found

What happens when a parent loses the car keys? Parent keys not found.

In SQL Server, this is Msg 547. A child row points to a parent row that does not exist, and the foreign key refuses it.

CREATE TABLE dbo.Customer
(
    CustomerId int PRIMARY KEY
);

CREATE TABLE dbo.CustomerOrder
(
    OrderId int PRIMARY KEY,
    CustomerId int NOT NULL
        CONSTRAINT FK_CustomerOrder_Customer REFERENCES dbo.Customer(CustomerId)
);

INSERT dbo.CustomerOrder (OrderId, CustomerId)
VALUES (1, 99);  -- Msg 547: there is no customer 99

The message says the INSERT statement conflicted with the FOREIGN KEY constraint and names the table and column it checked. Insert the parent first, and the child is welcome.

Duplicate Key at the Party

What happens when two guests arrive at a party in the same outfit? Duplicate key.

That is Msg 2627, a violation of a PRIMARY KEY or UNIQUE constraint. Modern versions of SQL Server even tell you the duplicate value, which is kinder than most party guests.

Too Big for the Hole

What happens when the golf ball is bigger than the hole? Arithmetic overflow.

SELECT CAST(3000000000 AS int);  -- Msg 8115: int stops at 2,147,483,647

The value simply does not fit the data type. Use bigint, or decimal with enough precision, when the numbers can grow.

No Invitation, No Entry

What happens when you try to join a club without an invitation? Permission denied.

That is Msg 229, which says the SELECT permission was denied on the object and names the database and schema. The fix is a GRANT from someone who has the right to give it, not a sysadmin login shared with the whole team.

Telling SQL Error Riddles at Work

Here is one more for the road. What happens when you text a friend and get no reply? Zero rows affected. Use SQL error riddles in training and in code reviews. People remember the laugh, and the error comes along with it.

Related reading on this blog: Find Untrusted Foreign Key and Understanding Grant, Deny, and Revoke Permissions.

Keep the riddles kind, and keep the real error message next to each punchline. That is where the learning lives.

An error riddle is not just a joke, it’s a lesson that sneaks in while people are laughing.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

Primary Key, SQL Constraint and Keys, SQL Error Messages, SQL Humor
Previous Post
SQL SERVER – DATE and TIME in SQL Server 2008
Next Post
SQLAuthority News – Guest Post – Performance Counters Gathering using Powershell

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.