Employee and Payroll Management ER diagram and database schema

The Employee and Payroll Management database schema is a DB Planner template with 25 tables, 187 columns and 43 relationships, free to use. Core tables include bank_accounts, compensation_records, deduction_types, departments. It can be opened as an interactive ER diagram or exported as PostgreSQL, MySQL, or SQL Server DDL.

ER diagram of the Employee and Payroll Management database schema, showing 25 tables and their relationships
FIG. 1: entity relationship diagram, 25 tables
Tables
25
Columns
187
Relationships
43
Category
SaaS
Exports
DBML, SQL, Prisma, C#
Price
Free

What this schema models

A complete employee management and payroll system database design: 25 tables, 17 enums and 43 relationships covering legal entities, departments and work locations, employee records, effective-dated job assignments and compensation, tax withholding elections, benefits and deductions, time entries, leave requests and accrual, pay schedules and periods, pay runs, itemized pay stubs, and split direct deposits. THE FOUR DESIGN RULES 1. Everything that changes a paycheck is effective-dated. Job assignments, compensation, withholding elections, deductions and home addresses are rows with effective_from and effective_to, never overwritten, so "what was this employee paid on March 15?" and retro pay are exact. 2. Approved payroll is immutable. A run can be recalculated while it is a draft; once approved, a mistake is fixed with a correction or off-cycle run, and a voided paycheck points at the check that reversed it. 3. Pay stubs are itemized snapshots. Every earning, deduction, tax and employer contribution is its own line, with the hours and rate that produced it, and the totals are frozen at approval. 4. Leave balances are a ledger. Accruals, usage, carryover and payouts are entries; a balance is their sum, never a column someone can edit. DETAILS WORTH STEALING - Direct deposit splits across several accounts by priority, with fixed, percentage and remainder allocations. - Time entries record the run that paid them, so approved hours are never paid twice. - Taxes point at jurisdictions (federal, state, local), and both work location and home address matter. - Bank account numbers are stored as tokens with the last four digits, and national IDs as the last four only; full numbers belong in a vault. - Departments nest, so labor cost rolls up the org chart. - Multiple legal entities each run their own payroll. WHAT YOU GET Clean DBML that imports directly into DB Planner, and a laid-out board with zones color-coded and framed: organization, employees, employment terms, deductions, time and leave, payroll runs, and pay stubs and deposits. Every table carries a note. Postgres-flavoured types that port cleanly to MySQL or SQL Server. Useful for developers building HR, HRIS or payroll software, and for students who need a realistic employee management or payroll system ER diagram for a DBMS project. This is a schema design reference, not a tax engine: withholding calculations and compliance rules vary by jurisdiction and belong in your payroll provider or tax service.

Tables (25)

Every table and its columns

departments

The org chart. Labor cost reports roll up through parent_id.

Columns of the departments table
ColumnTypeConstraintsReferencesDescription
iduuidPK
legal_entity_iduuidFK, not nulllegal_entities.id
parent_iduuidFKdepartments.idDepartments nest; the root has no parent
namevarchar(200)not null
cost_centervarchar(30)

tax_jurisdictions

Where taxes are owed. Work locations and withholding elections point here; the tax rules themselves live in your tax engine.

Columns of the tax_jurisdictions table
ColumnTypeConstraintsReferencesDescription
iduuidPK
codevarchar(20)unique, not nulle.g. US-FED, US-CA, US-NY-NYC
namevarchar(200)not null
leveljurisdiction_levelnot null

work_locations

Offices and remote work sites. Where someone works drives which taxes apply.

Columns of the work_locations table
ColumnTypeConstraintsReferences
iduuidPK
legal_entity_iduuidFK, not nulllegal_entities.id
namevarchar(200)not null
address_line1varchar(200)
cityvarchar(120)
regionvarchar(60)
countrychar(2)not null
tax_jurisdiction_iduuidFK, not nulltax_jurisdictions.id
is_remotebooleannot null, default false

job_titles

The catalog of roles. Who holds which one, and since when, is in job_assignments.

Columns of the job_titles table
ColumnTypeConstraintsReferences
iduuidPK
namevarchar(120)unique, not null
job_familyvarchar(60)
pay_gradevarchar(10)

holidays

Paid holidays per employer. Holiday pay and leave calculations read this calendar.

Columns of the holidays table
ColumnTypeConstraintsReferences
iduuidPK
legal_entity_iduuidFK, not nulllegal_entities.id
holiday_datedatenot null
namevarchar(100)not null

Indexes: (legal_entity_id, holiday_date) unique

employees

One row per person employed by an entity. A rehire reuses the row, so tenure and history survive.

Columns of the employees table
ColumnTypeConstraintsReferencesDescription
iduuidPK
employee_numbervarchar(20)unique, not null
legal_entity_iduuidFK, not nulllegal_entities.id
legal_first_namevarchar(100)not null
legal_last_namevarchar(100)not null
preferred_namevarchar(100)
work_emailvarchar(254)unique
date_of_birthdatenot null
national_id_last4char(4)Last four of the SSN or equivalent for matching; the full number belongs in a vault, not this table
hire_datedatenot null
termination_datedate
statusemployee_statusnot null, default 'active'

employee_addresses

Home address history. Residence drives some local taxes, so the address on a past pay date has to be recoverable.

Columns of the employee_addresses table
ColumnTypeConstraintsReferencesDescription
iduuidPK
employee_iduuidFK, not nullemployees.id
address_line1varchar(200)not null
cityvarchar(120)not null
regionvarchar(60)
postal_codevarchar(20)
countrychar(2)not null
effective_fromdatenot null
effective_todateNull for the current address

emergency_contacts

Who to call.

Columns of the emergency_contacts table
ColumnTypeConstraintsReferences
iduuidPK
employee_iduuidFK, not nullemployees.id
namevarchar(200)not null
relationshipvarchar(60)not null
phonevarchar(30)not null

bank_accounts

Direct deposit accounts. Net pay is split across active accounts by priority, with one remainder account taking the rest.

Columns of the bank_accounts table
ColumnTypeConstraintsReferencesDescription
iduuidPK
employee_iduuidFK, not nullemployees.id
routing_numbervarchar(20)not null
account_tokenvarchar(200)not nullTokenized account number from your payments provider
account_last4char(4)not null
account_typebank_account_typenot null
allocationallocation_kindnot nullFixed amount, percentage, or the remainder
allocation_valuenumeric(12,2)Cents for amount, 0-100 for percent, null for remainder
prioritysmallintnot null, default 1Order in which a split paycheck is distributed
activebooleannot null, default true

job_assignments

Role, department, manager and location over time. A promotion closes one row and opens the next.

Columns of the job_assignments table
ColumnTypeConstraintsReferencesDescription
iduuidPK
employee_iduuidFK, not nullemployees.id
job_title_iduuidFK, not nulljob_titles.id
department_iduuidFK, not nulldepartments.id
work_location_iduuidFK, not nullwork_locations.id
manager_iduuidFKemployees.id
employment_typeemployment_typenot null
flsa_statusflsa_statusnot nullExempt or non-exempt from overtime
ftenumeric(3,2)not null, default 1Full-time equivalent, e.g. 0.50 for half time
effective_fromdatenot null
effective_todateNull for the current assignment
reasonvarchar(100)e.g. Hire, Promotion, Transfer

Indexes: (employee_id, effective_from) unique

compensation_records

Pay rate history. A raise is a new row from its effective date, so a retro adjustment can be computed exactly.

Columns of the compensation_records table
ColumnTypeConstraintsReferencesDescription
iduuidPK
employee_iduuidFK, not nullemployees.id
pay_typepay_typenot null
amount_centsbigintnot nullAnnual salary for salaried, hourly rate for hourly
currencychar(3)not null
pay_schedule_iduuidFK, not nullpay_schedules.id
effective_fromdatenot null
effective_todate
reasonvarchar(100)e.g. Annual review, Market adjustment
approved_byuuidFKemployees.id

Indexes: (employee_id, effective_from) unique

tax_withholding_elections

Withholding choices per jurisdiction, effective-dated like everything else that changes a paycheck.

Columns of the tax_withholding_elections table
ColumnTypeConstraintsReferencesDescription
iduuidPK
employee_iduuidFK, not nullemployees.id
tax_jurisdiction_iduuidFK, not nulltax_jurisdictions.id
filing_statusvarchar(30)not nullAs declared on the W-4 or local equivalent
allowancessmallintnot null, default 0
extra_withholding_centsintegernot null, default 0
exemptbooleannot null, default false
effective_fromdatenot null
effective_todate

employee_deductions

An employee enrolled in a deduction: benefits, retirement, garnishments.

Columns of the employee_deductions table
ColumnTypeConstraintsReferencesDescription
iduuidPK
employee_iduuidFK, not nullemployees.id
deduction_type_iduuidFK, not nulldeduction_types.id
calculationcalculation_kindnot null
valuenumeric(12,2)not nullCents per pay period, or percent of gross
annual_limit_centsbiginte.g. the 401(k) contribution limit
effective_fromdatenot null
effective_todate

time_entries

Hours worked by non-exempt staff. Only approved, unpaid entries are picked up by the next run.

Columns of the time_entries table
ColumnTypeConstraintsReferencesDescription
iduuidPK
employee_iduuidFK, not nullemployees.id
work_datedatenot null
hoursnumeric(5,2)not null
earning_codeearning_codenot null, default 'regular'
approved_byuuidFKemployees.id
approved_attimestamptz
pay_run_iduuidFKpay_runs.idSet when paid, so hours are never paid twice

Indexes: (employee_id, work_date)

leave_types

Kinds of time off and how they accrue.

Columns of the leave_types table
ColumnTypeConstraintsReferencesDescription
iduuidPK
legal_entity_iduuidFK, not nulllegal_entities.id
codevarchar(20)not nulle.g. PTO, SICK, PARENTAL
namevarchar(100)not null
accrual_hours_per_periodnumeric(5,2)not null, default 0
max_balance_hoursnumeric(6,2)
paidbooleannot null, default true

leave_requests

Time off asked for. Approval writes a usage entry to the leave ledger.

Columns of the leave_requests table
ColumnTypeConstraintsReferences
iduuidPK
employee_iduuidFK, not nullemployees.id
leave_type_iduuidFK, not nullleave_types.id
starts_ondatenot null
ends_ondatenot null
hoursnumeric(6,2)not null
statusrequest_statusnot null, default 'pending'
decided_byuuidFKemployees.id
decided_attimestamptz

leave_ledger

Every accrual, use and adjustment. A balance is the sum of this ledger, never a column someone can overwrite.

Columns of the leave_ledger table
ColumnTypeConstraintsReferencesDescription
iduuidPK
employee_iduuidFK, not nullemployees.id
leave_type_iduuidFK, not nullleave_types.id
kindledger_kindnot null
hoursnumeric(6,2)not nullPositive for accruals, negative for usage
effective_ondatenot null
leave_request_iduuidFKleave_requests.id
pay_run_iduuidFKpay_runs.idThe run that accrued it, for accrual entries
memovarchar(200)

deduction_types

Benefit and deduction plans an employee can be enrolled in.

Columns of the deduction_types table
ColumnTypeConstraintsReferencesDescription
iduuidPK
codevarchar(20)unique, not nulle.g. 401K, MEDICAL, DENTAL, HSA, GARNISH
namevarchar(100)not null
pre_taxbooleannot nullReduces taxable wages when true
employer_match_percentnumeric(5,2)Employer contribution as a percent of the employee amount

pay_schedules

How often a group is paid.

Columns of the pay_schedules table
ColumnTypeConstraintsReferencesDescription
iduuidPK
legal_entity_iduuidFK, not nulllegal_entities.id
namevarchar(60)not null
frequencypay_frequencynot null
anchor_datedatenot nullA known period start; every other period is computed from it

pay_periods

The concrete periods of a schedule.

Columns of the pay_periods table
ColumnTypeConstraintsReferences
iduuidPK
pay_schedule_iduuidFK, not nullpay_schedules.id
starts_ondatenot null
ends_ondatenot null
pay_datedatenot null

Indexes: (pay_schedule_id, starts_on) unique

pay_runs

One payroll calculation. Drafts can be recalculated; once approved, a mistake is fixed by an off-cycle or correction run, never by editing this one.

Columns of the pay_runs table
ColumnTypeConstraintsReferences
iduuidPK
pay_period_iduuidFK, not nullpay_periods.id
kindrun_kindnot null, default 'regular'
statusrun_statusnot null, default 'draft'
calculated_attimestamptz
approved_byuuidFKemployees.id
approved_attimestamptz
paid_attimestamptz

paychecks

One employee in one run: the pay stub. Totals are snapshots of the lines below, frozen at approval.

