PostgreSQL

Chapter 5 - DDL (Data Definition Language)

NUll vs NOT NULL

In PostgreSQL, the NULL and NOT NULLconstraints are essential for defining how columns in your tables handle missing or undefined data. Understanding these constraints is crucial for maintaining data integrity and ensuring that your database schema accurately reflects the requirements of your application. Here's a guide to help you understand the difference between NULL and NOT NULL, along with practical examples using tables related to a banking system, such as customers, accounts, and transactions.

Understanding NULL and NOT NULL

  1. NULL Constraint
    • A NULL value represents the absence of a value or an unknown value. By default, columns in PostgreSQL can accept NULL values unless explicitly restricted.
    • Syntax: No special keyword is needed; the absence ofNOT NULLimplies the column can contain NULL.
      Example: Creating a customers Table with Nullable Columns
      CREATE TABLE customers (
          customer_id SERIAL PRIMARY KEY,
          first_name VARCHAR(50),
          last_name VARCHAR(50),
          email VARCHAR(100),
          phone_number VARCHAR(15)  -- This column can have NULL values
      );
      
  2. NOT NULL Constraint
    • The NOT NULLconstraint ensures that a column cannot have NULLvalues. This constraint is used to enforce that every row in the table must contain a value for this column.
    • Syntax: Specify NOT NULL when defining the column.
      Example: Creating an accounts Table with NOT NULL Columns
      CREATE TABLE accounts (
          account_id SERIAL PRIMARY KEY,
          customer_id INTEGER NOT NULL REFERENCES customers(customer_id),
          account_type VARCHAR(20) NOT NULL,
          balance DECIMAL(15, 2) NOT NULL
      );
      
  3. Adding and Modifying NULL/NOT NULL Constraints
    • Add a NOT NULL Constraint: You can add aNOT NULL constraint to an existing column using ALTER TABLE.
      ALTER TABLE table_name
      ALTER COLUMN column_name SET NOT NULL;
      
      Example: Adding NOT NULL Constraint to phone_number in customers Table
      ALTER TABLE customers
      ALTER COLUMN phone_number SET NOT NULL;
      
    • Remove a NOT NULL Constraint: To allow NULLvalues in a column previously defined asNOT NULL, you can remove the constraint.
      ALTER TABLE table_name
      ALTER COLUMN column_name DROP NOT NULL;
      
      Example: Removing NOT NULL Constraint fromemailin customers Table
      ALTER TABLE customers
      ALTER COLUMN email DROP NOT NULL;
      
  4. Using NULL in Queries
    • When querying columns with NULL values, use IS NULL or IS NOT NULL to filter results.
    • Syntax:
      SELECT * FROM table_name
      WHERE column_name IS NULL;
      
      Example: Finding Accounts with Missing account_type
      SELECT * FROM accounts
      WHERE account_type IS NULL;
      
  5. Default Values and NULL
    • If a column does not have aNOT NULL constraint and no default value is specified, it will default to NULL.
    • Syntax: Specify a default value to NULL if needed.
    CREATE TABLE transactions (
        transaction_id SERIAL PRIMARY KEY,
        account_id INTEGER NOT NULL,
        transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        amount DECIMAL(15, 2) NOT NULL,
        description TEXT DEFAULT 'No description'
    );
    
    
    Example: Creating transactions Table with Default Values
    CREATE TABLE transactions (
        transaction_id SERIAL PRIMARY KEY,
        account_id INTEGER NOT NULL,
        transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        amount DECIMAL(15, 2) NOT NULL,
        description TEXT DEFAULT 'No description'
    );
    
    

    Considerations When Using NULL vs NOT NULL

    • Data Integrity: UseNOT NULL to ensure critical columns always have values.
    • Flexibility: Allow NULL for optional or unknown values but be mindful of how it affects data queries and integrity.
    • Performance: Queries on NULL values can sometimes be slower due to the extra handling required.

    This guide should help beginners understand and applyNULL andNOT NULL constraints in PostgreSQL. By using these constraints appropriately, you can ensure that your database schema enforces the necessary rules for data integrity and accuracy.

  6. Tansy SQL Course - NUll vs NOT NULL - Video Thumbnail
Comments(0 comments)

Comments Not Found