← 返回课程: SQL and databases from scratch

Installing PostgreSQL and creating the shop

视频即将上线

To learn SQL you have to practice, and to practice you need a database engine where you can run queries. You have three paths. The first is to install PostgreSQL 17 on your computer from postgresql.org (there are installers for Windows, macOS and Linux). The second is to use Docker. The third is a free online tool that runs PostgreSQL in the browser, with nothing to install. Any of them works; choose the one that feels most comfortable and remember that everything you type is safe: it is a practice database.

An important idea before starting: the server is a program that runs in the background and stores the data; you, from psql or pgAdmin, are a client that sends it instructions and receives answers. That is why you first create a database inside the server and then enter it. A single server can contain many separate databases.

Option A: local installation

Download the installer, accept the default options and set a password for the postgres user: write it down, you will need it. The installer includes psql, the text console of the server, and pgAdmin, a graphical interface. With either one you can type SQL. If you prefer Docker, a single command starts a temporary server that is erased when you stop it.

sql
-- In a terminal (not SQL), choose ONE of these:
--   Docker:  docker run --name sqlcourse -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres:17
--            docker exec -it sqlcourse psql -U postgres
--   Local:   psql -U postgres

-- Inside psql, create and enter our practice database:
CREATE DATABASE shop;
\c shop

-- Useful psql commands (they start with a backslash, no semicolon):
-- \dt         list tables
-- \d orders   describe the orders table
-- \q          quit

Creating the tables and loading the data

Copy the full script below and paste it into psql (or into the query window of pgAdmin or the online tool). It creates the four tables and fills them with 8 customers, 10 products, 12 orders and 22 order lines. You do not need to understand every word yet: CREATE TABLE defines the structure and INSERT INTO adds rows. In week 6 we will study them in detail. Notice that some data is missing on purpose (Felipe's email, Hugo's city): it will help us understand NULL.

sql
CREATE TABLE customers (
  customer_id INTEGER PRIMARY KEY,
  name        TEXT NOT NULL,
  email       TEXT,
  city        TEXT,
  signup_date DATE NOT NULL
);

CREATE TABLE products (
  product_id INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  category   TEXT NOT NULL,
  price      NUMERIC(10,2) NOT NULL,
  stock      INTEGER NOT NULL
);

CREATE TABLE orders (
  order_id    INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
  order_date  DATE NOT NULL,
  status      TEXT NOT NULL
);

CREATE TABLE order_items (
  item_id    INTEGER PRIMARY KEY,
  order_id   INTEGER NOT NULL REFERENCES orders (order_id),
  product_id INTEGER NOT NULL REFERENCES products (product_id),
  quantity   INTEGER NOT NULL,
  unit_price NUMERIC(10,2) NOT NULL
);

INSERT INTO customers (customer_id, name, email, city, signup_date) VALUES
  (1, 'Ana Silva',    'ana@example.com',   'Madrid',    '2023-01-15'),
  (2, 'Bruno Costa',  'bruno@example.com', 'Lisbon',    '2023-02-20'),
  (3, 'Carla Gomez',  'carla@example.com', 'Madrid',    '2023-03-05'),
  (4, 'Diego Ruiz',   'diego@example.com', 'Barcelona', '2023-04-10'),
  (5, 'Elena Rossi',  'elena@example.com', 'Rome',      '2023-05-22'),
  (6, 'Felipe Souza', NULL,                'Sao Paulo', '2023-06-30'),
  (7, 'Gina Lopez',   'gina@example.com',  'Barcelona', '2023-07-18'),
  (8, 'Hugo Martin',  'hugo@example.com',  NULL,        '2023-08-01');

INSERT INTO products (product_id, name, category, price, stock) VALUES
  (1,  'Laptop 14',           'Computers',   899.00,  15),
  (2,  'Wireless Mouse',      'Accessories',  19.90, 120),
  (3,  'Mechanical Keyboard', 'Accessories',  59.00,  40),
  (4,  'USB-C Hub',           'Accessories',  34.50,   0),
  (5,  'Monitor 27',          'Computers',   249.00,  22),
  (6,  'Webcam HD',           'Accessories',  45.00,  30),
  (7,  'Desk Lamp',           'Home',         24.90,  55),
  (8,  'Office Chair',        'Home',        189.00,   8),
  (9,  'Notebook A5',         'Stationery',    3.50, 300),
  (10, 'Gel Pens Pack',       'Stationery',    6.90, 200);

INSERT INTO orders (order_id, customer_id, order_date, status) VALUES
  (1,  1, '2024-01-10', 'delivered'),
  (2,  1, '2024-02-14', 'delivered'),
  (3,  2, '2024-02-20', 'delivered'),
  (4,  3, '2024-03-03', 'shipped'),
  (5,  4, '2024-03-15', 'delivered'),
  (6,  2, '2024-04-02', 'cancelled'),
  (7,  5, '2024-04-18', 'delivered'),
  (8,  1, '2024-05-05', 'pending'),
  (9,  3, '2024-05-21', 'delivered'),
  (10, 7, '2024-06-11', 'shipped'),
  (11, 4, '2024-06-25', 'delivered'),
  (12, 5, '2024-07-09', 'pending');

INSERT INTO order_items (item_id, order_id, product_id, quantity, unit_price) VALUES
  (1,  1,  1,  1, 899.00),
  (2,  1,  2,  1,  19.90),
  (3,  2,  3,  1,  59.00),
  (4,  2,  9,  4,   3.50),
  (5,  3,  5,  1, 249.00),
  (6,  3,  2,  2,  19.90),
  (7,  4,  8,  1, 189.00),
  (8,  5,  1,  1, 899.00),
  (9,  5,  3,  1,  59.00),
  (10, 6,  7,  2,  24.90),
  (11, 7,  10, 5,   6.90),
  (12, 7,  9,  3,   3.50),
  (13, 8,  5,  2, 249.00),
  (14, 8,  4,  1,  34.50),
  (15, 9,  7,  1,  24.90),
  (16, 9,  2,  1,  19.90),
  (17, 10, 3,  1,  59.00),
  (18, 10, 2,  2,  19.90),
  (19, 11, 8,  1, 189.00),
  (20, 11, 9,  10,  3.50),
  (21, 12, 1,  1, 899.00),
  (22, 12, 10, 2,   6.90);

Checking that everything works

If the script ran without errors, count the rows of each table. You should get 8, 10, 12 and 22. If you see an error like relation already exists, you ran the script twice: drop the tables with DROP TABLE in reverse order (first order_items, then orders, products and customers) or create a new database and repeat. If the error says violates foreign key constraint, check that you did not skip any part of the script, because tables are loaded in a specific order: first the ones that do not depend on others.

sql
SELECT COUNT(*) AS customers FROM customers;
SELECT COUNT(*) AS products FROM products;
SELECT COUNT(*) AS orders FROM orders;
SELECT COUNT(*) AS order_items FROM order_items;

✎ 我的笔记

仅保存在此浏览器中。登录后可保存到账号。0/5000 字符

← 上一节下一节 →