Tables, rows and columns
A table in a database looks like a spreadsheet sheet: the same rows and columns. Here are five rows of our chain's menu, the menu_items table:
id | name | category | price | volume_ml
----+---------------+----------+--------+-----------
1 | Espresso | coffee | 140.00 | 40
4 | Latte | coffee | 220.00 | 350
8 | Green tea | tea | 150.00 | 400
10 | Croissant | food | 160.00 |
13 | Hot chocolate | | 240.00 | 300
(5 rows)The menu has 13 rows and seven columns in all. Two more columns, is_seasonal and added_on, didn't fit here.
Two rules set a database table apart from a sheet:
- each row describes exactly one thing;
- each column holds values of only one kind.
These rules are what make a database more reliable than a spreadsheet.
A row is one thing
A row of a table holds all the information about one thing. In the menu, one row is one item: the latte's name, category, price and volume sit in one row.
Every item has a number in the id column. It finds the row even if two items one day get the same name.
Rows in a table have no order. In a spreadsheet the latte always sits in the fourth row of the sheet. In a database the rows lie however suits the DBMS, and the DBMS decides the order they come back in. If the order matters, you ask for it in the query.
A column is one property
A column holds one property of all rows: price holds prices, name holds names. A column has a name and a type, the kind of values it can hold.
The menu's columns and their types:
| Column | Type | What it holds |
|---|---|---|
id | integer | the item's number, a whole number |
name | text | the name |
category | text | the category: coffee, tea or food |
price | numeric | the price in rubles, a number with kopecks |
volume_ml | integer | the volume in milliliters |
is_seasonal | boolean | whether the item is seasonal: t is yes, f is no |
added_on | date | the date the item was added to the menu |
The DBMS checks the type on every write. The word "two hundred" will not get into the price column, so the sum of prices never breaks because of text.
Empty cells
Look at the croissant and the hot chocolate at the start of the lesson. The croissant has no volume, and the hot chocolate has no category yet: those cells are empty. An empty value in SQL is called NULL, and it has rules of its own. A whole chapter covers them; for now it is enough to know that such a cell looks empty in a query result.
How a table is described in SQL
A table is created with the CREATE TABLE command, and rows are added with INSERT. Chapters 12 and 13 cover these commands in detail. For now it is enough to read them: they appear in quiz questions.
CREATE TABLE menu (
name text,
price numeric
);
INSERT INTO menu (name, price) VALUES ('Latte', 220);
INSERT INTO menu (name, price) VALUES ('Green tea', 150);Let's take the example apart:
CREATE TABLE menucreates the tablemenu.- The parentheses list its columns: each has a name first, then a type.
- Each
INSERTcommand adds one row: the first parentheses name the columns, the second hold their values in the same order. - The column list may be left out:
INSERT INTO menu VALUES ('Latte', 220). Then the values go in the order the columns are described inCREATE TABLE. - Text is written in single quotes,
'Latte', and numbers without quotes.
The result is a table of two rows and two columns.
Common mistake
In a spreadsheet you can type anything into any cell. Beginners bring this habit to the database and put text into a numeric column:
INSERT INTO menu (name, price) VALUES ('Mocha', 'two hundred'); -- ✗ERROR: invalid input syntax for type numeric: "two hundred"The DBMS refuses to add the row: the price column has the type numeric, and 'two hundred' is not a number. The table stays as it was: it has two rows. The price has to be written as a number: 200.
Key points
- A row holds all the information about one thing, a column holds one property of all rows.
- Every column has a name and a type, and the DBMS won't let a value of another type into it.
- Rows in a table have no order: you ask for one in the query.
- An empty cell in SQL is
NULL.