×

sql · level 2 · unit 6 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

refuses a value that another row already has.

Two members may share a name, but never an email address. Add UNIQUE after a column's type and the database checks every new row against all the others.

Add an email that is already there and the statement stops with UNIQUE constraint failed, naming the table and column. The rows already in the table stay as they were.

PRIMARY KEY is unique too, which is why ids never repeat. NULL is different: a UNIQUE column can hold many empty boxes, because nothing equals NULL.

CREATE TABLE members (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT UNIQUE
);
INSERT INTO members (name, email) VALUES ('Ava', 'ava@example.com'), ('Leo', 'leo@example.com');
SELECT * FROM members;

Put UNIQUE on anything people look rows up by, like an email address or a username.