Database schema
I have 2 Office and Employee objects and I want to create a database schema for WorkHours. There are offices that have default working days for their employees, but there are Employees who may have different working hours.
What's the best way to model it? Do I have business hours in both tables?
source to share
What you can do is create a diagram similar to the one below. Of course, you need to add additional columns to store additional data if you have one, and also set up queries with data types specific to the RDBMS being used.
CREATE TABLE Office(OfficeID integer
, OfficeName VARCHAR(10))
CREATE TABLE Employee(EmployeeID integer
, EmployeeName VARCHAR(10)
, OfficeID integer
, WorkingHoursID integer
, UseOfficeDefaultWorkingHours Boolean)
CREATE TABLE WorkingHours(ID integer
, StartTime TIME
, EndTime TIME
, OfficeID integer
, OfficeDefaultWorkingHours Boolean)
Also, be sure to implement constraints and primary keys on your unique columns in each of your tables.
In the table, Employee
you add a column to indicate if Employee is running during the default office hours ( UseOfficeDefaultWorkingHours
).
In the table, WorkingHours
you add a column to indicate if the row contains the default office hours by using another boolean column, in this case OfficeDefaultWorkingHours
.
You can request this diagram to get working hours for an employee with a request similar to the following:
SELECT
E.EmployeeName
, W.StartTime
, W.EndTime
FROM Employee E
INNER JOIN WorkingHours W ON E.OfficeID = WorkingHours.OfficeID
AND E.UseOfficeDefaultWorkingHours = W.OfficeDefaultWorkingHours
AND W.ID = CASE
WHEN E.WorkingHoursID IS NOT NULL
THEN E.WorkingHoursID
ELSE W.ID
END
WHERE E.EmployeeID = 1
This query will work in SQL Server RDBMS, but I'm not sure if it will work with other RDBMS products and you might need to tweak accordingly.
source to share
WorkingHours will be a different entity.
The office has one default working day (or maybe more than one?). The employee has one working hour. The employee's working time data is taken from the employee's working hour. An employee's working hour can be changed without changing the working day at the office.
source to share
Create three tables
1. Office: this should be an office location, name and own ID
2. Synchronization table: ID, OfficeID, TimeSLot
3. Employee. In this table, create an OfficeID column and pass the office id from the office table it works in and another column with TimeslotID and pass id of the second table.
For example: EmpID, OfficeID, TimeSLotID
source to share