Columns of the paychecks table
ColumnTypeConstraintsReferencesDescription
iduuidPK
pay_run_iduuidFK, not nullpay_runs.id
employee_iduuidFK, not nullemployees.id
gross_centsbigintnot null
taxes_centsbigintnot null
deductions_centsbigintnot null
net_centsbigintnot null
statuspaycheck_statusnot null, default 'calculated'
voided_by_paycheck_iduuidFKpaychecks.idThe reversing check, when this one was voided

Indexes: (pay_run_id, employee_id) unique

paycheck_lines

Every earning, deduction and tax on a stub, with the rate and hours that produced it.

Columns of the paycheck_lines table
ColumnTypeConstraintsReferencesDescription
iduuidPK
paycheck_iduuidFK, not nullpaychecks.id
kindline_kindnot null
codevarchar(30)not nullEarning code, deduction code or tax code
descriptionvarchar(200)not null
hoursnumeric(6,2)
rate_centsbigint
amount_centsbigintnot null
employer_paidbooleannot null, default falseEmployer taxes and matches: cost to the company, not taken from the employee
tax_jurisdiction_iduuidFKtax_jurisdictions.id

direct_deposits

Net pay as it was actually split and sent. Bank account details are snapshotted by reference, so a later change never rewrites where money went.

Columns of the direct_deposits table
ColumnTypeConstraintsReferencesDescription
iduuidPK
paycheck_iduuidFK, not nullpaychecks.id
bank_account_iduuidFK, not nullbank_accounts.id
amount_centsbigintnot null
statusdeposit_statusnot null, default 'pending'
trace_numbervarchar(30)ACH trace number from the bank file
settled_attimestamptz

Enums (17)

jurisdiction_level
federal, state, local
employee_status
active, on_leave, terminated
bank_account_type
checking, savings
allocation_kind
amount, percent, remainder
employment_type
full_time, part_time, temporary, intern
flsa_status
exempt, non_exempt
pay_type
salary, hourly
calculation_kind
fixed_amount, percent_of_gross
earning_code
regular, overtime, double_time, holiday, shift_differential
request_status
pending, approved, denied, cancelled
ledger_kind
accrual, usage, adjustment, carryover, payout
pay_frequency
weekly, biweekly, semimonthly, monthly
run_kind
regular, off_cycle, correction, bonus
run_status
draft, calculated, approved, paid, voided
paycheck_status
calculated, approved, paid, voided
line_kind
earning, pre_tax_deduction, tax, post_tax_deduction, employer_contribution
deposit_status
pending, sent, settled, returned

Relationships (43)

  • bank_accounts.employee_id references employees.id many to one
  • compensation_records.approved_by references employees.id many to one
  • compensation_records.employee_id references employees.id many to one
  • compensation_records.pay_schedule_id references pay_schedules.id many to one
  • departments.legal_entity_id references legal_entities.id many to one
  • departments.parent_id references departments.id many to one
  • direct_deposits.bank_account_id references bank_accounts.id many to one
  • direct_deposits.paycheck_id references paychecks.id many to one
  • emergency_contacts.employee_id references employees.id many to one
  • employee_addresses.employee_id references employees.id many to one
  • employee_deductions.deduction_type_id references deduction_types.id many to one
  • employee_deductions.employee_id references employees.id many to one
  • employees.legal_entity_id references legal_entities.id many to one
  • holidays.legal_entity_id references legal_entities.id many to one
  • job_assignments.department_id references departments.id many to one
  • job_assignments.employee_id references employees.id many to one
  • job_assignments.job_title_id references job_titles.id many to one
  • job_assignments.manager_id references employees.id many to one
  • job_assignments.work_location_id references work_locations.id many to one
  • leave_ledger.employee_id references employees.id many to one
  • leave_ledger.leave_request_id references leave_requests.id many to one
  • leave_ledger.leave_type_id references leave_types.id many to one
  • leave_ledger.pay_run_id references pay_runs.id many to one
  • leave_requests.decided_by references employees.id many to one
  • leave_requests.employee_id references employees.id many to one
  • leave_requests.leave_type_id references leave_types.id many to one
  • leave_types.legal_entity_id references legal_entities.id many to one
  • pay_periods.pay_schedule_id references pay_schedules.id many to one
  • pay_runs.approved_by references employees.id many to one
  • pay_runs.pay_period_id references pay_periods.id many to one
  • pay_schedules.legal_entity_id references legal_entities.id many to one
  • paycheck_lines.paycheck_id references paychecks.id many to one
  • paycheck_lines.tax_jurisdiction_id references tax_jurisdictions.id many to one
  • paychecks.employee_id references employees.id many to one
  • paychecks.pay_run_id references pay_runs.id many to one
  • paychecks.voided_by_paycheck_id references paychecks.id many to one
  • tax_withholding_elections.employee_id references employees.id many to one
  • tax_withholding_elections.tax_jurisdiction_id references tax_jurisdictions.id many to one
  • time_entries.approved_by references employees.id many to one
  • time_entries.employee_id references employees.id many to one
  • time_entries.pay_run_id references pay_runs.id many to one
  • work_locations.legal_entity_id references legal_entities.id many to one
  • work_locations.tax_jurisdiction_id references tax_jurisdictions.id many to one

Employee and Payroll Management database schema in DBML

The schema as it is written: DBML, the text format DB Planner draws its diagrams from. Copy it into any DBML tool, or open it in the diagram above.

employee-payroll.dbml dbml
// Employee and Payroll Management
// Zones (header colors): organization (gray), employees (green),
// effective-dated employment terms (teal), time and leave (amber),
// deductions (blue), payroll runs (purple), money out (coral).

// ---------------------------------------------------------------------------
// ORGANIZATION
// ---------------------------------------------------------------------------

Table legal_entities [headercolor: #90a4ae] {
  id uuid [pk]
  name varchar(200) [not null]
  tax_id varchar(30) [not null, unique, note: 'Employer identification number (e.g. US EIN)']
  country char(2) [not null]
  Note: 'The company that legally employs and pays people. A group with two subsidiaries runs two payrolls.'
}

Table departments [headercolor: #90a4ae] {
  id uuid [pk]
  legal_entity_id uuid [not null, ref: > legal_entities.id]
  parent_id uuid [ref: > departments.id, note: 'Departments nest; the root has no parent']
  name varchar(200) [not null]
  cost_center varchar(30)
  Note: 'The org chart. Labor cost reports roll up through parent_id.'
}

Table tax_jurisdictions [headercolor: #90a4ae] {
  id uuid [pk]
  code varchar(20) [not null, unique, note: 'e.g. US-FED, US-CA, US-NY-NYC']
  name varchar(200) [not null]
  level jurisdiction_level [not null]
  Note: 'Where taxes are owed. Work locations and withholding elections point here; the tax rules themselves live in your tax engine.'
}

Table work_locations [headercolor: #90a4ae] {
  id uuid [pk]
  legal_entity_id uuid [not null, ref: > legal_entities.id]
  name varchar(200) [not null]
  address_line1 varchar(200)
  city varchar(120)
  region varchar(60)
  country char(2) [not null]
  tax_jurisdiction_id uuid [not null, ref: > tax_jurisdictions.id]
  is_remote boolean [not null, default: false]
  Note: 'Offices and remote work sites. Where someone works drives which taxes apply.'
}

Table job_titles [headercolor: #90a4ae] {
  id uuid [pk]
  name varchar(120) [not null, unique]
  job_family varchar(60)
  pay_grade varchar(10)
  Note: 'The catalog of roles. Who holds which one, and since when, is in job_assignments.'
}

Table holidays [headercolor: #90a4ae] {
  id uuid [pk]
  legal_entity_id uuid [not null, ref: > legal_entities.id]
  holiday_date date [not null]
  name varchar(100) [not null]

  indexes {
    (legal_entity_id, holiday_date) [unique]
  }

  Note: 'Paid holidays per employer. Holiday pay and leave calculations read this calendar.'
}

// ---------------------------------------------------------------------------
// EMPLOYEES
// ---------------------------------------------------------------------------

Table employees [headercolor: #8bc34a] {
  id uuid [pk]
  employee_number varchar(20) [not null, unique]
  legal_entity_id uuid [not null, ref: > legal_entities.id]
  legal_first_name varchar(100) [not null]
  legal_last_name varchar(100) [not null]
  preferred_name varchar(100)
  work_email varchar(254) [unique]
  date_of_birth date [not null]
  national_id_last4 char(4) [note: 'Last four of the SSN or equivalent for matching; the full number belongs in a vault, not this table']
  hire_date date [not null]
  termination_date date
  status employee_status [not null, default: 'active']
  Note: 'One row per person employed by an entity. A rehire reuses the row, so tenure and history survive.'
}

Table employee_addresses [headercolor: #8bc34a] {
  id uuid [pk]
  employee_id uuid [not null, ref: > employees.id]
  address_line1 varchar(200) [not null]
  city varchar(120) [not null]
  region varchar(60)
  postal_code varchar(20)
  country char(2) [not null]
  effective_from date [not null]
  effective_to date [note: 'Null for the current address']
  Note: 'Home address history. Residence drives some local taxes, so the address on a past pay date has to be recoverable.'
}

Table emergency_contacts [headercolor: #8bc34a] {
  id uuid [pk]
  employee_id uuid [not null, ref: > employees.id]
  name varchar(200) [not null]
  relationship varchar(60) [not null]
  phone varchar(30) [not null]
  Note: 'Who to call.'
}

Table bank_accounts [headercolor: #8bc34a] {
  id uuid [pk]
  employee_id uuid [not null, ref: > employees.id]
  routing_number varchar(20) [not null]
  account_token varchar(200) [not null, note: 'Tokenized account number from your payments provider']
  account_last4 char(4) [not null]
  account_type bank_account_type [not null]
  allocation allocation_kind [not null, note: 'Fixed amount, percentage, or the remainder']
  allocation_value numeric(12,2) [note: 'Cents for amount, 0-100 for percent, null for remainder']
  priority smallint [not null, default: 1, note: 'Order in which a split paycheck is distributed']
  active boolean [not null, default: true]
  Note: 'Direct deposit accounts. Net pay is split across active accounts by priority, with one remainder account taking the rest.'
}

// ---------------------------------------------------------------------------
// EMPLOYMENT TERMS: effective-dated, never overwritten
// ---------------------------------------------------------------------------

Table job_assignments [headercolor: #4cd0b0] {
  id uuid [pk]
  employee_id uuid [not null, ref: > employees.id]
  job_title_id uuid [not null, ref: > job_titles.id]
  department_id uuid [not null, ref: > departments.id]
  work_location_id uuid [not null, ref: > work_locations.id]
  manager_id uuid [ref: > employees.id]
  employment_type employment_type [not null]
  flsa_status flsa_status [not null, note: 'Exempt or non-exempt from overtime']
  fte numeric(3,2) [not null, default: 1.00, note: 'Full-time equivalent, e.g. 0.50 for half time']
  effective_from date [not null]
  effective_to date [note: 'Null for the current assignment']
  reason varchar(100) [note: 'e.g. Hire, Promotion, Transfer']

  indexes {
    (employee_id, effective_from) [unique]
  }

  Note: 'Role, department, manager and location over time. A promotion closes one row and opens the next.'
}

Table compensation_records [headercolor: #4cd0b0] {
  id uuid [pk]
  employee_id uuid [not null, ref: > employees.id]
  pay_type pay_type [not null]
  amount_cents bigint [not null, note: 'Annual salary for salaried, hourly rate for hourly']
  currency char(3) [not null]
  pay_schedule_id uuid [not null, ref: > pay_schedules.id]
  effective_from date [not null]
  effective_to date
  reason varchar(100) [note: 'e.g. Annual review, Market adjustment']
  approved_by uuid [ref: > employees.id]

  indexes {
    (employee_id, effective_from) [unique]
  }

  Note: 'Pay rate history. A raise is a new row from its effective date, so a retro adjustment can be computed exactly.'
}

Table tax_withholding_elections [headercolor: #4cd0b0] {
  id uuid [pk]
  employee_id uuid [not null, ref: > employees.id]
  tax_jurisdiction_id uuid [not null, ref: > tax_jurisdictions.id]
  filing_status varchar(30) [not null, note: 'As declared on the W-4 or local equivalent']
  allowances smallint [not null, default: 0]
  extra_withholding_cents integer [not null, default: 0]
  exempt boolean [not null, default: false]
  effective_from date [not null]
  effective_to date
  Note: 'Withholding choices per jurisdiction, effective-dated like everything else that changes a paycheck.'
}

