PostgreSQL Table Operations - 1
PostgreSQL provides robust and flexible commands for table management. This guide covers DDL operations such as create, select into, create as, create sequence
CREATE TABLE
The CREATE TABLE statement is fundamental for structuring data within a database. It allows you to define a table’s name, columns, data types, and any constraints. By mastering this statement, you can ensure efficient and reliable database design. Basic syntax is :
CREATE TABLE table_name (
column_name TYPE column_constraint,
table_constraint table_constraint
) INHERITS existing_table_name;
Column-Level Constraints
These constraints are applied to individual columns:
Table-Level Constraints
These constraints define rules for the entire table:
Key points
SELECT INTO
The SELECT INTO statement is a SQL feature that creates a new table directly from the result set of a query. SELECT INTO does not return data to the client but saves it in a new table, allowing for streamlined data handling and improved query performance. Basic syntax is:
SELECT
column_list
INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table_name
FROM
table_name
WHERE
condition;
TEMP (Temporary)
UNLOGGED
We can use additional clauses GROUP BY, JOINS, HAVING along with WHERE to filter the data
Key points
CREATE TABLE AS
CREATE TABLE AS statement is used to create a new table and populate it with data returned by a query. The new table does not inherit indexes or constraints from the source table. These need to be added manually if required. Basic syntax is:
CREATE TABLE new_table_name AS query;
Recommended by LinkedIn
Temporary Tables
Temporary tables are session-specific and automatically dropped at the end of the session or transaction. Ideal for transient data that does not need to persist. Basic syntax is:
CREATE TEMP TABLE new_table_name AS query;
Unlogged Tables
Unlogged tables improve performance like faster data insertion and querying due to no WAL (Write ahead logging) writes. However, they are not crash-safe, and data is lost during crashes. Basic syntax is:
CREATE UNLOGGED TABLE new_table_name AS query;
Specifying Column Names
You can define custom column names for the new table by specifying them explicitly. Basic syntax is:
CREATE TABLE new_table_name ( column_name_list) AS query;
IF NOT EXISTS
Use the IF NOT EXISTS clause to avoid an error if the table already exists. Basic syntax is:
CREATE TABLE IF NOT EXISTS new_table_name AS query;
Difference between CREATE TABLE & SELECT INTO :
CREATE TABLE AS is a more appropriate and feature-rich alternative to SELECT INTO. It allows copying data along with additional options for creating indexes, specifying tablespaces, and other configurations.
CREATE SEQUENCE
Syntax
CREATE SEQUENCE sequence_name
[INCREMENT BY increment]
[START WITH start]
[MINVALUE min]
[MAXVALUE max]
[CYCLE | NO CYCLE]
[CACHE cache_size]
[OWNED BY { table_name.column_name | NONE }];
NOTE : I used DVD rental database to perform SQL queries. Set up DVD rental database
Great! Now lets try to practice the above clauses, Sample queries.
GFG tutorials :