Hands-on task
PostgreSQL: table with constraints
In the shop database, create a table products with columns: id — integer, PRIMARY KEY; name — text, NOT NULL; price — numeric.
- postgresql
- create table
- primary key
- not null
- data types
Hands-on task
In the shop database, create a table products with columns: id — integer, PRIMARY KEY; name — text, NOT NULL; price — numeric.
Constraints in PostgreSQL are declared right inside CREATE TABLE next to the column: PRIMARY KEY gives uniqueness plus a NULL ban and automatically creates a unique index, while NOT NULL merely forbids an empty value with no uniqueness requirement. Special attention to the choice of type for money: an exact decimal type (numeric/decimal) stores sums without rounding errors, whereas float/real is an approximate binary representation that makes fractions "drift." Mental model: column names and order come from the task, while each column's type is chosen to fit its meaning. It's convenient to view a table's structure with the \d command in psql.