4. Going live, and the first bad data · Concepts

Concepts

  • INSERT INTO table (col, col) VALUES (val, val), (val, val); adds rows. Columns you leave out get their default, or NULL if there isn't one. Text goes in single quotes.
  • NULL means "no value". It is not 0 and not ''.
  • Constraints are rules the database enforces on every write, no matter which part of the app (or which person in psql) does the writing:
    • NOT NULL: the column must have a value.
    • UNIQUE: no two rows share this value.
    • DEFAULT value: used when an insert doesn't supply the column. DEFAULT now() stamps the current time.
    • CHECK (condition): the condition must be true for every row.
  • You can add all of these to existing tables with ALTER TABLE. Use \h ALTER TABLE to find the forms. Postgres will refuse to add a rule that existing rows already break, so you have to clean up the data first.