PostgreSQL

Chapter 5 - DDL (Data Definition Language)

CHECK Constraint

The CHECK constraint in PostgreSQL is a powerful tool used to enforce data integrity by ensuring that the values in a column meet specific criteria. This constraint allows you to define conditions that must be satisfied for the data to be inserted or updated in a table. It's especially useful for maintaining consistency and correctness in your database, such as ensuring valid account balances or valid transaction amounts. Below, you'll find a guide on using the CHECK constraint with practical examples related to a banking system, including tables for customers, accounts, and transactions.

Using CHECK Constraint in PostgreSQL

  1. Basic Syntax for CHECK Constraint
    CREATE TABLE table_name (
        column_name data_type CHECK (condition)
    );
    
    • table_name: The name of the table.
    • column_name: The name of the column to apply the constraint.
    • data_type: The data type of the column.
    • condition: The condition that must be met for the data to be valid.
    Example: Creating a customers Table with CHECK Constraints
    CREATE TABLE customers (
        customer_id SERIAL PRIMARY KEY,
        first_name VARCHAR(50) NOT NULL,
        last_name VARCHAR(50) NOT NULL,
        email VARCHAR(100) UNIQUE,
        age INTEGER CHECK (age >= 18)
    );
    
  2. CHECK Constraint with Multiple Conditions
    • You can combine multiple conditions using logical operators like AND and OR.
    • Syntax:
    CREATE TABLE table_name (
        column_name data_type CHECK (condition1 AND condition2)
    );
    
    Example: Creating an accounts Table with Combined CHECK Constraints
    CREATE TABLE accounts (
        account_id SERIAL PRIMARY KEY,
        customer_id INTEGER NOT NULL REFERENCES customers(customer_id),
        account_type VARCHAR(20) CHECK (account_type IN ('savings', 'checking')),
        balance DECIMAL(15,2) CHECK (balance >= 0)
    );
    
  3. Adding CHECK Constraints to Existing Tables
    • You can add a CHECK constraint to an existing table using ALTER TABLE.
    • Syntax:
    ALTER TABLE table_name
    ADD CONSTRAINT constraint_name CHECK (condition);
    
    Example: Adding CHECK Constraint to transactions Table
    ALTER TABLE transactions
    ADD CONSTRAINT check_amount_positive CHECK (amount > 0);
    
  4. Dropping CHECK Constraints
    • You can remove a CHECK constraint using ALTER TABLE and DROP CONSTRAINT.
    • Syntax:
    ALTER TABLE table_name
    DROP CONSTRAINT constraint_name;
    
    Example: Dropping a CHECK Constraint from accounts Table
    ALTER TABLE accounts
    DROP CONSTRAINT account_balance_positive;
    
  5. Using CHECK Constraints with Default Values
    • CHECK constraints work with default values to ensure that the default data also meets the specified conditions.
    • Syntax:
    CREATE TABLE table_name (
        column_name data_type DEFAULT default_value CHECK (condition)
    );
    
    Example: Creating transactions Table with Default Values and CHECK Constraints
    CREATE TABLE transactions (
        transaction_id SERIAL PRIMARY KEY,
        account_id INTEGER NOT NULL REFERENCES accounts(account_id),
        transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        amount DECIMAL(15,2) DEFAULT 0 CHECK (amount > 0),
        description TEXT
    );
    
  6. Considerations When Using CHECK Constraints
    • Data Integrity: Use CHECK constraints to enforce rules and prevent invalid data from being entered into the database.
    • Complex Conditions: You can write complex conditions, but be mindful of performance impacts.
    • Error Handling: If a CHECK constraint fails, PostgreSQL raises an error, preventing invalid data from being inserted or updated.

This guide should help beginners understand and apply CHECK constraints in PostgreSQL. By using these constraints effectively, you can ensure that your data adheres to the necessary rules and maintain the integrity of your database.

Tansy SQL Course - CHECK Constraint - Video Thumbnail
Comments(0 comments)

Comments Not Found