I currently have a table which represents the start and stop work times of an employee:
- id_employee int
- check_in datetime
- check_out datetime
It requires an update on check_out when the employee is finished.
Would it be preferable to have a table as follows ?
- id_employee int
- date_event datetime
- event_type varchar, values can be CHECKIN or CHECKOUT.
To determine if an employee has already checked in all I have to is check if the last record for a given employee has an event_type of CHECKIN. Also, fetching a record and updating it is no longer necessary.
Is the second approach better ? Or do you have other suggestions ?
As usual, “it depends”.
Option 1 is easier to build, and simpler to query. Finding out who checked in but didn’t check out is a simple query; finding the total hours worked for each employee is also straightforward. This simplicity probably means it will be faster for common queries. The only drawback I see is that it is harder to extend. If you want to capture a different event type for “lunch break”, for instance, you have to add extra columns.
Option 2 is more flexible – you can add new event types without changing your schema. However, simple queries – how many hours did employee x work in June – are quite tricky. You pay for the flexibility in significant additional effort.
So, it depends what you mean by “better”.