Table employee_deductions [headercolor: #4cd0b0] {
  id uuid [pk]
  employee_id uuid [not null, ref: > employees.id]
  deduction_type_id uuid [not null, ref: > deduction_types.id]
  calculation calculation_kind [not null]
  value numeric(12,2) [not null, note: 'Cents per pay period, or percent of gross']
  annual_limit_cents bigint [note: 'e.g. the 401(k) contribution limit']
  effective_from date [not null]
  effective_to date
  Note: 'An employee enrolled in a deduction: benefits, retirement, garnishments.'
}

// ---------------------------------------------------------------------------
// TIME AND LEAVE
// ---------------------------------------------------------------------------

Table time_entries [headercolor: #f5b74c] {
  id uuid [pk]
  employee_id uuid [not null, ref: > employees.id]
  work_date date [not null]
  hours numeric(5,2) [not null]
  earning_code earning_code [not null, default: 'regular']
  approved_by uuid [ref: > employees.id]
  approved_at timestamptz
  pay_run_id uuid [ref: > pay_runs.id, note: 'Set when paid, so hours are never paid twice']

  indexes {
    (employee_id, work_date)
  }

  Note: 'Hours worked by non-exempt staff. Only approved, unpaid entries are picked up by the next run.'
}

Table leave_types [headercolor: #f5b74c] {
  id uuid [pk]
  legal_entity_id uuid [not null, ref: > legal_entities.id]
  code varchar(20) [not null, note: 'e.g. PTO, SICK, PARENTAL']
  name varchar(100) [not null]
  accrual_hours_per_period numeric(5,2) [not null, default: 0]
  max_balance_hours numeric(6,2)
  paid boolean [not null, default: true]
  Note: 'Kinds of time off and how they accrue.'
}

Table leave_requests [headercolor: #f5b74c] {
  id uuid [pk]
  employee_id uuid [not null, ref: > employees.id]
  leave_type_id uuid [not null, ref: > leave_types.id]
  starts_on date [not null]
  ends_on date [not null]
  hours numeric(6,2) [not null]
  status request_status [not null, default: 'pending']
  decided_by uuid [ref: > employees.id]
  decided_at timestamptz
  Note: 'Time off asked for. Approval writes a usage entry to the leave ledger.'
}

Table leave_ledger [headercolor: #f5b74c] {
  id uuid [pk]
  employee_id uuid [not null, ref: > employees.id]
  leave_type_id uuid [not null, ref: > leave_types.id]
  kind ledger_kind [not null]
  hours numeric(6,2) [not null, note: 'Positive for accruals, negative for usage']
  effective_on date [not null]
  leave_request_id uuid [ref: > leave_requests.id]
  pay_run_id uuid [ref: > pay_runs.id, note: 'The run that accrued it, for accrual entries']
  memo varchar(200)
  Note: 'Every accrual, use and adjustment. A balance is the sum of this ledger, never a column someone can overwrite.'
}

// ---------------------------------------------------------------------------
// DEDUCTION CATALOG
// ---------------------------------------------------------------------------

Table deduction_types [headercolor: #4c8bf5] {
  id uuid [pk]
  code varchar(20) [not null, unique, note: 'e.g. 401K, MEDICAL, DENTAL, HSA, GARNISH']
  name varchar(100) [not null]
  pre_tax boolean [not null, note: 'Reduces taxable wages when true']
  employer_match_percent numeric(5,2) [note: 'Employer contribution as a percent of the employee amount']
  Note: 'Benefit and deduction plans an employee can be enrolled in.'
}

// ---------------------------------------------------------------------------
// PAYROLL RUNS: immutable once approved
// ---------------------------------------------------------------------------

Table pay_schedules [headercolor: #b98cf0] {
  id uuid [pk]
  legal_entity_id uuid [not null, ref: > legal_entities.id]
  name varchar(60) [not null]
  frequency pay_frequency [not null]
  anchor_date date [not null, note: 'A known period start; every other period is computed from it']
  Note: 'How often a group is paid.'
}

Table pay_periods [headercolor: #b98cf0] {
  id uuid [pk]
  pay_schedule_id uuid [not null, ref: > pay_schedules.id]
  starts_on date [not null]
  ends_on date [not null]
  pay_date date [not null]

  indexes {
    (pay_schedule_id, starts_on) [unique]
  }

  Note: 'The concrete periods of a schedule.'
}

Table pay_runs [headercolor: #b98cf0] {
  id uuid [pk]
  pay_period_id uuid [not null, ref: > pay_periods.id]
  kind run_kind [not null, default: 'regular']
  status run_status [not null, default: 'draft']
  calculated_at timestamptz
  approved_by uuid [ref: > employees.id]
  approved_at timestamptz
  paid_at timestamptz
  Note: 'One payroll calculation. Drafts can be recalculated; once approved, a mistake is fixed by an off-cycle or correction run, never by editing this one.'
}

Table paychecks [headercolor: #b98cf0] {
  id uuid [pk]
  pay_run_id uuid [not null, ref: > pay_runs.id]
  employee_id uuid [not null, ref: > employees.id]
  gross_cents bigint [not null]
  taxes_cents bigint [not null]
  deductions_cents bigint [not null]
  net_cents bigint [not null]
  status paycheck_status [not null, default: 'calculated']
  voided_by_paycheck_id uuid [ref: > paychecks.id, note: 'The reversing check, when this one was voided']

  indexes {
    (pay_run_id, employee_id) [unique]
  }

  Note: 'One employee in one run: the pay stub. Totals are snapshots of the lines below, frozen at approval.'
}

Table paycheck_lines [headercolor: #b98cf0] {
  id uuid [pk]
  paycheck_id uuid [not null, ref: > paychecks.id]
  kind line_kind [not null]
  code varchar(30) [not null, note: 'Earning code, deduction code or tax code']
  description varchar(200) [not null]
  hours numeric(6,2)
  rate_cents bigint
  amount_cents bigint [not null]
  employer_paid boolean [not null, default: false, note: 'Employer taxes and matches: cost to the company, not taken from the employee']
  tax_jurisdiction_id uuid [ref: > tax_jurisdictions.id]
  Note: 'Every earning, deduction and tax on a stub, with the rate and hours that produced it.'
}

// ---------------------------------------------------------------------------
// MONEY OUT
// ---------------------------------------------------------------------------

Table direct_deposits [headercolor: #f07178] {
  id uuid [pk]
  paycheck_id uuid [not null, ref: > paychecks.id]
  bank_account_id uuid [not null, ref: > bank_accounts.id]
  amount_cents bigint [not null]
  status deposit_status [not null, default: 'pending']
  trace_number varchar(30) [note: 'ACH trace number from the bank file']
  settled_at timestamptz
  Note: 'Net pay as it was actually split and sent. Bank account details are snapshotted by reference, so a later change never rewrites where money went.'
}

// ---------------------------------------------------------------------------
// ENUMS
// ---------------------------------------------------------------------------

Enum jurisdiction_level {
  federal
  state
  local
}

Enum employee_status {
  active
  on_leave
  terminated
}

Enum bank_account_type {
  checking
  savings
}

Enum allocation_kind {
  amount
  percent
  remainder
}

Enum employment_type {
  full_time
  part_time
  temporary
  intern
}

Enum flsa_status {
  exempt
  non_exempt
}

Enum pay_type {
  salary
  hourly
}

Enum calculation_kind {
  fixed_amount
  percent_of_gross
}

Enum earning_code {
  regular
  overtime
  double_time
  holiday
  shift_differential
}

Enum request_status {
  pending
  approved
  denied
  cancelled
}

Enum ledger_kind {
  accrual
  usage
  adjustment
  carryover
  payout
}

Enum pay_frequency {
  weekly
  biweekly
  semimonthly
  monthly
}

Enum run_kind {
  regular
  off_cycle
  correction
  bonus
}

Enum run_status {
  draft
  calculated
  approved
  paid
  voided
}

Enum paycheck_status {
  calculated
  approved
  paid
  voided
}

Enum line_kind {
  earning
  pre_tax_deduction
  tax
  post_tax_deduction
  employer_contribution
}

Enum deposit_status {
  pending
  sent
  settled
  returned
}

Employee and Payroll Management database schema as SQL

The same schema as CREATE TABLE statements, exported by DB Planner. PostgreSQL is shown first; MySQL and SQL Server are below it.

PostgreSQL
postgresql.sql PostgreSQL
CREATE TYPE "jurisdiction_level" AS ENUM (
  'federal',
  'state',
  'local'
);

CREATE TYPE "employee_status" AS ENUM (
  'active',
  'on_leave',
  'terminated'
);

CREATE TYPE "bank_account_type" AS ENUM (
  'checking',
  'savings'
);

CREATE TYPE "allocation_kind" AS ENUM (
  'amount',
  'percent',
  'remainder'
);

CREATE TYPE "employment_type" AS ENUM (
  'full_time',
  'part_time',
  'temporary',
  'intern'
);

CREATE TYPE "flsa_status" AS ENUM (
  'exempt',
  'non_exempt'
);

CREATE TYPE "pay_type" AS ENUM (
  'salary',
  'hourly'
);

CREATE TYPE "calculation_kind" AS ENUM (
  'fixed_amount',
  'percent_of_gross'
);

CREATE TYPE "earning_code" AS ENUM (
  'regular',
  'overtime',
  'double_time',
  'holiday',
  'shift_differential'
);

CREATE TYPE "request_status" AS ENUM (
  'pending',
  'approved',
  'denied',
  'cancelled'
);

CREATE TYPE "ledger_kind" AS ENUM (
  'accrual',
  'usage',
  'adjustment',
  'carryover',
  'payout'
);

CREATE TYPE "pay_frequency" AS ENUM (
  'weekly',
  'biweekly',
  'semimonthly',
  'monthly'
);

CREATE TYPE "run_kind" AS ENUM (
  'regular',
  'off_cycle',
  'correction',
  'bonus'
);

CREATE TYPE "run_status" AS ENUM (
  'draft',
  'calculated',
  'approved',
  'paid',
  'voided'
);

CREATE TYPE "paycheck_status" AS ENUM (
  'calculated',
  'approved',
  'paid',
  'voided'
);

CREATE TYPE "line_kind" AS ENUM (
  'earning',
  'pre_tax_deduction',
  'tax',
  'post_tax_deduction',
  'employer_contribution'
);

CREATE TYPE "deposit_status" AS ENUM (
  'pending',
  'sent',
  'settled',
  'returned'
);

CREATE TABLE "legal_entities" (
  "id" uuid PRIMARY KEY,
  "name" varchar(200) NOT NULL,
  "tax_id" varchar(30) UNIQUE NOT NULL,
  "country" char(2) NOT NULL
);

CREATE TABLE "departments" (
  "id" uuid PRIMARY KEY,
  "legal_entity_id" uuid NOT NULL,
  "parent_id" uuid,
  "name" varchar(200) NOT NULL,
  "cost_center" varchar(30)
);

CREATE TABLE "tax_jurisdictions" (
  "id" uuid PRIMARY KEY,
  "code" varchar(20) UNIQUE NOT NULL,
  "name" varchar(200) NOT NULL,
  "level" jurisdiction_level NOT NULL
);

CREATE TABLE "work_locations" (
  "id" uuid PRIMARY KEY,
  "legal_entity_id" uuid NOT NULL,
  "name" varchar(200) NOT NULL,
  "address_line1" varchar(200),
  "city" varchar(120),
  "region" varchar(60),
  "country" char(2) NOT NULL,
  "tax_jurisdiction_id" uuid NOT NULL,
  "is_remote" boolean NOT NULL DEFAULT false
);

CREATE TABLE "job_titles" (
  "id" uuid PRIMARY KEY,
  "name" varchar(120) UNIQUE NOT NULL,
  "job_family" varchar(60),
  "pay_grade" varchar(10)
);

CREATE TABLE "holidays" (
  "id" uuid PRIMARY KEY,
  "legal_entity_id" uuid NOT NULL,
  "holiday_date" date NOT NULL,
  "name" varchar(100) NOT NULL
);

CREATE TABLE "employees" (
  "id" uuid PRIMARY KEY,
  "employee_number" varchar(20) UNIQUE NOT NULL,
  "legal_entity_id" uuid NOT NULL,
  "legal_first_name" varchar(100) NOT NULL,
  "legal_last_name" varchar(100) NOT NULL,
  "preferred_name" varchar(100),
  "work_email" varchar(254) UNIQUE,
  "date_of_birth" date NOT NULL,
  "national_id_last4" char(4),
  "hire_date" date NOT NULL,
  "termination_date" date,
  "status" employee_status NOT NULL DEFAULT 'active'
);

CREATE TABLE "employee_addresses" (
  "id" uuid PRIMARY KEY,
  "employee_id" uuid NOT NULL,
  "address_line1" varchar(200) NOT NULL,
  "city" varchar(120) NOT NULL,
  "region" varchar(60),
  "postal_code" varchar(20),
  "country" char(2) NOT NULL,
  "effective_from" date NOT NULL,
  "effective_to" date
);

