Structure table day and time (calendar type)

4

I need to create a table that contains days and times of operation of an establishment, a sort of agenda. This table should represent every day of the week and each day contain the opening and closing times. The challenge is to create this table in a way that does not have redundant data and that facilitates the search for the opening hours of a certain day.

For example, I'm going to need to pick up the property's operating day next Wednesday, or today's time. There is still another factor that is the holiday days (that the schedule will certainly be different from the normal days, taking into account that the establishment is open). Facebook has something like that on business pages.

In the image a representation of the idea (the first time is the opening time and the second time the closing time). I will use MySQL.

    
asked by anonymous 07.11.2016 / 15:37

3 answers

1

According to the comments you described, you have a COMPANY and Schedules ratio of 1 to Many , that way I would do as follows.

declare @Empresa table
(
    id int,
    nome varchar(50)
)


declare @Horarios table
(
    id int,
    idEmpresa int,
    diaSemana varchar(20),
    abertura time,
    fechamento time
)

insert into @Empresa values
(1, 'X'),
(2, 'Y')

insert into @Horarios values
(1,1, 'segunda-feira', '08:00', '18:00'),
(2,1, 'terca-feira', '08:00', '18:00'),
(3,1, 'quarta-feira', '08:00', '18:00'),
(4,1, 'quinta-feira', '08:00', '18:00'),
(5,1, 'sexta-feira', '08:00', '18:00'),
(6,1, 'sabado', '09:00', '17:00'),
(7,1, 'domingo', '09:00', '15:00'),
(8,1, 'feriado', '10:00', '13:00'),
(9,2, 'segunda-feira', '08:00', '18:00'),
(10,2, 'terca-feira', '08:00', '18:00'),
(11,2, 'quarta-feira', '08:00', '18:00'),
(12,2, 'quinta-feira', '08:00', '18:00'),
(13,2, 'sexta-feira', '08:00', '18:00'),
(14,2, 'sabado', '08:00', '18:00'),
(15,2, 'domingo', '09:00', '17:00'),
(16,2, 'feriado', '09:00', '16:00')

declare @horaInformada time = '10:00';

select nome, diaSemana, abertura, fechamento
from @Empresa e
join @Horarios h
on h.idEmpresa = e.id
where e.id = 2
and diaSemana = 'feriado'
and @horaInformada between abertura and fechamento 

I've already made it clear that there may be several contradictory answers to this, because your question I would classify as (mostly opinion-based) ... each one can specify a different way of doing this.     

07.11.2016 / 18:30
0

The best way would be:

id_dia(Primary key) -> int
horario -> varchar(15)

In id_day you enter the number of the corresponding day of the week, and in the opening hours. The id_day should be the number that corresponds to the day of the week, for example:

  

Day Id

     

(1) for Sunday

     

(2) for second

     

(3) for Tuesday

     

...

     

(7) for Saturday

    
17.08.2017 / 11:02
-2

I recommend three tables:

The first table will be to insert the days of the week, so you create two fields:

semana -> Nome Tabela
s_id   -> int
s_name -> varchar

The second table will be to insert the opening and closing times, for this you create 4 fields:

horarios   -> Nome Tabela
h_id       -> int
h_s_id     -> int
h_abertura -> time
h_fecho    -> time

The third table will be to insert the holidays, for this you create 5 fields:

feriados     -> Nome Tabela
f_id         -> int
f_s_id       -> int
f_diaferiado -> datetime
f_abertura   -> time
f_fecho      -> time

Then to return the values of a specific day, just use join , for example:

SELECT * FROM semana 
LEFT JOIN horarios ON horarios.h_s_id=semana.s_id
WHERE s_name=domingo 
    
07.11.2016 / 16:29