SQL basics and CRUD with one table

PostgreSQL 19.0

If you exited psql in the previous lesson, reconnect from your terminal with the command for your operating system in the setup lesson. Inside psql, run \c learning_db. Then run each SQL block in order to create a users table and practice Create, Read, Update, and Delete on one sample row.

On this page

Create the users table

CREATE TABLE defines the table and its columns. The column types control the kinds of values each column accepts, while constraints set additional rules.

CREATE TABLE users (
  id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name varchar(100) NOT NULL,
  email text NOT NULL UNIQUE
);
integer / IDENTITY / PRIMARY KEY
id is a whole number. PostgreSQL generates it when inserting a row. The primary key makes each id unique and non-null.
SERIAL PRIMARY KEY
You may also see this shorthand in older examples. SERIAL uses a sequence to supply integer IDs. IDs generated by sequences can have gaps.
varchar(100) / NOT NULL
name is text of at most 100 characters, and a value is required.
text / UNIQUE
email is text without a declared length limit. NOT NULL requires a value and UNIQUE prevents duplicate emails.
numeric(10, 2) / boolean / date
Other common types include exact decimals such as prices, true/false flags, and dates. The courses table uses numeric(10, 2) for the course fee.

If the result differs If users already exists from another lesson, use a fresh practice database or choose a different table name consistently in all of the examples.

Create a row with INSERT

INSERT INTO users (name, email) selects the target table and two columns. VALUES supplies their values in that order. PostgreSQL generates id, and RETURNING shows the inserted row.

INSERT INTO users (name, email) VALUES ('Nyi', 'nyi@example.com')
RETURNING id, name, email;

What to expect One row is returned and psql reports INSERT 0 1. The final 1 is the inserted row count.

Find users and inspect its structure

In psql, \dt lists visible tables and \d users shows the columns, types, defaults, and constraints of users. Run the commands one line at a time.

\dt
\d users

Read rows with SELECT

SELECT names the columns to display. FROM names the table. WHERE limits the result to the row matching the sample email. To see every row and column in this small practice table, try SELECT * FROM users; separately.

SELECT id, name, email
FROM users
WHERE email = 'nyi@example.com';

Change a row with UPDATE

UPDATE chooses the table. SET assigns the new value. WHERE matches only the sample email. Reuse the SELECT above to confirm the target before running the update. RETURNING shows what was changed.

UPDATE users SET name = 'Nyi Nyi'
WHERE email = 'nyi@example.com'
RETURNING id, name, email;

What to expect The returned name is Nyi Nyi and psql reports UPDATE 1.

Remove a row with DELETE

DELETE FROM selects the table. WHERE limits the deletion to the sample email. Preview the target with SELECT before running this. Without WHERE, DELETE targets every row in the table.

DELETE FROM users
WHERE email = 'nyi@example.com'
RETURNING id, name, email;

What to expect psql reports DELETE 1. Running the sample SELECT again reports zero matching rows.

Try DROP TABLE in a transaction

Run this optional block with the practice users table. DROP TABLE removes the table structure and its rows. The ROLLBACK in this example restores the table. Run the example only after finishing the row-level CRUD above.

BEGIN;
DROP TABLE users;
ROLLBACK;

What to expect Run \dt users after ROLLBACK to see that the table is still there.

Official references