deskDBMSid name1 Frank Herbert2 Neil Gaiman3 Louis Sachar1CREATE TABLE books (2 id integer PRIMARY KEY,3 name text NOT NULL,4 author_id integer5 REFERENCES authors(id)6);id name author_id1 Dune 12 Coraline 23 Holes 34 Neverwhere 25 Dune Messiah 1392
Back row: the front desk, the DBMS and the authors tableFront: the CREATE TABLE statement and the books tableArcs: foreign keys, from a book's author_id to its author's idAt the back: the front desk (a program that adds books), the DBMS that checks every change, and the authors table. In front: the CREATE TABLE statement for books and the books table. id is the primary key of each table: unique and never missing (NULL). author_id in books is a foreign key: each value must be the id of a real row in authors. The DBMS refuses a second book with id 3 and a book whose author_id 9 names no author, and stores the row that keeps both rules. SQL does not promise any order for the rows of a table or of an answer unless the query says ORDER BY. The figure draws them in id order.

Tables, rows and columns

  1. A relational database keeps data in tables. This one, books, holds one kind of thing. Each row is one book. Each column is one fact that every book has: its id, its name and who wrote it.

Primary keys

Foreign keys

What happens

;; step 1/10 · table
table books
  id  name         author_id
  1   Dune         1
  2   Coraline     2
  3   Holes        3
  4   Neverwhere   2

4 rows    one per book
3 columns one per fact

textbook words for the same things:
  table  = relation
  row    = tuple
  column = attribute

SQL does not promise any order for the rows of a table or of an answer unless the query says ORDER BY. The figure draws them in id order. SQLite checks foreign keys only after PRAGMA foreign_keys = ON on each connection; without it, the book with author_id 9 would be stored. Its error messages are shorter than PostgreSQL’s. SQLite is also looser about types: an ordinary table stores a value of the wrong type, such as text in an integer column, unless the table is declared STRICT.