DDL Commands For Table
CREATE TABLEโ
the CREATE TABLE statement is used to define a new table. It allows specifying column names, data types, constraints, and default values.
Syntax:
CREATE TABLE table_name (
column_name data_type constraints,
...
);
Example:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INTEGER CHECK (age > 18),
department TEXT,
salary NUMERIC(10,2) DEFAULT 0.00,
hired_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
- Column Definitions โ Each column has a name, data type, and optional constraints.
- Constraints:
PRIMARY KEYโ Ensures uniqueness.NOT NULLโ PreventsNULLvalues.CHECKโ Adds validation conditions.DEFAULTโ Assigns a default value.FOREIGN KEYโ Establishes relationships between tables.
TRUNCATE TABLEโ
The TRUNCATE command is used to quickly remove all rows from a table. It is faster than the DELETE command because it does not generate individual row delete actions.
Syntax:
TRUNCATE [TABLE] table_name [, ...] [RESTART IDENTITY] [CASCADE | RESTRICT];
RESTART IDENTITYโ Resets the sequence associated with the tableโs columns.CASCADEโ Automatically truncates all tables that have foreign-key references to the truncated table.RESTRICT (default)โ Refuses to truncate if any tables have foreign-key references to the truncated table.
- RESTRICT (default)
- RESTART IDENTITY
- CASCADE
TRUNCATE TABLE my_table; // one or more tables
๐น Removes all rows from my_table.
Example: Basic PostgreSQL TRUNCATE TABLE statement example
First, create a new table called products:
CREATE TABLE products(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL DEFAULT 0
);
Second, insert some rows into the products table:
INSERT INTO products (name, price)
VALUES
('A', 19.99),
('B', 29.99),
('C', 39.99),
('D', 49.99)
RETURNING *;
Output:
id | name | price
----+------+-------
1 | A | 19.99
2 | B | 29.99
3 | C | 39.99
4 | D | 49.99
(4 rows)
Third, delete all data from the products table using the TRUNCATE TABLE statement:
TRUNCATE TABLE products;
Output:
TRUNCATE TABLE
DROP TABLEโ
The DROP TABLE statement is used to remove an existing table from the database. This operation deletes the table structure along with all the data stored in it.
Syntax:
DROP TABLE [IF EXISTS] table_name [, ...] [CASCADE | RESTRICT];
IF EXISTSโ Prevents an error from being thrown if the table does not exist.CASCADEโ Automatically drops objects that depend on the table (such as views).RESTRICT (default)โ Refuses to drop the table if any objects depend on it.
- RESTRICT (default)
- CASCADE
- IF EXISTS
DROP TABLE my_table;
๐น Removes my_table if no other objects depend on it.
Example: Basic PostgreSQL DROP TABLE statement example
First, create a new table called customers:
CREATE TABLE customers(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL
);
Second, insert some rows into the customers table:
INSERT INTO customers (name, email)
VALUES
('Alice', 'alice@example.com'),
('Bob', 'bob@example.com');
Output:
id | name | email
----+-------+--------------------
1 | Alice | alice@example.com
2 | Bob | bob@example.com
(2 rows)
Third, drop the customers table:
DROP TABLE customers;
Output:
DROP TABLE
ALTER TABLEโ
The ALTER TABLE command is used to modify an existing tableโs structure.
- Add a column
- Drop a column
- Change the data type of a column
- Rename a column
- Set a default value for a column
- Rename a table
Syntax:
ALTER TABLE table_name action;
Add, Drop, Rename Columnโ
- Add Column
- Drop Column
- Rename Column
ALTER TABLE table_name ADD COLUMN new_column_name column_type;
๐น Adds a new column to the table.
Example: Adding a new column to the employees table
First, create the employees table:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department TEXT
);
Second, add a new column birthdate:
ALTER TABLE employees ADD COLUMN birthdate DATE;
Before:
id | name | department
----+------+------------
1 | John | Sales
2 | Jane | HR
After:
id | name | department | birthdate
----+------+------------+-----------
1 | John | Sales |
2 | Jane | HR |
ALTER TABLE table_name DROP COLUMN column_name;
๐น Removes a column from the table.
Example: Dropping a column from the employees table
First, create the employees table with the birthdate column:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department TEXT,
birthdate DATE
);
Second, drop the birthdate column:
ALTER TABLE employees DROP COLUMN birthdate;
Before:
id | name | department | birthdate
----+------+------------+-----------
1 | John | Sales | 1980-01-01
2 | Jane | HR | 1990-02-02
After:
id | name | department
----+------+------------
1 | John | Sales
2 | Jane | HR
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
๐น Renames a column in the table.
Example: Renaming a column in the employees table
First, create the employees table:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
birthdate DATE
);
Second, rename the birthdate column to date_of_birth:
ALTER TABLE employees RENAME COLUMN birthdate TO date_of_birth;
Before:
id | name | birthdate
----+------+-----------
1 | John | 1980-01-01
2 | Jane | 1990-02-02
After:
id | name | date_of_birth
----+------+--------------
1 | John | 1980-01-01
2 | Jane | 1990-02-02
Change Data Typeโ
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type;
๐น Changes the data type of an existing column.
Example: Changing the data type of a column in the employees table
First, create the employees table:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
salary INTEGER
);
Second, change the data type of the salary column:
ALTER TABLE employees ALTER COLUMN salary TYPE NUMERIC(10, 2);
Before:
id | name | salary
----+------+--------
1 | John | 50000
2 | Jane | 60000
After:
id | name | salary
----+------+---------
1 | John | 50000.00
2 | Jane | 60000.00
Set Default Valueโ
ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT default_value;
๐น Sets a default value for a column.
Example: Setting a default value for a column in the employees table
First, create the employees table:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
status VARCHAR(20)
);
Second, set a default value for the status column:
ALTER TABLE employees ALTER COLUMN status SET DEFAULT 'active';
Before:
id | name | status
----+------+--------
1 | John |
2 | Jane |
After:
id | name | status
----+------+--------
1 | John | active
2 | Jane | active
Rename Tableโ
ALTER TABLE old_table_name RENAME TO new_table_name;
๐น Renames the table.
Example: Renaming the employees table
First, create the employees table:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department TEXT
);
Second, rename the employees table to staff:
ALTER TABLE employees RENAME TO staff;
Before:
id | name | department
----+------+------------
1 | John | Sales
2 | Jane | HR
After:
id | name | department
----+------+------------
1 | John | Sales
2 | Jane | HR