PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Tutorial / PostgreSQL CREATE TABLE

PostgreSQL CREATE TABLE

 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. The TEMPORARY 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 each NULL value to be unique. Notice that SQL standard only allows one NULL value in the column that has the UNIQUE constraint.
  • PRIMARY KEY – this constraint is the combination of NOT NULL and UNIQUE constraints. You can define one column as PRIMARY 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 the product 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 accounttable:

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
);

postgresql create table example

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
);

postgresql create table - role table example

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
)

postgresql create tables many to many

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_idcolumn references to the user_idcolumn in the account table, we need to define a foreign key constraint for the user_idcolumn:

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_idcolumn references to the role_idcolumn in the role table, we also need to define a foreign key constraint for the role_idcolumn.

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.

Related Tutorials

  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Foreign Key
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
Previous Tutorial: PostgreSQL Data Types
Next Tutorial: PostgreSQL ALTER TABLE

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2016 by PostgreSQL Tutorial Website. All Rights Reserved.