Database Design – Best Practices for Employee Information

database-design

I would like to ask if I'm doing this right since it's my first time creating a database model.

Basically what I'm trying do build is an Employee Information model where:

  • An employee can have multiple addresses but only one can be tagged as the permanent address
  • An employee can have multiple positions but only one can be tagged as the current position. Same for the employee department
    • The reason I choose one-to-many relationships on the position and department is to track its histories
  • An employee can have multiple dependents.

Here's an example of my initial schema

enter image description here

I would appreciate your feedback if I'm doing it right or if you can help me design it in a much better way.

Best Answer

The most common pattern for handling historical data in an OLTP schema is to have a current table and a separate history table. So any time an employee changes department, the employee_department table is updated, and a new row is inserted into the employee_department_history table.

You can also use a single table with begin/end dates. This example of this pattern in SQL Server's older AdventureWorks sample database. Which tracks both department and position (shift) in a single table:

CREATE TABLE [HumanResources].[EmployeeDepartmentHistory](
    [BusinessEntityID] [int] NOT NULL,
    [DepartmentID] [smallint] NOT NULL,
    [ShiftID] [tinyint] NOT NULL,
    [StartDate] [date] NOT NULL,
    [EndDate] [date] NULL,
    [ModifiedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_EmployeeDepartmentHistory_BusinessEntityID_StartDate_DepartmentID] PRIMARY KEY CLUSTERED 
(
    [BusinessEntityID] ASC,
    [StartDate] ASC,
    [DepartmentID] ASC,
    [ShiftID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

SQL Server eventually added a feature to implement the current/history pattern automatically called Temporal Tables, and the newer WideWorldImporters sample database uses those.