CREATE TABLE "emergency_contacts" (
  "id" uuid PRIMARY KEY,
  "employee_id" uuid NOT NULL,
  "name" varchar(200) NOT NULL,
  "relationship" varchar(60) NOT NULL,
  "phone" varchar(30) NOT NULL
);

CREATE TABLE "bank_accounts" (
  "id" uuid PRIMARY KEY,
  "employee_id" uuid NOT NULL,
  "routing_number" varchar(20) NOT NULL,
  "account_token" varchar(200) NOT NULL,
  "account_last4" char(4) NOT NULL,
  "account_type" bank_account_type NOT NULL,
  "allocation" allocation_kind NOT NULL,
  "allocation_value" numeric(12,2),
  "priority" smallint NOT NULL DEFAULT 1,
  "active" boolean NOT NULL DEFAULT true
);

CREATE TABLE "job_assignments" (
  "id" uuid PRIMARY KEY,
  "employee_id" uuid NOT NULL,
  "job_title_id" uuid NOT NULL,
  "department_id" uuid NOT NULL,
  "work_location_id" uuid NOT NULL,
  "manager_id" uuid,
  "employment_type" employment_type NOT NULL,
  "flsa_status" flsa_status NOT NULL,
  "fte" numeric(3,2) NOT NULL DEFAULT 1,
  "effective_from" date NOT NULL,
  "effective_to" date,
  "reason" varchar(100)
);

CREATE TABLE "compensation_records" (
  "id" uuid PRIMARY KEY,
  "employee_id" uuid NOT NULL,
  "pay_type" pay_type NOT NULL,
  "amount_cents" bigint NOT NULL,
  "currency" char(3) NOT NULL,
  "pay_schedule_id" uuid NOT NULL,
  "effective_from" date NOT NULL,
  "effective_to" date,
  "reason" varchar(100),
  "approved_by" uuid
);

CREATE TABLE "tax_withholding_elections" (
  "id" uuid PRIMARY KEY,
  "employee_id" uuid NOT NULL,
  "tax_jurisdiction_id" uuid NOT NULL,
  "filing_status" varchar(30) NOT NULL,
  "allowances" smallint NOT NULL DEFAULT 0,
  "extra_withholding_cents" integer NOT NULL DEFAULT 0,
  "exempt" boolean NOT NULL DEFAULT false,
  "effective_from" date NOT NULL,
  "effective_to" date
);

CREATE TABLE "employee_deductions" (
  "id" uuid PRIMARY KEY,
  "employee_id" uuid NOT NULL,
  "deduction_type_id" uuid NOT NULL,
  "calculation" calculation_kind NOT NULL,
  "value" numeric(12,2) NOT NULL,
  "annual_limit_cents" bigint,
  "effective_from" date NOT NULL,
  "effective_to" date
);

CREATE TABLE "time_entries" (
  "id" uuid PRIMARY KEY,
  "employee_id" uuid NOT NULL,
  "work_date" date NOT NULL,
  "hours" numeric(5,2) NOT NULL,
  "earning_code" earning_code NOT NULL DEFAULT 'regular',
  "approved_by" uuid,
  "approved_at" timestamptz,
  "pay_run_id" uuid
);

CREATE TABLE "leave_types" (
  "id" uuid PRIMARY KEY,
  "legal_entity_id" uuid NOT NULL,
  "code" varchar(20) NOT NULL,
  "name" varchar(100) NOT NULL,
  "accrual_hours_per_period" numeric(5,2) NOT NULL DEFAULT 0,
  "max_balance_hours" numeric(6,2),
  "paid" boolean NOT NULL DEFAULT true
);

CREATE TABLE "leave_requests" (
  "id" uuid PRIMARY KEY,
  "employee_id" uuid NOT NULL,
  "leave_type_id" uuid NOT NULL,
  "starts_on" date NOT NULL,
  "ends_on" date NOT NULL,
  "hours" numeric(6,2) NOT NULL,
  "status" request_status NOT NULL DEFAULT 'pending',
  "decided_by" uuid,
  "decided_at" timestamptz
);

CREATE TABLE "leave_ledger" (
  "id" uuid PRIMARY KEY,
  "employee_id" uuid NOT NULL,
  "leave_type_id" uuid NOT NULL,
  "kind" ledger_kind NOT NULL,
  "hours" numeric(6,2) NOT NULL,
  "effective_on" date NOT NULL,
  "leave_request_id" uuid,
  "pay_run_id" uuid,
  "memo" varchar(200)
);

CREATE TABLE "deduction_types" (
  "id" uuid PRIMARY KEY,
  "code" varchar(20) UNIQUE NOT NULL,
  "name" varchar(100) NOT NULL,
  "pre_tax" boolean NOT NULL,
  "employer_match_percent" numeric(5,2)
);

CREATE TABLE "pay_schedules" (
  "id" uuid PRIMARY KEY,
  "legal_entity_id" uuid NOT NULL,
  "name" varchar(60) NOT NULL,
  "frequency" pay_frequency NOT NULL,
  "anchor_date" date NOT NULL
);

CREATE TABLE "pay_periods" (
  "id" uuid PRIMARY KEY,
  "pay_schedule_id" uuid NOT NULL,
  "starts_on" date NOT NULL,
  "ends_on" date NOT NULL,
  "pay_date" date NOT NULL
);

CREATE TABLE "pay_runs" (
  "id" uuid PRIMARY KEY,
  "pay_period_id" uuid NOT NULL,
  "kind" run_kind NOT NULL DEFAULT 'regular',
  "status" run_status NOT NULL DEFAULT 'draft',
  "calculated_at" timestamptz,
  "approved_by" uuid,
  "approved_at" timestamptz,
  "paid_at" timestamptz
);

CREATE TABLE "paychecks" (
  "id" uuid PRIMARY KEY,
  "pay_run_id" uuid NOT NULL,
  "employee_id" uuid NOT NULL,
  "gross_cents" bigint NOT NULL,
  "taxes_cents" bigint NOT NULL,
  "deductions_cents" bigint NOT NULL,
  "net_cents" bigint NOT NULL,
  "status" paycheck_status NOT NULL DEFAULT 'calculated',
  "voided_by_paycheck_id" uuid
);

CREATE TABLE "paycheck_lines" (
  "id" uuid PRIMARY KEY,
  "paycheck_id" uuid NOT NULL,
  "kind" line_kind NOT NULL,
  "code" varchar(30) NOT NULL,
  "description" varchar(200) NOT NULL,
  "hours" numeric(6,2),
  "rate_cents" bigint,
  "amount_cents" bigint NOT NULL,
  "employer_paid" boolean NOT NULL DEFAULT false,
  "tax_jurisdiction_id" uuid
);

CREATE TABLE "direct_deposits" (
  "id" uuid PRIMARY KEY,
  "paycheck_id" uuid NOT NULL,
  "bank_account_id" uuid NOT NULL,
  "amount_cents" bigint NOT NULL,
  "status" deposit_status NOT NULL DEFAULT 'pending',
  "trace_number" varchar(30),
  "settled_at" timestamptz
);

CREATE UNIQUE INDEX ON "holidays" ("legal_entity_id", "holiday_date");

CREATE UNIQUE INDEX ON "job_assignments" ("employee_id", "effective_from");

CREATE UNIQUE INDEX ON "compensation_records" ("employee_id", "effective_from");

CREATE INDEX ON "time_entries" ("employee_id", "work_date");

CREATE UNIQUE INDEX ON "pay_periods" ("pay_schedule_id", "starts_on");

CREATE UNIQUE INDEX ON "paychecks" ("pay_run_id", "employee_id");

COMMENT ON TABLE "legal_entities" IS 'The company that legally employs and pays people. A group with two subsidiaries runs two payrolls.';

COMMENT ON COLUMN "legal_entities"."tax_id" IS 'Employer identification number (e.g. US EIN)';

COMMENT ON TABLE "departments" IS 'The org chart. Labor cost reports roll up through parent_id.';

COMMENT ON COLUMN "departments"."parent_id" IS 'Departments nest; the root has no parent';

COMMENT ON TABLE "tax_jurisdictions" IS 'Where taxes are owed. Work locations and withholding elections point here; the tax rules themselves live in your tax engine.';

COMMENT ON COLUMN "tax_jurisdictions"."code" IS 'e.g. US-FED, US-CA, US-NY-NYC';

COMMENT ON TABLE "work_locations" IS 'Offices and remote work sites. Where someone works drives which taxes apply.';

COMMENT ON TABLE "job_titles" IS 'The catalog of roles. Who holds which one, and since when, is in job_assignments.';

COMMENT ON TABLE "holidays" IS 'Paid holidays per employer. Holiday pay and leave calculations read this calendar.';

COMMENT ON TABLE "employees" IS 'One row per person employed by an entity. A rehire reuses the row, so tenure and history survive.';

COMMENT ON COLUMN "employees"."national_id_last4" IS 'Last four of the SSN or equivalent for matching; the full number belongs in a vault, not this table';

COMMENT ON TABLE "employee_addresses" IS 'Home address history. Residence drives some local taxes, so the address on a past pay date has to be recoverable.';

COMMENT ON COLUMN "employee_addresses"."effective_to" IS 'Null for the current address';

COMMENT ON TABLE "emergency_contacts" IS 'Who to call.';

COMMENT ON TABLE "bank_accounts" IS 'Direct deposit accounts. Net pay is split across active accounts by priority, with one remainder account taking the rest.';

COMMENT ON COLUMN "bank_accounts"."account_token" IS 'Tokenized account number from your payments provider';

COMMENT ON COLUMN "bank_accounts"."allocation" IS 'Fixed amount, percentage, or the remainder';

COMMENT ON COLUMN "bank_accounts"."allocation_value" IS 'Cents for amount, 0-100 for percent, null for remainder';

COMMENT ON COLUMN "bank_accounts"."priority" IS 'Order in which a split paycheck is distributed';

COMMENT ON TABLE "job_assignments" IS 'Role, department, manager and location over time. A promotion closes one row and opens the next.';

COMMENT ON COLUMN "job_assignments"."flsa_status" IS 'Exempt or non-exempt from overtime';

COMMENT ON COLUMN "job_assignments"."fte" IS 'Full-time equivalent, e.g. 0.50 for half time';

COMMENT ON COLUMN "job_assignments"."effective_to" IS 'Null for the current assignment';

COMMENT ON COLUMN "job_assignments"."reason" IS 'e.g. Hire, Promotion, Transfer';

COMMENT ON TABLE "compensation_records" IS 'Pay rate history. A raise is a new row from its effective date, so a retro adjustment can be computed exactly.';

COMMENT ON COLUMN "compensation_records"."amount_cents" IS 'Annual salary for salaried, hourly rate for hourly';

COMMENT ON COLUMN "compensation_records"."reason" IS 'e.g. Annual review, Market adjustment';

COMMENT ON TABLE "tax_withholding_elections" IS 'Withholding choices per jurisdiction, effective-dated like everything else that changes a paycheck.';

COMMENT ON COLUMN "tax_withholding_elections"."filing_status" IS 'As declared on the W-4 or local equivalent';

COMMENT ON TABLE "employee_deductions" IS 'An employee enrolled in a deduction: benefits, retirement, garnishments.';

COMMENT ON COLUMN "employee_deductions"."value" IS 'Cents per pay period, or percent of gross';

COMMENT ON COLUMN "employee_deductions"."annual_limit_cents" IS 'e.g. the 401(k) contribution limit';

COMMENT ON TABLE "time_entries" IS 'Hours worked by non-exempt staff. Only approved, unpaid entries are picked up by the next run.';

COMMENT ON COLUMN "time_entries"."pay_run_id" IS 'Set when paid, so hours are never paid twice';

COMMENT ON TABLE "leave_types" IS 'Kinds of time off and how they accrue.';

COMMENT ON COLUMN "leave_types"."code" IS 'e.g. PTO, SICK, PARENTAL';

COMMENT ON TABLE "leave_requests" IS 'Time off asked for. Approval writes a usage entry to the leave ledger.';

COMMENT ON TABLE "leave_ledger" IS 'Every accrual, use and adjustment. A balance is the sum of this ledger, never a column someone can overwrite.';

COMMENT ON COLUMN "leave_ledger"."hours" IS 'Positive for accruals, negative for usage';

COMMENT ON COLUMN "leave_ledger"."pay_run_id" IS 'The run that accrued it, for accrual entries';

COMMENT ON TABLE "deduction_types" IS 'Benefit and deduction plans an employee can be enrolled in.';

