PostgreSQL

Chapter 5 - DDL (Data Definition Language)

AUTO INCREMENT

postgresql-database-table/

In PostgreSQL, the AUTO_INCREMENT feature is used to automatically generate unique values for a column, typically for primary keys. This feature simplifies the process of creating unique identifiers for new rows without requiring manual intervention. PostgreSQL achieves this through sequences, which automatically increment a number each time a new row is inserted. This is particularly useful in managing unique identifiers in tables such as customers, accounts, and transactions in a banking system. Below is a guide on using auto-incrementing columns with practical examples.

Using Auto Increment in PostgreSQL

  1. Basic Syntax for Auto Increment with Serial Type
    CREATE TABLE table_name (
        column_name SERIAL PRIMARY KEY
    );
    
    • table_name: The name of the table.
    • column_name: The name of the column that will auto-increment.
    • SERIAL: A postgreSQL shorthand for creating an auto-incrementing integer column.

    Example: Creating a customers Table with Auto Incrementing ID

    CREATE TABLE customers (
        customer_id SERIAL PRIMARY KEY, -- Auto-incrementing column
        first_name VARCHAR(50) NOT NULL,
        last_name VARCHAR(50) NOT NULL,
        email VARCHAR(100) UNIQUE
    );
    
  2. Using BIGSERIAL for Larger Ranges
    • Syntax:
      CREATE TABLE table_name (
          column_name BIGSERIAL PRIMARY KEY
      );
      
    • BIGSERIAL: For larger ranges of auto-incrementing integers.

    Example: Creating an accounts Table with BIGSERIAL

    CREATE TABLE accounts (
        account_id BIGSERIAL PRIMARY KEY, -- Auto-incrementing column for larger range
        customer_id INTEGER NOT NULL REFERENCES customers(customer_id),
        account_number VARCHAR(20) UNIQUE,
        balance DECIMAL(15, 2) DEFAULT 0.00
    );
    
  3. Using Sequences for More Control
    • You can manually create and use sequences for more control over auto-incrementing values.
    • Syntax:
      CREATE SEQUENCE sequence_name
      START WITH initial_value
      INCREMENT BY increment_value;
      
      CREATE TABLE table_name (
          column_name INTEGER DEFAULT nextval('sequence_name') PRIMARY KEY
      );
      

    Example: Creating a transactions Table with a Custom Sequence

    CREATE SEQUENCE transaction_id_seq
    START WITH 1
    INCREMENT BY 1;
    
    CREATE TABLE transactions (
        transaction_id INTEGER DEFAULT nextval('transaction_id_seq') PRIMARY KEY,
        account_id INTEGER NOT NULL REFERENCES accounts(account_id),
        transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        amount DECIMAL(15, 2) DEFAULT 0.00,
        description TEXT
    );
    
  4. Changing the Sequence for an Existing Column
    • You can modify the sequence associated with an existing column.
    • Syntax:
      ALTER SEQUENCE sequence_name
      RESTART WITH new_value;
      

    Example: Resetting the Sequence for transactions Table

    ALTER SEQUENCE transaction_id_seq
    RESTART WITH 1000;
    
  5. Considerations When Using Auto Increment
    • Sequence Gaps: Gaps in auto-increment values can occur due to rollbacks or deletions.
    • Concurrency: PostgreSQL handles concurrency issues for sequences, ensuring unique values even in high-traffic environments.
    • Data Integrity: Ensure that the auto-incrementing values meet your data integrity and business logic requirements.

This guide should help beginners understand and use auto-incrementing columns in PostgreSQL. By automating the generation of unique identifiers, you simplify database management and ensure that each row in your tables has a unique key.

Tansy SQL Course - AUTO INCREMENT - Video Thumbnail
Comments(0 comments)

Comments Not Found