Summary: in this tutorial, you will learn how to use the PostgreSQL CREATE TABLE statement to create new tables.
PostgreSQL CREATE TABLE syntax
To create a new table in PostgreSQL, you use the CREATE TABLE
statement. The following illustrates the syntax of the CREATE TABLE
statement:
1 2 3 4 | CREATE TABLE table_name ( column_name TYPE column_constraint, table_constraint table_constraint ) INHERITS existing_table_name; |
Let’s examine the syntax of the CREATE TABLE
statement in more detail.
- First, you specify the name of the new table after the
CREATE TABLE
clause. TheTEMPORARY
keyword is for creating a temporary table, which we will discuss in the temporary table tutorial. - Next, you list the column name, its data type, and column constraint. You can have multiple columns in a table, each column is separated by a comma (,). The column constraint defines the rules for the column e.g., NOT NULL.
- Then, after the column list, you define a table-level constraint that defines rules for the data in the table.
- After that, you specify an existing table from which the new table inherits. It means the new table contains all columns of the existing table and the columns defined in the
CREATE TABLE
statement. This is a PostgreSQL’s extension to SQL.
PostgreSQL column constraints
The following are the commonly used column constraints in PostgreSQL:
- NOT NULL – the value of the column cannot be
NULL
. - UNIQUE – the value of the column must be unique across the whole table. However, the column can have many
NULL
values because PostgreSQL treats eachNULL
value to be unique. Notice that SQL standard only allows oneNULL
value in the column that has theUNIQUE
constraint. - PRIMARY KEY – this constraint is the combination of
NOT NULL
andUNIQUE
constraints. You can define one column asPRIMARY KEY
by using column-level constraint. In case the primary key contains multiple columns, you must use the table-level constraint. - CHECK – enables to check a condition when you insert or update data. For example, the values in the
price
column of theproduct
table must be positive values. - REFERENCES – constrains the value of the column that exists in a column in another table. You use
REFERENCES
to define the foreign key constraint.
PostgreSQL table constraints
The table constraints are similar to column constraints except that they are applied to the entire table rather than to an individual column.
The following are the table constraints:
UNIQUE (column_list)
– to force the value stored in the columns listed inside the parentheses to be unique.PRIMARY KEY(column_list)
– to define the primary key that consists of multiple columns.CHECK (condition)
– to check a condition when inserting or updating data.REFERENCES
– to constrain the value stored in the column that must exist in a column in another table.
PostgreSQL CREATE TABLE example
We will create a new table named account
that has the following columns with the corresponding constraints:
- user_id – primary key
- username – unique and not null
- password – not null
- email – unique and not null
- created_on – not null
- last_login – null
The following statement creates the account
table:
1 2 3 4 5 6 7 8 | CREATE TABLE account( user_id serial PRIMARY KEY, username VARCHAR (50) UNIQUE NOT NULL, password VARCHAR (50) NOT NULL, email VARCHAR (355) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL, last_login TIMESTAMP ); |
The following statement creates role
table that consists of two columns: role_id
and role_name
:
1 2 3 4 | CREATE TABLE role( role_id serial PRIMARY KEY, role_name VARCHAR (255) UNIQUE NOT NULL ); |
The following statement creates the account_roles
table that has three columns: user_id
, role_id
and grant_date
.
1 2 3 4 5 6 7 8 9 10 11 12 13 | CREATE TABLE account_role ( user_id integer NOT NULL, role_id integer NOT NULL, grant_date timestamp without time zone, PRIMARY KEY (user_id, role_id), CONSTRAINT account_role_role_id_fkey FOREIGN KEY (role_id) REFERENCES role (role_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT account_role_user_id_fkey FOREIGN KEY (user_id) REFERENCES account (user_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) |
Let’s examine the above statement in more detail.
The primary key of the account_role
table consists of two columns: user_id
and role_id
, therefore we have to use primary key table-level constraint to define the primary key as follows:
1 | PRIMARY KEY (user_id, role_id) |
Because the user_id
column references to the user_id
column in the account
table, we need to define a foreign key constraint for the user_id
column:
1 2 3 | CONSTRAINT account_role_user_id_fkey FOREIGN KEY (user_id) REFERENCES account (user_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION |
The role_id
column references to the role_id
column in the role
table, we also need to define a foreign key constraint for the role_id
column.
1 2 3 | CONSTRAINT account_role_role_id_fkey FOREIGN KEY (role_id) REFERENCES role (role_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, |
In this tutorial, we have shown you how to use the PostgreSQL CREATE TABLE statement to create new tables. We also showed you how to apply different kinds of constraints at column and table level.