PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the CREATE TABLE
command is used to create a new table in the database. Below is the basic syntax for creating a table:
CREATE TABLE table_name ( column1 data_type [constraints], column2 data_type [constraints], ... table_constraints );
Where:
table_name
is the name you want to give to the table.column
specifies the name of the column.data_type
specifies the type of data the column can hold (e.g., INTEGER
, TEXT
, DATE
, etc.).constraints
are the rules you want to apply to the data values (e.g., NOT NULL
, PRIMARY KEY
, etc.).Examples:
CREATE TABLE employees ( employee_id SERIAL PRIMARY KEY, first_name TEXT NOT NULL, last_name TEXT NOT NULL, birth_date DATE, hire_date DATE );
In the above table:
employee_id
is of type SERIAL
, which means PostgreSQL will automatically generate and increment this number for new rows. It's also the primary key for the table, ensuring uniqueness.first_name
and last_name
are text columns that cannot be left empty (NOT NULL
constraint).birth_date
and hire_date
are date columns.CREATE TABLE orders ( order_id SERIAL PRIMARY KEY, customer_id INTEGER, order_date DATE NOT NULL, amount DECIMAL(10, 2) NOT NULL CHECK (amount >= 0), FOREIGN KEY (customer_id) REFERENCES customers(customer_id) );
In this table:
order_id
is the primary key.customer_id
is a foreign key that references the customer_id
from the customers
table. This ensures that for every order, there is a corresponding customer in the customers
table.CHECK
constraint on the amount
column to ensure that the values are non-negative.Remember, the CREATE TABLE
command defines the table schema. You can then use INSERT
to add data to the table, UPDATE
to modify data, and SELECT
to retrieve data.
Defining columns and data types in CREATE TABLE in PostgreSQL:
CREATE TABLE your_table ( column1 datatype, column2 datatype, ... );
Creating primary keys and unique constraints in PostgreSQL tables:
CREATE TABLE your_table ( id SERIAL PRIMARY KEY, unique_column datatype UNIQUE, ... );
Setting default values for columns in CREATE TABLE in PostgreSQL:
CREATE TABLE your_table ( column1 datatype DEFAULT default_value, ... );
Creating foreign keys with REFERENCES in PostgreSQL:
CREATE TABLE table1 ( id SERIAL PRIMARY KEY ); CREATE TABLE table2 ( id SERIAL PRIMARY KEY, table1_id INTEGER REFERENCES table1(id) );
Defining CHECK constraints in PostgreSQL tables:
CREATE TABLE your_table ( column1 INTEGER CHECK (column1 > 0), ... );
Creating indexes with CREATE TABLE in PostgreSQL:
CREATE TABLE your_table ( column1 INTEGER, ... INDEX (column1) );
Setting column constraints like NOT NULL in PostgreSQL tables:
CREATE TABLE your_table ( column1 INTEGER NOT NULL, ... );
Inheriting from existing tables in PostgreSQL:
CREATE TABLE parent_table ( id SERIAL PRIMARY KEY, common_column INTEGER ); CREATE TABLE child_table ( child_column VARCHAR(255) ) INHERITS (parent_table);