Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jordantsap's avatar

DB structure for opereating day and time for object

building a city guide for companies and each company need to show the days, time and session (morning to noun and noun to evening) that is open, what structure is the best to build the database and relations

0 likes
4 replies
LaryAI's avatar
Level 58

To build a database structure for storing the operating days, time, and sessions for each company, you can use the following structure:

  1. Create a table for companies:
CREATE TABLE companies (
    id INT PRIMARY KEY,
    name VARCHAR(255),
    ...
);
  1. Create a table for operating days:
CREATE TABLE operating_days (
    id INT PRIMARY KEY,
    day VARCHAR(255)
);
  1. Create a table for operating times:
CREATE TABLE operating_times (
    id INT PRIMARY KEY,
    time_start TIME,
    time_end TIME
);
  1. Create a table for sessions:
CREATE TABLE sessions (
    id INT PRIMARY KEY,
    session VARCHAR(255)
);
  1. Create a pivot table to establish the many-to-many relationship between companies, operating days, operating times, and sessions:
CREATE TABLE company_operating_hours (
    company_id INT,
    operating_day_id INT,
    operating_time_id INT,
    session_id INT,
    FOREIGN KEY (company_id) REFERENCES companies(id),
    FOREIGN KEY (operating_day_id) REFERENCES operating_days(id),
    FOREIGN KEY (operating_time_id) REFERENCES operating_times(id),
    FOREIGN KEY (session_id) REFERENCES sessions(id)
);

This structure allows you to associate each company with multiple operating days, times, and sessions. You can then query the data based on your requirements.

Note: The above structure is a basic example. You may need to modify it based on your specific needs and additional columns you want to include in the tables.

jordantsap's avatar

@Snapey the package is new to me but the trhing is how to create and update records if i use this package

Please or to participate in this conversation.