Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Create a Table in PostgreSQL
Creating tables is an essential step in designing any relational database. In PostgreSQL, the CREATE TABLE
command is used to create a new table, specifying its structure and the data types of its columns.
Syntax for Creating a Table
The basic syntax for creating a table in PostgreSQL is as follows:
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);
In the above syntax:
- table_name is the name of the table you want to create.
- column1, column2, ... are the columns in the table, and you define each column with its name, datatype, and optional constraints.
Example: Creating a Simple Table
Let’s create a table called employees
to store employee information:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100),
hire_date DATE
);
This SQL command creates a table with the following columns:
- id: A unique identifier for each employee. We use the
SERIAL
datatype to automatically generate sequential numbers for each record. - first_name: A text field for the employee's first name (up to 100 characters).
- last_name: A text field for the employee's last name (up to 100 characters).
- hire_date: A date field to store when the employee was hired.
Adding Constraints
PostgreSQL supports various constraints that can be applied to columns. Constraints help ensure the integrity and correctness of the data. For example, the PRIMARY KEY
constraint ensures that values in the id
column are unique and not null.
Common Constraints
- NOT NULL: Ensures the column does not accept NULL values.
- UNIQUE: Ensures all values in the column are unique.
- CHECK: Ensures the value in a column satisfies a boolean expression.
- FOREIGN KEY: Enforces a link between two tables.
Conclusion
Creating tables in PostgreSQL is straightforward and essential for database design. With a clear understanding of the CREATE TABLE
syntax, you can define the structure of your data and apply constraints to maintain integrity.