$ ./guess_the_languageSQL & Databases
SQL & Databases · hard · #41

What happens to the insert?

CREATE TABLE line (
  qty INT CHECK (qty > 0)
);
INSERT INTO line VALUES (NULL);
  1. It stores 1, the minimum
  2. It stores 0 in place of NULL
  3. It succeeds and stores NULL
  4. It fails the CHECK rule
A CHECK only rejects rows where the condition is false; NULL > 0 is unknown, so it passes. Add NOT NULL to forbid it.
Play SQL & Databases ▸