Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Temporal Data and History Tables | Advanced Database Design Patterns
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Database Design Patterns

bookTemporal Data and History Tables

Tracking how data changes over time is a crucial requirement in many business systems. This need arises because organizations often want to know not just the current state of a record, but also its previous states—such as how an employee's salary has changed, or what a customer's address was at a certain point in the past. This is known as temporal data management. By maintaining a history of changes, you can answer questions about trends, audits, regulatory compliance, and more. To achieve this, databases use history tables or temporal tables that record every change to important data, along with information about when each version was valid.

-- Schema for employee_salaries history table
CREATE TABLE employee_salaries (
    employee_salary_id SERIAL PRIMARY KEY,
    employee_id INTEGER NOT NULL REFERENCES employees(employee_id),
    salary NUMERIC(12,2) NOT NULL,
    effective_from DATE NOT NULL,
    effective_to DATE
);

Temporal tables like employee_salaries allow you to store not only the current salary for each employee, but also all previous salaries and when they were in effect. The effective_from and effective_to columns define the period during which a particular salary was valid. To find the current salary, you look for the record where effective_to is null; to see historical changes, you query all records for an employee. This approach enables you to answer complex business questions, such as finding out an employee's salary at any point in the past or analyzing how salaries have evolved across the organization.

-- Query to get the entire salary history for employee_id = 2
SELECT salary, effective_from, effective_to
FROM employee_salaries
WHERE employee_id = 2
ORDER BY effective_from DESC;

1. What is the main purpose of a history table?

2. How can you track changes to a record over time?

3. What is a temporal table in database design?

question mark

What is the main purpose of a history table?

Select the correct answer

question mark

How can you track changes to a record over time?

Select the correct answer

question mark

What is a temporal table in database design?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

How can I retrieve the current salary for a specific employee?

Can you explain how to update an employee's salary in this temporal table?

What are some best practices for managing temporal data in databases?

bookTemporal Data and History Tables

Scorri per mostrare il menu

Tracking how data changes over time is a crucial requirement in many business systems. This need arises because organizations often want to know not just the current state of a record, but also its previous states—such as how an employee's salary has changed, or what a customer's address was at a certain point in the past. This is known as temporal data management. By maintaining a history of changes, you can answer questions about trends, audits, regulatory compliance, and more. To achieve this, databases use history tables or temporal tables that record every change to important data, along with information about when each version was valid.

-- Schema for employee_salaries history table
CREATE TABLE employee_salaries (
    employee_salary_id SERIAL PRIMARY KEY,
    employee_id INTEGER NOT NULL REFERENCES employees(employee_id),
    salary NUMERIC(12,2) NOT NULL,
    effective_from DATE NOT NULL,
    effective_to DATE
);

Temporal tables like employee_salaries allow you to store not only the current salary for each employee, but also all previous salaries and when they were in effect. The effective_from and effective_to columns define the period during which a particular salary was valid. To find the current salary, you look for the record where effective_to is null; to see historical changes, you query all records for an employee. This approach enables you to answer complex business questions, such as finding out an employee's salary at any point in the past or analyzing how salaries have evolved across the organization.

-- Query to get the entire salary history for employee_id = 2
SELECT salary, effective_from, effective_to
FROM employee_salaries
WHERE employee_id = 2
ORDER BY effective_from DESC;

1. What is the main purpose of a history table?

2. How can you track changes to a record over time?

3. What is a temporal table in database design?

question mark

What is the main purpose of a history table?

Select the correct answer

question mark

How can you track changes to a record over time?

Select the correct answer

question mark

What is a temporal table in database design?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5
some-alt