COMMENT ON COLUMN "deduction_types"."code" IS 'e.g. 401K, MEDICAL, DENTAL, HSA, GARNISH';

COMMENT ON COLUMN "deduction_types"."pre_tax" IS 'Reduces taxable wages when true';

COMMENT ON COLUMN "deduction_types"."employer_match_percent" IS 'Employer contribution as a percent of the employee amount';

COMMENT ON TABLE "pay_schedules" IS 'How often a group is paid.';

COMMENT ON COLUMN "pay_schedules"."anchor_date" IS 'A known period start; every other period is computed from it';

COMMENT ON TABLE "pay_periods" IS 'The concrete periods of a schedule.';

COMMENT ON TABLE "pay_runs" IS 'One payroll calculation. Drafts can be recalculated; once approved, a mistake is fixed by an off-cycle or correction run, never by editing this one.';

COMMENT ON TABLE "paychecks" IS 'One employee in one run: the pay stub. Totals are snapshots of the lines below, frozen at approval.';

COMMENT ON COLUMN "paychecks"."voided_by_paycheck_id" IS 'The reversing check, when this one was voided';

COMMENT ON TABLE "paycheck_lines" IS 'Every earning, deduction and tax on a stub, with the rate and hours that produced it.';

COMMENT ON COLUMN "paycheck_lines"."code" IS 'Earning code, deduction code or tax code';

COMMENT ON COLUMN "paycheck_lines"."employer_paid" IS 'Employer taxes and matches: cost to the company, not taken from the employee';

COMMENT ON TABLE "direct_deposits" IS 'Net pay as it was actually split and sent. Bank account details are snapshotted by reference, so a later change never rewrites where money went.';

COMMENT ON COLUMN "direct_deposits"."trace_number" IS 'ACH trace number from the bank file';

ALTER TABLE "departments" ADD FOREIGN KEY ("legal_entity_id") REFERENCES "legal_entities" ("id");

ALTER TABLE "departments" ADD FOREIGN KEY ("parent_id") REFERENCES "departments" ("id");

ALTER TABLE "work_locations" ADD FOREIGN KEY ("legal_entity_id") REFERENCES "legal_entities" ("id");

ALTER TABLE "work_locations" ADD FOREIGN KEY ("tax_jurisdiction_id") REFERENCES "tax_jurisdictions" ("id");

ALTER TABLE "holidays" ADD FOREIGN KEY ("legal_entity_id") REFERENCES "legal_entities" ("id");

ALTER TABLE "employees" ADD FOREIGN KEY ("legal_entity_id") REFERENCES "legal_entities" ("id");

ALTER TABLE "employee_addresses" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "emergency_contacts" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "bank_accounts" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "job_assignments" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "job_assignments" ADD FOREIGN KEY ("job_title_id") REFERENCES "job_titles" ("id");

ALTER TABLE "job_assignments" ADD FOREIGN KEY ("department_id") REFERENCES "departments" ("id");

ALTER TABLE "job_assignments" ADD FOREIGN KEY ("work_location_id") REFERENCES "work_locations" ("id");

ALTER TABLE "job_assignments" ADD FOREIGN KEY ("manager_id") REFERENCES "employees" ("id");

ALTER TABLE "compensation_records" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "compensation_records" ADD FOREIGN KEY ("pay_schedule_id") REFERENCES "pay_schedules" ("id");

ALTER TABLE "compensation_records" ADD FOREIGN KEY ("approved_by") REFERENCES "employees" ("id");

ALTER TABLE "tax_withholding_elections" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "tax_withholding_elections" ADD FOREIGN KEY ("tax_jurisdiction_id") REFERENCES "tax_jurisdictions" ("id");

ALTER TABLE "employee_deductions" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "employee_deductions" ADD FOREIGN KEY ("deduction_type_id") REFERENCES "deduction_types" ("id");

ALTER TABLE "time_entries" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "time_entries" ADD FOREIGN KEY ("approved_by") REFERENCES "employees" ("id");

ALTER TABLE "time_entries" ADD FOREIGN KEY ("pay_run_id") REFERENCES "pay_runs" ("id");

ALTER TABLE "leave_types" ADD FOREIGN KEY ("legal_entity_id") REFERENCES "legal_entities" ("id");

ALTER TABLE "leave_requests" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "leave_requests" ADD FOREIGN KEY ("leave_type_id") REFERENCES "leave_types" ("id");

ALTER TABLE "leave_requests" ADD FOREIGN KEY ("decided_by") REFERENCES "employees" ("id");

ALTER TABLE "leave_ledger" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "leave_ledger" ADD FOREIGN KEY ("leave_type_id") REFERENCES "leave_types" ("id");

ALTER TABLE "leave_ledger" ADD FOREIGN KEY ("leave_request_id") REFERENCES "leave_requests" ("id");

ALTER TABLE "leave_ledger" ADD FOREIGN KEY ("pay_run_id") REFERENCES "pay_runs" ("id");

ALTER TABLE "pay_schedules" ADD FOREIGN KEY ("legal_entity_id") REFERENCES "legal_entities" ("id");

ALTER TABLE "pay_periods" ADD FOREIGN KEY ("pay_schedule_id") REFERENCES "pay_schedules" ("id");

ALTER TABLE "pay_runs" ADD FOREIGN KEY ("pay_period_id") REFERENCES "pay_periods" ("id");

ALTER TABLE "pay_runs" ADD FOREIGN KEY ("approved_by") REFERENCES "employees" ("id");

ALTER TABLE "paychecks" ADD FOREIGN KEY ("pay_run_id") REFERENCES "pay_runs" ("id");

ALTER TABLE "paychecks" ADD FOREIGN KEY ("employee_id") REFERENCES "employees" ("id");

ALTER TABLE "paychecks" ADD FOREIGN KEY ("voided_by_paycheck_id") REFERENCES "paychecks" ("id");

ALTER TABLE "paycheck_lines" ADD FOREIGN KEY ("paycheck_id") REFERENCES "paychecks" ("id");

ALTER TABLE "paycheck_lines" ADD FOREIGN KEY ("tax_jurisdiction_id") REFERENCES "tax_jurisdictions" ("id");

ALTER TABLE "direct_deposits" ADD FOREIGN KEY ("paycheck_id") REFERENCES "paychecks" ("id");

ALTER TABLE "direct_deposits" ADD FOREIGN KEY ("bank_account_id") REFERENCES "bank_accounts" ("id");
MySQL
mysql.sql MySQL
CREATE TABLE `legal_entities` (
  `id` uuid PRIMARY KEY,
  `name` varchar(200) NOT NULL,
  `tax_id` varchar(30) UNIQUE NOT NULL COMMENT 'Employer identification number (e.g. US EIN)',
  `country` char(2) NOT NULL
);

CREATE TABLE `departments` (
  `id` uuid PRIMARY KEY,
  `legal_entity_id` uuid NOT NULL,
  `parent_id` uuid COMMENT 'Departments nest; the root has no parent',
  `name` varchar(200) NOT NULL,
  `cost_center` varchar(30)
);

CREATE TABLE `tax_jurisdictions` (
  `id` uuid PRIMARY KEY,
  `code` varchar(20) UNIQUE NOT NULL COMMENT 'e.g. US-FED, US-CA, US-NY-NYC',
  `name` varchar(200) NOT NULL,
  `level` ENUM ('federal', 'state', 'local') NOT NULL
);

CREATE TABLE `work_locations` (
  `id` uuid PRIMARY KEY,
  `legal_entity_id` uuid NOT NULL,
  `name` varchar(200) NOT NULL,
  `address_line1` varchar(200),
  `city` varchar(120),
  `region` varchar(60),
  `country` char(2) NOT NULL,
  `tax_jurisdiction_id` uuid NOT NULL,
  `is_remote` boolean NOT NULL DEFAULT false
);

CREATE TABLE `job_titles` (
  `id` uuid PRIMARY KEY,
  `name` varchar(120) UNIQUE NOT NULL,
  `job_family` varchar(60),
  `pay_grade` varchar(10)
);

CREATE TABLE `holidays` (
  `id` uuid PRIMARY KEY,
  `legal_entity_id` uuid NOT NULL,
  `holiday_date` date NOT NULL,
  `name` varchar(100) NOT NULL
);

CREATE TABLE `employees` (
  `id` uuid PRIMARY KEY,
  `employee_number` varchar(20) UNIQUE NOT NULL,
  `legal_entity_id` uuid NOT NULL,
  `legal_first_name` varchar(100) NOT NULL,
  `legal_last_name` varchar(100) NOT NULL,
  `preferred_name` varchar(100),
  `work_email` varchar(254) UNIQUE,
  `date_of_birth` date NOT NULL,
  `national_id_last4` char(4) COMMENT 'Last four of the SSN or equivalent for matching; the full number belongs in a vault, not this table',
  `hire_date` date NOT NULL,
  `termination_date` date,
  `status` ENUM ('active', 'on_leave', 'terminated') NOT NULL DEFAULT 'active'
);

CREATE TABLE `employee_addresses` (
  `id` uuid PRIMARY KEY,
  `employee_id` uuid NOT NULL,
  `address_line1` varchar(200) NOT NULL,
  `city` varchar(120) NOT NULL,
  `region` varchar(60),
  `postal_code` varchar(20),
  `country` char(2) NOT NULL,
  `effective_from` date NOT NULL,
  `effective_to` date COMMENT 'Null for the current address'
);

CREATE TABLE `emergency_contacts` (
  `id` uuid PRIMARY KEY,
  `employee_id` uuid NOT NULL,
  `name` varchar(200) NOT NULL,
  `relationship` varchar(60) NOT NULL,
  `phone` varchar(30) NOT NULL
);

CREATE TABLE `bank_accounts` (
  `id` uuid PRIMARY KEY,
  `employee_id` uuid NOT NULL,
  `routing_number` varchar(20) NOT NULL,
  `account_token` varchar(200) NOT NULL COMMENT 'Tokenized account number from your payments provider',
  `account_last4` char(4) NOT NULL,
  `account_type` ENUM ('checking', 'savings') NOT NULL,
  `allocation` ENUM ('amount', 'percent', 'remainder') NOT NULL COMMENT 'Fixed amount, percentage, or the remainder',
  `allocation_value` numeric(12,2) COMMENT 'Cents for amount, 0-100 for percent, null for remainder',
  `priority` smallint NOT NULL DEFAULT 1 COMMENT 'Order in which a split paycheck is distributed',
  `active` boolean NOT NULL DEFAULT true
);

CREATE TABLE `job_assignments` (
  `id` uuid PRIMARY KEY,
  `employee_id` uuid NOT NULL,
  `job_title_id` uuid NOT NULL,
  `department_id` uuid NOT NULL,
  `work_location_id` uuid NOT NULL,
  `manager_id` uuid,
  `employment_type` ENUM ('full_time', 'part_time', 'temporary', 'intern') NOT NULL,
  `flsa_status` ENUM ('exempt', 'non_exempt') NOT NULL COMMENT 'Exempt or non-exempt from overtime',
  `fte` numeric(3,2) NOT NULL DEFAULT 1 COMMENT 'Full-time equivalent, e.g. 0.50 for half time',
  `effective_from` date NOT NULL,
  `effective_to` date COMMENT 'Null for the current assignment',
  `reason` varchar(100) COMMENT 'e.g. Hire, Promotion, Transfer'
);

CREATE TABLE `compensation_records` (
  `id` uuid PRIMARY KEY,
  `employee_id` uuid NOT NULL,
  `pay_type` ENUM ('salary', 'hourly') NOT NULL,
  `amount_cents` bigint NOT NULL COMMENT 'Annual salary for salaried, hourly rate for hourly',
  `currency` char(3) NOT NULL,
  `pay_schedule_id` uuid NOT NULL,
  `effective_from` date NOT NULL,
  `effective_to` date,
  `reason` varchar(100) COMMENT 'e.g. Annual review, Market adjustment',
  `approved_by` uuid
);

CREATE TABLE `tax_withholding_elections` (
  `id` uuid PRIMARY KEY,
  `employee_id` uuid NOT NULL,
  `tax_jurisdiction_id` uuid NOT NULL,
  `filing_status` varchar(30) NOT NULL COMMENT 'As declared on the W-4 or local equivalent',
  `allowances` smallint NOT NULL DEFAULT 0,
  `extra_withholding_cents` integer NOT NULL DEFAULT 0,
  `exempt` boolean NOT NULL DEFAULT false,
  `effective_from` date NOT NULL,
  `effective_to` date
);

