PostgreSQL Constraints Explained (PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL)
Constraints in PostgreSQL are used to control the data that can be inserted into tables.
They help maintain data integrity and accuracy inside the database.
In this article, you will learn:
- What constraints are
- Why constraints are important
- Common PostgreSQL constraints
- Examples of each constraint
This guide is written for beginners.
What Are Constraints in PostgreSQL?
A constraint is a rule applied to a table column.
Constraints ensure that:
- Invalid data is not stored
- Duplicate or incorrect data is prevented
- Relationships between tables are maintained
PostgreSQL checks constraints automatically when data is inserted or updated.
Why Are Constraints Important?
Constraints help:
- Prevent bad data
- Enforce unique values
- Maintain relationships between tables
- Improve database reliability
Without constraints, data quality can quickly degrade.
Common PostgreSQL Constraints
PostgreSQL supports several constraints. The most commonly used ones are:
- PRIMARY KEY
- NOT NULL
- UNIQUE
- FOREIGN KEY
- CHECK
- DEFAULT
1. PRIMARY KEY Constraint
The PRIMARY KEY uniquely identifies each row in a table.
Rules:
- Must be unique
- Cannot be NULL
- Only one primary key per table
Example
Here:
iduniquely identifies each user
2. NOT NULL Constraint
The NOT NULL constraint ensures that a column cannot have NULL values.
Example
In this table:
namemust always have a value
3. UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different.
Example
This prevents two customers from having the same email address.
4. FOREIGN KEY Constraint
The FOREIGN KEY constraint creates a relationship between two tables.
It ensures that a value in one table exists in another table.
Example: Parent Table
Example: Child Table
This ensures that department_id in employees must exist in departments.
5. CHECK Constraint
The CHECK constraint ensures that values meet a specific condition.
Example
This prevents inserting products with negative prices.
6. DEFAULT Constraint
The DEFAULT constraint sets a default value if no value is provided.
Example
If status is not provided during insert, PostgreSQL stores PENDING.
Adding Constraints to an Existing Table
You can add constraints after table creation.
Add NOT NULL
Add UNIQUE
Removing Constraints
To remove a constraint, use DROP CONSTRAINT.
Best Practices for Using Constraints
- Always use a PRIMARY KEY
- Use NOT NULL for mandatory fields
- Use UNIQUE for emails and usernames
- Use FOREIGN KEY to maintain relationships
- Use CHECK for business rules
Constraints make your database strong and reliable.
Summary
- Constraints enforce rules on data
- PRIMARY KEY uniquely identifies rows
- NOT NULL prevents empty values
- UNIQUE avoids duplicate data
- FOREIGN KEY maintains relationships
- CHECK enforces conditions
- DEFAULT assigns default values
You now understand PostgreSQL constraints clearly.
What’s Next?
Next, we will learn about indexes and how they improve performance.
Next article:
PostgreSQL Indexes Explained with Examples
No comments:
Post a Comment