PostgreSQL

Chapter 8 - RDBMS Concepts

Surrogate Key

In relational database management systems (RDBMS) like PostgreSQL, a surrogate key is a unique identifier for an entity in a database that is not derived from the data itself. Unlike natural keys, which are based on the attributes of the data (e.g., a Social Security Number or email address), surrogate keys are typically simple, system-generated values such as sequential integers or UUIDs. They are used primarily to simplify relationships between tables and to ensure each record can be uniquely identified without relying on natural attributes that might change.

Why Use Surrogate Keys?

  1. Simplicity: Surrogate keys are usually integer-based and automatically generated, making them simple to use and manage.
  2. Stability: Since surrogate keys are not based on the data, they are less likely to change over time, providing a stable reference for relationships.
  3. Performance: Integer-based surrogate keys often improve performance, especially with indexing, as they require less storage and comparison time compared to complex natural keys.

Example of Surrogate Key in PostgreSQL

Let's consider a banking database with tables for customers, accounts, and transactions. Here’s how you might define tables using surrogate keys:

  1. Customers Table
    CREATE TABLE customers (
    
        customer_id SERIAL PRIMARY KEY,
    
        name VARCHAR(100) NOT NULL,
    
        email VARCHAR(100) UNIQUE NOT NULL
    
    );
    • customer_id is a surrogate key generated automatically using the SERIAL keyword.
  2. Accounts Table
    CREATE TABLE accounts (
    
        account_id SERIAL PRIMARY KEY,
    
        customer_id INTEGER NOT NULL REFERENCES customers(customer_id),
    
        balance DECIMAL(15, 2) NOT NULL
    
    );
    • account_id is a surrogate key for the accounts table.
    • customer_id is a foreign key referencing the customers table.
  3. Transactions Table
    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) NOT NULL
    
    );
    • transaction_id is a surrogate key for the transactions table.
    • account_id is a foreign key referencing the accounts table.

Key Points

  1. Auto-incremented Values:
    • Surrogate keys often use auto-incrementing values (e.g., SERIAL in PostgreSQL) to ensure uniqueness.
  2. Foreign Key Relationships:
    • Surrogate keys are used in foreign key constraints to link records between related tables.
  3. Avoiding Natural Key Problems:
    • By using surrogate keys, you avoid issues related to changes in natural key attributes.

Using surrogate keys simplifies database management and maintains data integrity across relational tables.

Definition and Characteristics

Synthetic Origin:

Surrogate keys are artificially generated, meaning they are not derived from the application data. They are usually numeric and often implemented as an auto-incrementing number or a globally unique identifier (GUID).

Uniqueness:

Like all primary keys, a surrogate key uniquely identifies each row in a table. No two rows can have the same surrogate key value.

Stability:

Once assigned, the value of a surrogate key does not change. This stability is crucial for maintaining referential integrity across tables.

Non-significance:

Surrogate keys have no intrinsic meaning. They do not carry information about the row's data, unlike natural keys, which are derived from application data and might convey meaning.


Advantages

  • Simplicity:Using an auto-incrementing number as a surrogate key simplifies the creation and management of keys. It eliminates the need to determine which natural data attributes can serve as a unique identifier.
  • Performance:Surrogate keys can improve database performance, especially in joins, indexing, and query speed, because of their simplicity and uniformity.
  • Flexibility:They allow for changes in business rules without affecting the primary key. For instance, if the natural key changes (e.g., a product code is updated), it doesn't require modifying the primary key in the database.
  • Referential Integrity:Surrogate keys aid in maintaining referential integrity, as they are stable and unaffected by changes in business data.

Use Cases

  • Dimensional Modeling:In data warehousing and dimensional modeling, surrogate keys are extensively used to uniquely identify dimension members, making it easier to track historical changes in dimension attributes.
  • Complex Natural Keys:When natural keys are complex (composed of multiple columns or prone to change), surrogate keys offer a simplified and stable alternative.
  • Support for ORM:Object-Relational Mapping (ORM) tools often work better with surrogate keys, as they can automatically generate and manage these keys, simplifying the development process.

Considerations

  • Overhead:Introducing an additional column for the surrogate key adds some overhead to the database schema. However, the benefits often outweigh this drawback.
  • Misuse:Surrogate keys should not be used as a substitute for understanding and modeling real-world relationships. They are a technical solution to a technical problem, not a replacement for good database design.

Conclusion

Surrogate keys are an essential tool in database design, offering a simple, stable, and efficient means of uniquely identifying rows in a table. They are particularly useful in scenarios where natural keys are complex, subject to change, or unsuitable for some reason. While surrogate keys add a layer of abstraction over natural data, they facilitate better performance, flexibility, and integrity in database management and design.



SURROGATE KEY DATA MODEL

Image Description
  • Designated Surrogate Keys: The department ID from the department table and the employee ID from the employee table are designated as surrogate keys.
  • No Inherent Business Meaning: Their primary function is to serve as primary keys without carrying any inherent significance in actual business processes.
  • Foreign Key References: Surrogate keys are employed as primary keys and are subsequently referenced in related tables as foreign keys.
  • Internal System Values: In practical business scenarios, referencing an employee ID like "32" wouldn't represent an actual individual; such values are strictly internal to the database system.

SURROGATE KEY DATA

Image Description
  • Employee & Department Link: Consider an employee identified by the employee number EMP-01, who has been linked to department ID = 2.
  • Department Lookup: Referencing this value in the department table aligns with the Executive Department, indicating that EMP-01 is affiliated with the executive department.
  • Auto-Incrementing Key: The department ID in the department table functions as an auto-incrementing surrogate primary key.
Comments(0 comments)

Comments Not Found