The tables
Seven tables in three groups: the organization, the pay, and leave and attendance.
dbo.DepartmentDepartments by name, each name once.dbo.PositionJob titles, each once, with the salary band from MinSalary to MaxSalary.dbo.EmployeeOne row per person, found by employee number or email: department, position and manager.dbo.SalaryAn employee's pay from a start date on; its key is the pair.dbo.LeaveTypeKinds of leave by name, with the days a year each allows.dbo.LeaveRequestLeave asked for: the dates, the status and who reviewed it.dbo.AttendanceAn employee's work day: clock-in and clock-out times; its key is the pair.
How the tables relate
- A department has zero or more employees , and each employee works in exactly one department:
DepartmentIdis NOT NULL, so the line has nothing at the department’s end . Positions work the same way. - An employee has zero or one manager:
ManagerIdis a foreign key todbo.Employeeitself, and it allows NULL for the person at the top, so the line from the table to itself has a ring alone at the manager’s end . dbo.LeaveRequesthas two lines todbo.Employee:EmployeeId, who asks, andReviewedById, who approved or rejected it, NULL until someone reviews it; the application keeps it in step withStatus.dbo.Salaryanddbo.Attendancehang fromdbo.Employeeon solid lines : each one’s primary key starts withEmployeeId, a composite key.- A leave type has zero or more requests; each request is of exactly one type.
Why it is built this way
A manager is another employee
The manager is a row of the same table, so the hierarchy needs no table of its own. The foreign key refuses a manager who doesn’t exist with Msg 547 (a FOREIGN KEY SAME TABLE constraint, in SQL Server’s words), and CK_Employee_ManagerId refuses an employee as their own manager with Msg 547 too. Longer loops, A under B under A, need a check in the application.
Salary is a history, not a column
A raise adds a row to dbo.Salary instead of overwriting a number, so last year’s pay is still there. The primary key (EmployeeId, StartsOn) allows one amount per employee per start date; a second salary starting the same day fails with Msg 2627. The current salary is each employee’s latest row that has started:
SELECT e.EmployeeNumber, e.FullName, s.Amount, s.StartsOn FROM dbo.Employee AS e CROSS APPLY ( SELECT TOP (1) sa.Amount, sa.StartsOn FROM dbo.Salary AS sa WHERE sa.EmployeeId = e.EmployeeId AND sa.StartsOn <= CAST(SYSDATETIME() AS date) ORDER BY sa.StartsOn DESC ) AS s;
A raise entered ahead of time waits in the table until its date comes. Whether an amount sits inside the position’s band is left to the application.
Leave can’t end before it starts
CK_LeaveRequest_EndsOn checks EndsOn >= StartsOn, so a request from August 10 to August 3 fails with Msg 547. A one-day leave starts and ends on the same date. Status takes Pending, Approved or Rejected.
One attendance row per day
The primary key (EmployeeId, WorkDate) keeps one row per employee per day; a second row for the same day fails with Msg 2627. ClockOutAt stays NULL while the employee is at work, and a check keeps it after ClockInAt. A split shift is one row from the first clock-in to the last clock-out; to record each part, add a sequence number to the key.
The SQL Server script
-- Table [dbo].[Department] CREATE TABLE [dbo].[Department] ( [DepartmentId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (100) NOT NULL, CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED ([DepartmentId] ASC), CONSTRAINT [AK_Department_Name] UNIQUE NONCLUSTERED ([Name] ASC) ); GO -- Table [dbo].[LeaveType] CREATE TABLE [dbo].[LeaveType] ( [LeaveTypeId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (50) NOT NULL, [DaysPerYear] TINYINT NOT NULL, CONSTRAINT [PK_LeaveType] PRIMARY KEY CLUSTERED ([LeaveTypeId] ASC), CONSTRAINT [AK_LeaveType_Name] UNIQUE NONCLUSTERED ([Name] ASC) ); GO -- Table [dbo].[Position] CREATE TABLE [dbo].[Position] ( [PositionId] INT IDENTITY (1, 1) NOT NULL, [Title] NVARCHAR (100) NOT NULL, [MinSalary] DECIMAL (10, 2) NOT NULL, [MaxSalary] DECIMAL (10, 2) NOT NULL, CONSTRAINT [PK_Position] PRIMARY KEY CLUSTERED ([PositionId] ASC), CONSTRAINT [CK_Position_MaxSalary] CHECK (MaxSalary >= MinSalary), CONSTRAINT [AK_Position_Title] UNIQUE NONCLUSTERED ([Title] ASC) ); GO -- Table [dbo].[Employee] CREATE TABLE [dbo].[Employee] ( [EmployeeId] INT IDENTITY (1, 1) NOT NULL, [EmployeeNumber] VARCHAR (20) NOT NULL, [FullName] NVARCHAR (200) NOT NULL, [Email] NVARCHAR (254) NOT NULL, [HiredOn] DATE NOT NULL, [DepartmentId] INT NOT NULL, [PositionId] INT NOT NULL, [ManagerId] INT NULL, CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ([EmployeeId] ASC), CONSTRAINT [CK_Employee_ManagerId] CHECK (ManagerId <> EmployeeId), CONSTRAINT [FK_Employee_Department_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [dbo].[Department] ([DepartmentId]), CONSTRAINT [FK_Employee_Employee_ManagerId] FOREIGN KEY ([ManagerId]) REFERENCES [dbo].[Employee] ([EmployeeId]), CONSTRAINT [FK_Employee_Position_PositionId] FOREIGN KEY ([PositionId]) REFERENCES [dbo].[Position] ([PositionId]), CONSTRAINT [AK_Employee_Email] UNIQUE NONCLUSTERED ([Email] ASC), CONSTRAINT [AK_Employee_EmployeeNumber] UNIQUE NONCLUSTERED ([EmployeeNumber] ASC) ); GO -- Table [dbo].[Attendance] CREATE TABLE [dbo].[Attendance] ( [EmployeeId] INT NOT NULL, [WorkDate] DATE NOT NULL, [ClockInAt] DATETIME2 (0) NOT NULL, [ClockOutAt] DATETIME2 (0) NULL, CONSTRAINT [PK_Attendance] PRIMARY KEY CLUSTERED ([EmployeeId] ASC, [WorkDate] ASC), CONSTRAINT [CK_Attendance_ClockOutAt] CHECK (ClockOutAt > ClockInAt), CONSTRAINT [FK_Attendance_Employee_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [dbo].[Employee] ([EmployeeId]) ); GO -- Table [dbo].[LeaveRequest] CREATE TABLE [dbo].[LeaveRequest] ( [LeaveRequestId] INT IDENTITY (1, 1) NOT NULL, [EmployeeId] INT NOT NULL, [LeaveTypeId] INT NOT NULL, [StartsOn] DATE NOT NULL, [EndsOn] DATE NOT NULL, [Status] VARCHAR (10) NOT NULL, [ReviewedById] INT NULL, CONSTRAINT [PK_LeaveRequest] PRIMARY KEY CLUSTERED ([LeaveRequestId] ASC), CONSTRAINT [CK_LeaveRequest_EndsOn] CHECK (EndsOn >= StartsOn), CONSTRAINT [CK_LeaveRequest_Status] CHECK (Status IN ('Pending', 'Approved', 'Rejected')), CONSTRAINT [FK_LeaveRequest_Employee_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [dbo].[Employee] ([EmployeeId]), CONSTRAINT [FK_LeaveRequest_Employee_ReviewedById] FOREIGN KEY ([ReviewedById]) REFERENCES [dbo].[Employee] ([EmployeeId]), CONSTRAINT [FK_LeaveRequest_LeaveType_LeaveTypeId] FOREIGN KEY ([LeaveTypeId]) REFERENCES [dbo].[LeaveType] ([LeaveTypeId]) ); GO -- Table [dbo].[Salary] CREATE TABLE [dbo].[Salary] ( [EmployeeId] INT NOT NULL, [StartsOn] DATE NOT NULL, [Amount] DECIMAL (10, 2) NOT NULL, CONSTRAINT [PK_Salary] PRIMARY KEY CLUSTERED ([EmployeeId] ASC, [StartsOn] ASC), CONSTRAINT [CK_Salary_Amount] CHECK (Amount > 0), CONSTRAINT [FK_Salary_Employee_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [dbo].[Employee] ([EmployeeId]) ); GO
Questions
What tables does an employee management system need?
Employees, departments and positions at the least. Salary history, leave types and requests, and attendance come next. This design has all seven.
How do I store who reports to whom?
With a foreign key from the employee table to itself: ManagerId references EmployeeId. A self join on it lists each employee with their manager's name.
Why is the salary not a column of the employee?
A column holds one amount and forgets the last one. A table keyed by employee and start date keeps every change, and the query under "Why it is built this way" picks the current one.
Can I change the design?
Yes. Open it in ERDsmith, free in the browser: add a table, rename a column, and the keys and lines follow.
Does it work with MySQL or PostgreSQL?
The script is written for SQL Server. The tables, keys and relationships carry over to other databases; the types need that database's own names, and the current-salary query uses CROSS APPLY, which is LATERAL in PostgreSQL and MySQL.