CREATE TABLE `employee_deductions` (
  `id` uuid PRIMARY KEY,
  `employee_id` uuid NOT NULL,
  `deduction_type_id` uuid NOT NULL,
  `calculation` ENUM ('fixed_amount', 'percent_of_gross') NOT NULL,
  `value` numeric(12,2) NOT NULL COMMENT 'Cents per pay period, or percent of gross',
  `annual_limit_cents` bigint COMMENT 'e.g. the 401(k) contribution limit',
  `effective_from` date NOT NULL,
  `effective_to` date
);

CREATE TABLE `time_entries` (
  `id` uuid PRIMARY KEY,
  `employee_id` uuid NOT NULL,
  `work_date` date NOT NULL,
  `hours` numeric(5,2) NOT NULL,
  `earning_code` ENUM ('regular', 'overtime', 'double_time', 'holiday', 'shift_differential') NOT NULL DEFAULT 'regular',
  `approved_by` uuid,
  `approved_at` timestamptz,
  `pay_run_id` uuid COMMENT 'Set when paid, so hours are never paid twice'
);

CREATE TABLE `leave_types` (
  `id` uuid PRIMARY KEY,
  `legal_entity_id` uuid NOT NULL,
  `code` varchar(20) NOT NULL COMMENT 'e.g. PTO, SICK, PARENTAL',
  `name` varchar(100) NOT NULL,
  `accrual_hours_per_period` numeric(5,2) NOT NULL DEFAULT 0,
  `max_balance_hours` numeric(6,2),
  `paid` boolean NOT NULL DEFAULT true
);

CREATE TABLE `leave_requests` (
  `id` uuid PRIMARY KEY,
  `employee_id` uuid NOT NULL,
  `leave_type_id` uuid NOT NULL,
  `starts_on` date NOT NULL,
  `ends_on` date NOT NULL,
  `hours` numeric(6,2) NOT NULL,
  `status` ENUM ('pending', 'approved', 'denied', 'cancelled') NOT NULL DEFAULT 'pending',
  `decided_by` uuid,
  `decided_at` timestamptz
);

CREATE TABLE `leave_ledger` (
  `id` uuid PRIMARY KEY,
  `employee_id` uuid NOT NULL,
  `leave_type_id` uuid NOT NULL,
  `kind` ENUM ('accrual', 'usage', 'adjustment', 'carryover', 'payout') NOT NULL,
  `hours` numeric(6,2) NOT NULL COMMENT 'Positive for accruals, negative for usage',
  `effective_on` date NOT NULL,
  `leave_request_id` uuid,
  `pay_run_id` uuid COMMENT 'The run that accrued it, for accrual entries',
  `memo` varchar(200)
);

CREATE TABLE `deduction_types` (
  `id` uuid PRIMARY KEY,
  `code` varchar(20) UNIQUE NOT NULL COMMENT 'e.g. 401K, MEDICAL, DENTAL, HSA, GARNISH',
  `name` varchar(100) NOT NULL,
  `pre_tax` boolean NOT NULL COMMENT 'Reduces taxable wages when true',
  `employer_match_percent` numeric(5,2) COMMENT 'Employer contribution as a percent of the employee amount'
);

CREATE TABLE `pay_schedules` (
  `id` uuid PRIMARY KEY,
  `legal_entity_id` uuid NOT NULL,
  `name` varchar(60) NOT NULL,
  `frequency` ENUM ('weekly', 'biweekly', 'semimonthly', 'monthly') NOT NULL,
  `anchor_date` date NOT NULL COMMENT 'A known period start; every other period is computed from it'
);

CREATE TABLE `pay_periods` (
  `id` uuid PRIMARY KEY,
  `pay_schedule_id` uuid NOT NULL,
  `starts_on` date NOT NULL,
  `ends_on` date NOT NULL,
  `pay_date` date NOT NULL
);

CREATE TABLE `pay_runs` (
  `id` uuid PRIMARY KEY,
  `pay_period_id` uuid NOT NULL,
  `kind` ENUM ('regular', 'off_cycle', 'correction', 'bonus') NOT NULL DEFAULT 'regular',
  `status` ENUM ('draft', 'calculated', 'approved', 'paid', 'voided') NOT NULL DEFAULT 'draft',
  `calculated_at` timestamptz,
  `approved_by` uuid,
  `approved_at` timestamptz,
  `paid_at` timestamptz
);

CREATE TABLE `paychecks` (
  `id` uuid PRIMARY KEY,
  `pay_run_id` uuid NOT NULL,
  `employee_id` uuid NOT NULL,
  `gross_cents` bigint NOT NULL,
  `taxes_cents` bigint NOT NULL,
  `deductions_cents` bigint NOT NULL,
  `net_cents` bigint NOT NULL,
  `status` ENUM ('calculated', 'approved', 'paid', 'voided') NOT NULL DEFAULT 'calculated',
  `voided_by_paycheck_id` uuid COMMENT 'The reversing check, when this one was voided'
);

CREATE TABLE `paycheck_lines` (
  `id` uuid PRIMARY KEY,
  `paycheck_id` uuid NOT NULL,
  `kind` ENUM ('earning', 'pre_tax_deduction', 'tax', 'post_tax_deduction', 'employer_contribution') NOT NULL,
  `code` varchar(30) NOT NULL COMMENT 'Earning code, deduction code or tax code',
  `description` varchar(200) NOT NULL,
  `hours` numeric(6,2),
  `rate_cents` bigint,
  `amount_cents` bigint NOT NULL,
  `employer_paid` boolean NOT NULL DEFAULT false COMMENT 'Employer taxes and matches: cost to the company, not taken from the employee',
  `tax_jurisdiction_id` uuid
);

CREATE TABLE `direct_deposits` (
  `id` uuid PRIMARY KEY,
  `paycheck_id` uuid NOT NULL,
  `bank_account_id` uuid NOT NULL,
  `amount_cents` bigint NOT NULL,
  `status` ENUM ('pending', 'sent', 'settled', 'returned') NOT NULL DEFAULT 'pending',
  `trace_number` varchar(30) COMMENT 'ACH trace number from the bank file',
  `settled_at` timestamptz
);

CREATE UNIQUE INDEX `holidays_index_0` ON `holidays` (`legal_entity_id`, `holiday_date`);

CREATE UNIQUE INDEX `job_assignments_index_1` ON `job_assignments` (`employee_id`, `effective_from`);

CREATE UNIQUE INDEX `compensation_records_index_2` ON `compensation_records` (`employee_id`, `effective_from`);

CREATE INDEX `time_entries_index_3` ON `time_entries` (`employee_id`, `work_date`);

CREATE UNIQUE INDEX `pay_periods_index_4` ON `pay_periods` (`pay_schedule_id`, `starts_on`);

CREATE UNIQUE INDEX `paychecks_index_5` ON `paychecks` (`pay_run_id`, `employee_id`);

ALTER TABLE `legal_entities` COMMENT = 'The company that legally employs and pays people. A group with two subsidiaries runs two payrolls.';

ALTER TABLE `departments` COMMENT = 'The org chart. Labor cost reports roll up through parent_id.';

ALTER TABLE `tax_jurisdictions` COMMENT = 'Where taxes are owed. Work locations and withholding elections point here; the tax rules themselves live in your tax engine.';

ALTER TABLE `work_locations` COMMENT = 'Offices and remote work sites. Where someone works drives which taxes apply.';

ALTER TABLE `job_titles` COMMENT = 'The catalog of roles. Who holds which one, and since when, is in job_assignments.';

ALTER TABLE `holidays` COMMENT = 'Paid holidays per employer. Holiday pay and leave calculations read this calendar.';

ALTER TABLE `employees` COMMENT = 'One row per person employed by an entity. A rehire reuses the row, so tenure and history survive.';

ALTER TABLE `employee_addresses` COMMENT = 'Home address history. Residence drives some local taxes, so the address on a past pay date has to be recoverable.';

ALTER TABLE `emergency_contacts` COMMENT = 'Who to call.';

ALTER TABLE `bank_accounts` COMMENT = 'Direct deposit accounts. Net pay is split across active accounts by priority, with one remainder account taking the rest.';

ALTER TABLE `job_assignments` COMMENT = 'Role, department, manager and location over time. A promotion closes one row and opens the next.';

ALTER TABLE `compensation_records` COMMENT = 'Pay rate history. A raise is a new row from its effective date, so a retro adjustment can be computed exactly.';

ALTER TABLE `tax_withholding_elections` COMMENT = 'Withholding choices per jurisdiction, effective-dated like everything else that changes a paycheck.';

ALTER TABLE `employee_deductions` COMMENT = 'An employee enrolled in a deduction: benefits, retirement, garnishments.';

ALTER TABLE `time_entries` COMMENT = 'Hours worked by non-exempt staff. Only approved, unpaid entries are picked up by the next run.';

ALTER TABLE `leave_types` COMMENT = 'Kinds of time off and how they accrue.';

ALTER TABLE `leave_requests` COMMENT = 'Time off asked for. Approval writes a usage entry to the leave ledger.';

ALTER TABLE `leave_ledger` COMMENT = 'Every accrual, use and adjustment. A balance is the sum of this ledger, never a column someone can overwrite.';

ALTER TABLE `deduction_types` COMMENT = 'Benefit and deduction plans an employee can be enrolled in.';

ALTER TABLE `pay_schedules` COMMENT = 'How often a group is paid.';

ALTER TABLE `pay_periods` COMMENT = 'The concrete periods of a schedule.';

ALTER TABLE `pay_runs` COMMENT = 'One payroll calculation. Drafts can be recalculated; once approved, a mistake is fixed by an off-cycle or correction run, never by editing this one.';

ALTER TABLE `paychecks` COMMENT = 'One employee in one run: the pay stub. Totals are snapshots of the lines below, frozen at approval.';

ALTER TABLE `paycheck_lines` COMMENT = 'Every earning, deduction and tax on a stub, with the rate and hours that produced it.';

ALTER TABLE `direct_deposits` COMMENT = 'Net pay as it was actually split and sent. Bank account details are snapshotted by reference, so a later change never rewrites where money went.';

ALTER TABLE `departments` ADD FOREIGN KEY (`legal_entity_id`) REFERENCES `legal_entities` (`id`);

ALTER TABLE `departments` ADD FOREIGN KEY (`parent_id`) REFERENCES `departments` (`id`);

ALTER TABLE `work_locations` ADD FOREIGN KEY (`legal_entity_id`) REFERENCES `legal_entities` (`id`);

ALTER TABLE `work_locations` ADD FOREIGN KEY (`tax_jurisdiction_id`) REFERENCES `tax_jurisdictions` (`id`);

ALTER TABLE `holidays` ADD FOREIGN KEY (`legal_entity_id`) REFERENCES `legal_entities` (`id`);

ALTER TABLE `employees` ADD FOREIGN KEY (`legal_entity_id`) REFERENCES `legal_entities` (`id`);

ALTER TABLE `employee_addresses` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `emergency_contacts` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `bank_accounts` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `job_assignments` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `job_assignments` ADD FOREIGN KEY (`job_title_id`) REFERENCES `job_titles` (`id`);

ALTER TABLE `job_assignments` ADD FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`);

ALTER TABLE `job_assignments` ADD FOREIGN KEY (`work_location_id`) REFERENCES `work_locations` (`id`);

ALTER TABLE `job_assignments` ADD FOREIGN KEY (`manager_id`) REFERENCES `employees` (`id`);

ALTER TABLE `compensation_records` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `compensation_records` ADD FOREIGN KEY (`pay_schedule_id`) REFERENCES `pay_schedules` (`id`);

ALTER TABLE `compensation_records` ADD FOREIGN KEY (`approved_by`) REFERENCES `employees` (`id`);

ALTER TABLE `tax_withholding_elections` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `tax_withholding_elections` ADD FOREIGN KEY (`tax_jurisdiction_id`) REFERENCES `tax_jurisdictions` (`id`);

ALTER TABLE `employee_deductions` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `employee_deductions` ADD FOREIGN KEY (`deduction_type_id`) REFERENCES `deduction_types` (`id`);

ALTER TABLE `time_entries` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `time_entries` ADD FOREIGN KEY (`approved_by`) REFERENCES `employees` (`id`);

ALTER TABLE `time_entries` ADD FOREIGN KEY (`pay_run_id`) REFERENCES `pay_runs` (`id`);

ALTER TABLE `leave_types` ADD FOREIGN KEY (`legal_entity_id`) REFERENCES `legal_entities` (`id`);

ALTER TABLE `leave_requests` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `leave_requests` ADD FOREIGN KEY (`leave_type_id`) REFERENCES `leave_types` (`id`);

ALTER TABLE `leave_requests` ADD FOREIGN KEY (`decided_by`) REFERENCES `employees` (`id`);

ALTER TABLE `leave_ledger` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `leave_ledger` ADD FOREIGN KEY (`leave_type_id`) REFERENCES `leave_types` (`id`);

ALTER TABLE `leave_ledger` ADD FOREIGN KEY (`leave_request_id`) REFERENCES `leave_requests` (`id`);

ALTER TABLE `leave_ledger` ADD FOREIGN KEY (`pay_run_id`) REFERENCES `pay_runs` (`id`);

ALTER TABLE `pay_schedules` ADD FOREIGN KEY (`legal_entity_id`) REFERENCES `legal_entities` (`id`);

ALTER TABLE `pay_periods` ADD FOREIGN KEY (`pay_schedule_id`) REFERENCES `pay_schedules` (`id`);

ALTER TABLE `pay_runs` ADD FOREIGN KEY (`pay_period_id`) REFERENCES `pay_periods` (`id`);

ALTER TABLE `pay_runs` ADD FOREIGN KEY (`approved_by`) REFERENCES `employees` (`id`);

ALTER TABLE `paychecks` ADD FOREIGN KEY (`pay_run_id`) REFERENCES `pay_runs` (`id`);

ALTER TABLE `paychecks` ADD FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`);

