0
Challenge · 16SQL II: Tables, Types & Constraints
Constraints: NOT NULL, DEFAULT, CHECK, UNIQUE
Schemaused in ~90% of apps
This is the concept your capstone sprint actually grades. When someone pastes a 250-character title into DevLog's compose form, is the error coming from a JavaScript if-statement that a bug, a bypassed form, or a raw API call can simply skip — or is it coming from the database itself, which nothing can talk its way around? A CHECK constraint is the difference between "we validate this" and "this cannot exist."
Core idea. Four constraint types, four different sentences enforced on every write, by Postgres, forever: NOT NULL says "this column must have a value"; DEFAULT says "use this value when none is given" — and only when none is given; CHECK says "this value (or combination of values) must satisfy a boolean expression you write"; UNIQUE says "no two rows may share this value, or this combination of values." None of the four are suggestions, and none of them live in your application code.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
Live demointeractive
Real Postgres · in your browseridle
This
posts table has a CHECK (char_length(title) <= 200). Run this exact insert of a 201-character title. Then edit the 201 down to 200 and run it again. What's different?Cmd/Ctrl + Enter
↑ play a little first, then dig in