PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - NOT NULL Constraint

In PostgreSQL, the NOT NULL constraint is used to ensure that a column in a table cannot contain any NULL values. This constraint enforces the column to always have a value, ensuring data integrity and consistency.

Syntax:

When defining a table:

CREATE TABLE table_name (
    column_name1 data_type NOT NULL,
    column_name2 data_type,
    ...
);

For adding a NOT NULL constraint to an existing table:

ALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL;

Examples:

  1. Creating a New Table with a NOT NULL Constraint:

    If you're creating a new table named employees and you want the employee_id and first_name columns to always have values:

    CREATE TABLE employees (
        employee_id INT NOT NULL,
        first_name VARCHAR(100) NOT NULL,
        last_name VARCHAR(100)
    );
    
  2. Adding a NOT NULL Constraint to an Existing Table:

    If you have an existing table named employees and you want to ensure that the last_name column always has a value:

    ALTER TABLE employees ALTER COLUMN last_name SET NOT NULL;
    

    Before adding a NOT NULL constraint to an existing column, make sure that the column doesn't already contain any NULL values. If it does, the ALTER TABLE command will fail.

Notes:

  • Removing the NOT NULL Constraint: If you need to remove the NOT NULL constraint later, you can use the ALTER TABLE command with the DROP NOT NULL option:

    ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL;
    
  • Combination with Primary Key: Often, the NOT NULL constraint is combined with the PRIMARY KEY constraint, because primary keys inherently cannot have NULL values. For example:

    CREATE TABLE employees (
        employee_id INT NOT NULL PRIMARY KEY,
        first_name VARCHAR(100) NOT NULL,
        last_name VARCHAR(100)
    );
    
  • Performance: Adding a NOT NULL constraint can increase query performance because the database knows that the column always contains a value, simplifying some internal operations.

In summary, the NOT NULL constraint in PostgreSQL is a fundamental tool to ensure that specific columns in your database tables always contain data and do not inadvertently store NULL values. This constraint aids in maintaining data integrity and consistency across your database.

  1. How to define NOT NULL constraint in PostgreSQL:

    • The NOT NULL constraint ensures that a column cannot contain NULL values.
    CREATE TABLE your_table (
       column_name INT NOT NULL,
       other_column VARCHAR(255) NOT NULL
    );
    
  2. Adding NOT NULL constraint to an existing column in PostgreSQL:

    • Add the NOT NULL constraint to an existing column.
    ALTER TABLE your_table
    ALTER COLUMN existing_column SET NOT NULL;
    
  3. NOT NULL vs. DEFAULT NULL in PostgreSQL:

    • Understand the difference between NOT NULL and DEFAULT NULL.
    CREATE TABLE your_table (
       column1 INT NOT NULL,
       column2 INT DEFAULT NULL
    );
    
  4. Removing NOT NULL constraint in PostgreSQL:

    • Remove the NOT NULL constraint from a column.
    ALTER TABLE your_table
    ALTER COLUMN column_name DROP NOT NULL;
    
  5. Handling NOT NULL constraints during data insertion in PostgreSQL:

    • Ensure that data insertion complies with NOT NULL constraints.
    INSERT INTO your_table (column1, column2) VALUES (1, 'value');
    
  6. Checking for NOT NULL values in PostgreSQL:

    • Use IS NOT NULL to filter rows with non-NULL values.
    SELECT *
    FROM your_table
    WHERE column_name IS NOT NULL;
    
  7. Using NOT NULL with CREATE TABLE in PostgreSQL:

    • Specify NOT NULL in the CREATE TABLE statement for each column.
    CREATE TABLE your_table (
       column1 INT NOT NULL,
       column2 VARCHAR(255) NOT NULL
    );
    
  8. Enforcing NOT NULL constraint on multiple columns in PostgreSQL:

    • Apply NOT NULL to multiple columns to ensure data integrity.
    CREATE TABLE your_table (
       column1 INT NOT NULL,
       column2 VARCHAR(255) NOT NULL,
       column3 DATE NOT NULL
    );
    
  9. NOT NULL constraint and foreign key relationships in PostgreSQL:

    • Use NOT NULL in foreign keys for a mandatory relationship.
    CREATE TABLE parent_table (
       parent_id INT PRIMARY KEY
    );
    
    CREATE TABLE child_table (
       child_id INT PRIMARY KEY,
       parent_id INT NOT NULL REFERENCES parent_table(parent_id)
    );
    
  10. NOT NULL constraint and unique constraints in PostgreSQL:

    • Combine NOT NULL with unique constraints for unique non-NULL values.
    CREATE TABLE your_table (
       column1 INT NOT NULL,
       column2 VARCHAR(255) UNIQUE NOT NULL
    );
    
  11. NOT NULL constraint with CHECK constraints in PostgreSQL:

    • Use CHECK constraints along with NOT NULL for more complex conditions.
    CREATE TABLE your_table (
       column1 INT CHECK (column1 > 0),
       column2 VARCHAR(255) NOT NULL
    );
    
  12. Handling NULL values in NOT NULL columns in PostgreSQL:

    • Ensure that NULL values are not inserted into NOT NULL columns.
    INSERT INTO your_table (column1, column2) VALUES (1, NULL); -- Error
    
  13. Adding NOT NULL to primary key columns in PostgreSQL:

    • Specify NOT NULL for primary key columns.
    CREATE TABLE your_table (
       id SERIAL PRIMARY KEY NOT NULL,
       name VARCHAR(255) NOT NULL
    );
    
  14. Using NOT NULL in ALTER TABLE statements in PostgreSQL:

    • Add or remove NOT NULL using ALTER TABLE statements.
    ALTER TABLE your_table
    ALTER COLUMN column_name SET NOT NULL;
    
    ALTER TABLE your_table
    ALTER COLUMN column_name DROP NOT NULL;
    
  15. NOT NULL constraint with composite types in PostgreSQL:

    • Apply NOT NULL to each column of a composite type.
    CREATE TYPE person_type AS (
       first_name VARCHAR(255) NOT NULL,
       last_name VARCHAR(255) NOT NULL
    );
    
    CREATE TABLE your_table (
       person person_type NOT NULL
    );
    
  16. NOT NULL and DEFAULT values in PostgreSQL:

    • Combine NOT NULL with default values for columns.
    CREATE TABLE your_table (
       column1 INT DEFAULT 0 NOT NULL,
       column2 VARCHAR(255) DEFAULT 'default_value' NOT NULL
    );
    
  17. Using NOT NULL in stored procedures and functions in PostgreSQL:

    • Enforce NOT NULL constraints within stored procedures and functions.
    CREATE OR REPLACE FUNCTION your_function()
    RETURNS VOID AS $$
    BEGIN
       -- Check and handle NOT NULL conditions
    END;
    $$ LANGUAGE plpgsql;