ALTER TABLE `paychecks` ADD FOREIGN KEY (`voided_by_paycheck_id`) REFERENCES `paychecks` (`id`);

ALTER TABLE `paycheck_lines` ADD FOREIGN KEY (`paycheck_id`) REFERENCES `paychecks` (`id`);

ALTER TABLE `paycheck_lines` ADD FOREIGN KEY (`tax_jurisdiction_id`) REFERENCES `tax_jurisdictions` (`id`);

ALTER TABLE `direct_deposits` ADD FOREIGN KEY (`paycheck_id`) REFERENCES `paychecks` (`id`);

ALTER TABLE `direct_deposits` ADD FOREIGN KEY (`bank_account_id`) REFERENCES `bank_accounts` (`id`);
SQL Server
sql-server.sql SQL Server
CREATE TABLE [legal_entities] (
  [id] uuid PRIMARY KEY,
  [name] varchar(200) NOT NULL,
  [tax_id] varchar(30) UNIQUE NOT NULL,
  [country] char(2) NOT NULL
)
GO

CREATE TABLE [departments] (
  [id] uuid PRIMARY KEY,
  [legal_entity_id] uuid NOT NULL,
  [parent_id] uuid,
  [name] varchar(200) NOT NULL,
  [cost_center] varchar(30)
)
GO

CREATE TABLE [tax_jurisdictions] (
  [id] uuid PRIMARY KEY,
  [code] varchar(20) UNIQUE NOT NULL,
  [name] varchar(200) NOT NULL,
  [level] nvarchar(255) NOT NULL CHECK ([level] IN ('federal', 'state', 'local'))
)
GO

CREATE TABLE [work_locations] (
  [id] uuid PRIMARY KEY,
  [legal_entity_id] uuid NOT NULL,
  [name] varchar(200) NOT NULL,
  [address_line1] varchar(200),
  [city] varchar(120),
  [region] varchar(60),
  [country] char(2) NOT NULL,
  [tax_jurisdiction_id] uuid NOT NULL,
  [is_remote] boolean NOT NULL DEFAULT (false)
)
GO

CREATE TABLE [job_titles] (
  [id] uuid PRIMARY KEY,
  [name] varchar(120) UNIQUE NOT NULL,
  [job_family] varchar(60),
  [pay_grade] varchar(10)
)
GO

CREATE TABLE [holidays] (
  [id] uuid PRIMARY KEY,
  [legal_entity_id] uuid NOT NULL,
  [holiday_date] date NOT NULL,
  [name] varchar(100) NOT NULL
)
GO

CREATE TABLE [employees] (
  [id] uuid PRIMARY KEY,
  [employee_number] varchar(20) UNIQUE NOT NULL,
  [legal_entity_id] uuid NOT NULL,
  [legal_first_name] varchar(100) NOT NULL,
  [legal_last_name] varchar(100) NOT NULL,
  [preferred_name] varchar(100),
  [work_email] varchar(254) UNIQUE,
  [date_of_birth] date NOT NULL,
  [national_id_last4] char(4),
  [hire_date] date NOT NULL,
  [termination_date] date,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('active', 'on_leave', 'terminated')) DEFAULT 'active'
)
GO

CREATE TABLE [employee_addresses] (
  [id] uuid PRIMARY KEY,
  [employee_id] uuid NOT NULL,
  [address_line1] varchar(200) NOT NULL,
  [city] varchar(120) NOT NULL,
  [region] varchar(60),
  [postal_code] varchar(20),
  [country] char(2) NOT NULL,
  [effective_from] date NOT NULL,
  [effective_to] date
)
GO

CREATE TABLE [emergency_contacts] (
  [id] uuid PRIMARY KEY,
  [employee_id] uuid NOT NULL,
  [name] varchar(200) NOT NULL,
  [relationship] varchar(60) NOT NULL,
  [phone] varchar(30) NOT NULL
)
GO

CREATE TABLE [bank_accounts] (
  [id] uuid PRIMARY KEY,
  [employee_id] uuid NOT NULL,
  [routing_number] varchar(20) NOT NULL,
  [account_token] varchar(200) NOT NULL,
  [account_last4] char(4) NOT NULL,
  [account_type] nvarchar(255) NOT NULL CHECK ([account_type] IN ('checking', 'savings')),
  [allocation] nvarchar(255) NOT NULL CHECK ([allocation] IN ('amount', 'percent', 'remainder')),
  [allocation_value] numeric(12,2),
  [priority] smallint NOT NULL DEFAULT (1),
  [active] boolean NOT NULL DEFAULT (true)
)
GO

CREATE TABLE [job_assignments] (
  [id] uuid PRIMARY KEY,
  [employee_id] uuid NOT NULL,
  [job_title_id] uuid NOT NULL,
  [department_id] uuid NOT NULL,
  [work_location_id] uuid NOT NULL,
  [manager_id] uuid,
  [employment_type] nvarchar(255) NOT NULL CHECK ([employment_type] IN ('full_time', 'part_time', 'temporary', 'intern')),
  [flsa_status] nvarchar(255) NOT NULL CHECK ([flsa_status] IN ('exempt', 'non_exempt')),
  [fte] numeric(3,2) NOT NULL DEFAULT (1),
  [effective_from] date NOT NULL,
  [effective_to] date,
  [reason] varchar(100)
)
GO

CREATE TABLE [compensation_records] (
  [id] uuid PRIMARY KEY,
  [employee_id] uuid NOT NULL,
  [pay_type] nvarchar(255) NOT NULL CHECK ([pay_type] IN ('salary', 'hourly')),
  [amount_cents] bigint NOT NULL,
  [currency] char(3) NOT NULL,
  [pay_schedule_id] uuid NOT NULL,
  [effective_from] date NOT NULL,
  [effective_to] date,
  [reason] varchar(100),
  [approved_by] uuid
)
GO

CREATE TABLE [tax_withholding_elections] (
  [id] uuid PRIMARY KEY,
  [employee_id] uuid NOT NULL,
  [tax_jurisdiction_id] uuid NOT NULL,
  [filing_status] varchar(30) NOT NULL,
  [allowances] smallint NOT NULL DEFAULT (0),
  [extra_withholding_cents] integer NOT NULL DEFAULT (0),
  [exempt] boolean NOT NULL DEFAULT (false),
  [effective_from] date NOT NULL,
  [effective_to] date
)
GO

CREATE TABLE [employee_deductions] (
  [id] uuid PRIMARY KEY,
  [employee_id] uuid NOT NULL,
  [deduction_type_id] uuid NOT NULL,
  [calculation] nvarchar(255) NOT NULL CHECK ([calculation] IN ('fixed_amount', 'percent_of_gross')),
  [value] numeric(12,2) NOT NULL,
  [annual_limit_cents] bigint,
  [effective_from] date NOT NULL,
  [effective_to] date
)
GO

CREATE TABLE [time_entries] (
  [id] uuid PRIMARY KEY,
  [employee_id] uuid NOT NULL,
  [work_date] date NOT NULL,
  [hours] numeric(5,2) NOT NULL,
  [earning_code] nvarchar(255) NOT NULL CHECK ([earning_code] IN ('regular', 'overtime', 'double_time', 'holiday', 'shift_differential')) DEFAULT 'regular',
  [approved_by] uuid,
  [approved_at] timestamptz,
  [pay_run_id] uuid
)
GO

CREATE TABLE [leave_types] (
  [id] uuid PRIMARY KEY,
  [legal_entity_id] uuid NOT NULL,
  [code] varchar(20) NOT NULL,
  [name] varchar(100) NOT NULL,
  [accrual_hours_per_period] numeric(5,2) NOT NULL DEFAULT (0),
  [max_balance_hours] numeric(6,2),
  [paid] boolean NOT NULL DEFAULT (true)
)
GO

CREATE TABLE [leave_requests] (
  [id] uuid PRIMARY KEY,
  [employee_id] uuid NOT NULL,
  [leave_type_id] uuid NOT NULL,
  [starts_on] date NOT NULL,
  [ends_on] date NOT NULL,
  [hours] numeric(6,2) NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('pending', 'approved', 'denied', 'cancelled')) DEFAULT 'pending',
  [decided_by] uuid,
  [decided_at] timestamptz
)
GO

CREATE TABLE [leave_ledger] (
  [id] uuid PRIMARY KEY,
  [employee_id] uuid NOT NULL,
  [leave_type_id] uuid NOT NULL,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('accrual', 'usage', 'adjustment', 'carryover', 'payout')),
  [hours] numeric(6,2) NOT NULL,
  [effective_on] date NOT NULL,
  [leave_request_id] uuid,
  [pay_run_id] uuid,
  [memo] varchar(200)
)
GO

CREATE TABLE [deduction_types] (
  [id] uuid PRIMARY KEY,
  [code] varchar(20) UNIQUE NOT NULL,
  [name] varchar(100) NOT NULL,
  [pre_tax] boolean NOT NULL,
  [employer_match_percent] numeric(5,2)
)
GO

CREATE TABLE [pay_schedules] (
  [id] uuid PRIMARY KEY,
  [legal_entity_id] uuid NOT NULL,
  [name] varchar(60) NOT NULL,
  [frequency] nvarchar(255) NOT NULL CHECK ([frequency] IN ('weekly', 'biweekly', 'semimonthly', 'monthly')),
  [anchor_date] date NOT NULL
)
GO

CREATE TABLE [pay_periods] (
  [id] uuid PRIMARY KEY,
  [pay_schedule_id] uuid NOT NULL,
  [starts_on] date NOT NULL,
  [ends_on] date NOT NULL,
  [pay_date] date NOT NULL
)
GO

CREATE TABLE [pay_runs] (
  [id] uuid PRIMARY KEY,
  [pay_period_id] uuid NOT NULL,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('regular', 'off_cycle', 'correction', 'bonus')) DEFAULT 'regular',
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('draft', 'calculated', 'approved', 'paid', 'voided')) DEFAULT 'draft',
  [calculated_at] timestamptz,
  [approved_by] uuid,
  [approved_at] timestamptz,
  [paid_at] timestamptz
)
GO

CREATE TABLE [paychecks] (
  [id] uuid PRIMARY KEY,
  [pay_run_id] uuid NOT NULL,
  [employee_id] uuid NOT NULL,
  [gross_cents] bigint NOT NULL,
  [taxes_cents] bigint NOT NULL,
  [deductions_cents] bigint NOT NULL,
  [net_cents] bigint NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('calculated', 'approved', 'paid', 'voided')) DEFAULT 'calculated',
  [voided_by_paycheck_id] uuid
)
GO

CREATE TABLE [paycheck_lines] (
  [id] uuid PRIMARY KEY,
  [paycheck_id] uuid NOT NULL,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('earning', 'pre_tax_deduction', 'tax', 'post_tax_deduction', 'employer_contribution')),
  [code] varchar(30) NOT NULL,
  [description] varchar(200) NOT NULL,
  [hours] numeric(6,2),
  [rate_cents] bigint,
  [amount_cents] bigint NOT NULL,
  [employer_paid] boolean NOT NULL DEFAULT (false),
  [tax_jurisdiction_id] uuid
)
GO

CREATE TABLE [direct_deposits] (
  [id] uuid PRIMARY KEY,
  [paycheck_id] uuid NOT NULL,
  [bank_account_id] uuid NOT NULL,
  [amount_cents] bigint NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('pending', 'sent', 'settled', 'returned')) DEFAULT 'pending',
  [trace_number] varchar(30),
  [settled_at] timestamptz
)
GO

CREATE UNIQUE INDEX [holidays_index_0] ON [holidays] ("legal_entity_id", "holiday_date")
GO

CREATE UNIQUE INDEX [job_assignments_index_1] ON [job_assignments] ("employee_id", "effective_from")
GO

CREATE UNIQUE INDEX [compensation_records_index_2] ON [compensation_records] ("employee_id", "effective_from")
GO

CREATE INDEX [time_entries_index_3] ON [time_entries] ("employee_id", "work_date")
GO

CREATE UNIQUE INDEX [pay_periods_index_4] ON [pay_periods] ("pay_schedule_id", "starts_on")
GO

CREATE UNIQUE INDEX [paychecks_index_5] ON [paychecks] ("pay_run_id", "employee_id")
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The company that legally employs and pays people. A group with two subsidiaries runs two payrolls.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'legal_entities';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Employer identification number (e.g. US EIN)',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'legal_entities',
@level2type = N'Column', @level2name = 'tax_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The org chart. Labor cost reports roll up through parent_id.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'departments';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Departments nest; the root has no parent',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'departments',
@level2type = N'Column', @level2name = 'parent_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Where taxes are owed. Work locations and withholding elections point here; the tax rules themselves live in your tax engine.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'tax_jurisdictions';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. US-FED, US-CA, US-NY-NYC',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'tax_jurisdictions',
@level2type = N'Column', @level2name = 'code';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Offices and remote work sites. Where someone works drives which taxes apply.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'work_locations';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The catalog of roles. Who holds which one, and since when, is in job_assignments.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'job_titles';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Paid holidays per employer. Holiday pay and leave calculations read this calendar.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'holidays';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One row per person employed by an entity. A rehire reuses the row, so tenure and history survive.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'employees';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Last four of the SSN or equivalent for matching; the full number belongs in a vault, not this table',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'employees',
@level2type = N'Column', @level2name = 'national_id_last4';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Home address history. Residence drives some local taxes, so the address on a past pay date has to be recoverable.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'employee_addresses';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for the current address',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'employee_addresses',
@level2type = N'Column', @level2name = 'effective_to';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Who to call.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'emergency_contacts';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Direct deposit accounts. Net pay is split across active accounts by priority, with one remainder account taking the rest.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bank_accounts';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Tokenized account number from your payments provider',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bank_accounts',
@level2type = N'Column', @level2name = 'account_token';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Fixed amount, percentage, or the remainder',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bank_accounts',
@level2type = N'Column', @level2name = 'allocation';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Cents for amount, 0-100 for percent, null for remainder',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bank_accounts',
@level2type = N'Column', @level2name = 'allocation_value';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Order in which a split paycheck is distributed',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'bank_accounts',
@level2type = N'Column', @level2name = 'priority';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Role, department, manager and location over time. A promotion closes one row and opens the next.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'job_assignments';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Exempt or non-exempt from overtime',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'job_assignments',
@level2type = N'Column', @level2name = 'flsa_status';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Full-time equivalent, e.g. 0.50 for half time',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'job_assignments',
@level2type = N'Column', @level2name = 'fte';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for the current assignment',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'job_assignments',
@level2type = N'Column', @level2name = 'effective_to';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Hire, Promotion, Transfer',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'job_assignments',
@level2type = N'Column', @level2name = 'reason';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Pay rate history. A raise is a new row from its effective date, so a retro adjustment can be computed exactly.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'compensation_records';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Annual salary for salaried, hourly rate for hourly',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'compensation_records',
@level2type = N'Column', @level2name = 'amount_cents';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Annual review, Market adjustment',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'compensation_records',
@level2type = N'Column', @level2name = 'reason';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Withholding choices per jurisdiction, effective-dated like everything else that changes a paycheck.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'tax_withholding_elections';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'As declared on the W-4 or local equivalent',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'tax_withholding_elections',
@level2type = N'Column', @level2name = 'filing_status';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'An employee enrolled in a deduction: benefits, retirement, garnishments.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'employee_deductions';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Cents per pay period, or percent of gross',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'employee_deductions',
@level2type = N'Column', @level2name = 'value';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. the 401(k) contribution limit',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'employee_deductions',
@level2type = N'Column', @level2name = 'annual_limit_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Hours worked by non-exempt staff. Only approved, unpaid entries are picked up by the next run.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'time_entries';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Set when paid, so hours are never paid twice',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'time_entries',
@level2type = N'Column', @level2name = 'pay_run_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Kinds of time off and how they accrue.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'leave_types';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. PTO, SICK, PARENTAL',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'leave_types',
@level2type = N'Column', @level2name = 'code';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Time off asked for. Approval writes a usage entry to the leave ledger.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'leave_requests';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Every accrual, use and adjustment. A balance is the sum of this ledger, never a column someone can overwrite.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'leave_ledger';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Positive for accruals, negative for usage',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'leave_ledger',
@level2type = N'Column', @level2name = 'hours';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The run that accrued it, for accrual entries',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'leave_ledger',
@level2type = N'Column', @level2name = 'pay_run_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Benefit and deduction plans an employee can be enrolled in.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'deduction_types';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. 401K, MEDICAL, DENTAL, HSA, GARNISH',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'deduction_types',
@level2type = N'Column', @level2name = 'code';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Reduces taxable wages when true',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'deduction_types',
@level2type = N'Column', @level2name = 'pre_tax';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Employer contribution as a percent of the employee amount',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'deduction_types',
@level2type = N'Column', @level2name = 'employer_match_percent';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'How often a group is paid.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pay_schedules';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'A known period start; every other period is computed from it',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pay_schedules',
@level2type = N'Column', @level2name = 'anchor_date';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The concrete periods of a schedule.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pay_periods';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One payroll calculation. Drafts can be recalculated; once approved, a mistake is fixed by an off-cycle or correction run, never by editing this one.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'pay_runs';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One employee in one run: the pay stub. Totals are snapshots of the lines below, frozen at approval.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'paychecks';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The reversing check, when this one was voided',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'paychecks',
@level2type = N'Column', @level2name = 'voided_by_paycheck_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Every earning, deduction and tax on a stub, with the rate and hours that produced it.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'paycheck_lines';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Earning code, deduction code or tax code',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'paycheck_lines',
@level2type = N'Column', @level2name = 'code';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Employer taxes and matches: cost to the company, not taken from the employee',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'paycheck_lines',
@level2type = N'Column', @level2name = 'employer_paid';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Net pay as it was actually split and sent. Bank account details are snapshotted by reference, so a later change never rewrites where money went.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'direct_deposits';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'ACH trace number from the bank file',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'direct_deposits',
@level2type = N'Column', @level2name = 'trace_number';
GO

ALTER TABLE [departments] ADD FOREIGN KEY ([legal_entity_id]) REFERENCES [legal_entities] ([id])
GO

ALTER TABLE [departments] ADD FOREIGN KEY ([parent_id]) REFERENCES [departments] ([id])
GO

ALTER TABLE [work_locations] ADD FOREIGN KEY ([legal_entity_id]) REFERENCES [legal_entities] ([id])
GO

ALTER TABLE [work_locations] ADD FOREIGN KEY ([tax_jurisdiction_id]) REFERENCES [tax_jurisdictions] ([id])
GO

ALTER TABLE [holidays] ADD FOREIGN KEY ([legal_entity_id]) REFERENCES [legal_entities] ([id])
GO

ALTER TABLE [employees] ADD FOREIGN KEY ([legal_entity_id]) REFERENCES [legal_entities] ([id])
GO

ALTER TABLE [employee_addresses] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [emergency_contacts] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [bank_accounts] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [job_assignments] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [job_assignments] ADD FOREIGN KEY ([job_title_id]) REFERENCES [job_titles] ([id])
GO

ALTER TABLE [job_assignments] ADD FOREIGN KEY ([department_id]) REFERENCES [departments] ([id])
GO

ALTER TABLE [job_assignments] ADD FOREIGN KEY ([work_location_id]) REFERENCES [work_locations] ([id])
GO

ALTER TABLE [job_assignments] ADD FOREIGN KEY ([manager_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [compensation_records] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [compensation_records] ADD FOREIGN KEY ([pay_schedule_id]) REFERENCES [pay_schedules] ([id])
GO

ALTER TABLE [compensation_records] ADD FOREIGN KEY ([approved_by]) REFERENCES [employees] ([id])
GO

ALTER TABLE [tax_withholding_elections] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [tax_withholding_elections] ADD FOREIGN KEY ([tax_jurisdiction_id]) REFERENCES [tax_jurisdictions] ([id])
GO

ALTER TABLE [employee_deductions] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [employee_deductions] ADD FOREIGN KEY ([deduction_type_id]) REFERENCES [deduction_types] ([id])
GO

ALTER TABLE [time_entries] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [time_entries] ADD FOREIGN KEY ([approved_by]) REFERENCES [employees] ([id])
GO

ALTER TABLE [time_entries] ADD FOREIGN KEY ([pay_run_id]) REFERENCES [pay_runs] ([id])
GO

ALTER TABLE [leave_types] ADD FOREIGN KEY ([legal_entity_id]) REFERENCES [legal_entities] ([id])
GO

ALTER TABLE [leave_requests] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [leave_requests] ADD FOREIGN KEY ([leave_type_id]) REFERENCES [leave_types] ([id])
GO

ALTER TABLE [leave_requests] ADD FOREIGN KEY ([decided_by]) REFERENCES [employees] ([id])
GO

ALTER TABLE [leave_ledger] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [leave_ledger] ADD FOREIGN KEY ([leave_type_id]) REFERENCES [leave_types] ([id])
GO

ALTER TABLE [leave_ledger] ADD FOREIGN KEY ([leave_request_id]) REFERENCES [leave_requests] ([id])
GO

ALTER TABLE [leave_ledger] ADD FOREIGN KEY ([pay_run_id]) REFERENCES [pay_runs] ([id])
GO

ALTER TABLE [pay_schedules] ADD FOREIGN KEY ([legal_entity_id]) REFERENCES [legal_entities] ([id])
GO

ALTER TABLE [pay_periods] ADD FOREIGN KEY ([pay_schedule_id]) REFERENCES [pay_schedules] ([id])
GO

ALTER TABLE [pay_runs] ADD FOREIGN KEY ([pay_period_id]) REFERENCES [pay_periods] ([id])
GO

ALTER TABLE [pay_runs] ADD FOREIGN KEY ([approved_by]) REFERENCES [employees] ([id])
GO

ALTER TABLE [paychecks] ADD FOREIGN KEY ([pay_run_id]) REFERENCES [pay_runs] ([id])
GO

ALTER TABLE [paychecks] ADD FOREIGN KEY ([employee_id]) REFERENCES [employees] ([id])
GO

ALTER TABLE [paychecks] ADD FOREIGN KEY ([voided_by_paycheck_id]) REFERENCES [paychecks] ([id])
GO

ALTER TABLE [paycheck_lines] ADD FOREIGN KEY ([paycheck_id]) REFERENCES [paychecks] ([id])
GO

ALTER TABLE [paycheck_lines] ADD FOREIGN KEY ([tax_jurisdiction_id]) REFERENCES [tax_jurisdictions] ([id])
GO

ALTER TABLE [direct_deposits] ADD FOREIGN KEY ([paycheck_id]) REFERENCES [paychecks] ([id])
GO

ALTER TABLE [direct_deposits] ADD FOREIGN KEY ([bank_account_id]) REFERENCES [bank_accounts] ([id])
GO

How to use this template

  1. Open the ER diagram to see the tables and how they relate.
  2. Copy it into your workspace. You get your own editable copy, in DBML.
  3. Export it as PostgreSQL, MySQL, or SQL Server DDL, or as Prisma, Drizzle, SQLAlchemy, TypeScript, Zod or C# models.

The copy opens in DB Planner’s ER diagram tool, with the schema as text in the DBML editor beside it. Have a database already? Turn its SQL into an ER diagram instead.

Frequently asked questions

Which table is at the centre of the Employee and Payroll Management database schema?
employees. It is referenced by 12 other tables: bank_accounts, compensation_records, emergency_contacts, employee_addresses, employee_deductions and 7 more.
How is bank_accounts related to employees?
bank_accounts.employee_id references employees.id, a many to one relationship. All 43 relationships in the schema are listed on this page.
Which table in the Employee and Payroll Management database schema has the most columns?
employees, with 12 columns, followed by job_assignments (12) and bank_accounts (10). The schema has 187 columns across 25 tables.
Can I export it to PostgreSQL or MySQL?
Yes. Every DB Planner template exports as DBML, as PostgreSQL, MySQL, or SQL Server DDL, and as C# models.
Is this template free?
Yes. You can open it in the browser without an account, and copy it into your own workspace with a free DB Planner account.
Can I change it after copying it?
Yes. A copy is yours: you can rename tables, add columns, and export it. Changes the author makes later do not affect your copy.