Answer the question
In order to leave comments, you need to log in
Is the database decomposition correct?
There is a database for a stadium or for some other event where it is possible to buy a ticket or just reserve a seat, it does not matter yet. The goal is to start by simply looking at whether a seat is booked or not. It is believed that there is a sector, a row, and of course the place itself. At first, I did this, created a sector table, a row table and a seat table that stored the sector and row id, and the user table, in addition to the contact data fields, also had the reserved(bool) and seat_id fields (the id of the seat table with other IDs) and more field for the number of the seat itself. But it seemed to me that it was necessary and could be done much easier, so I settled on this option:
User(table):
id int PK,
name varchar,
surname varchar
Seat(table)
id int PK,
row int,
number int,
sector enum('A', 'B', 'C')
Reservation(table)
id int PK,
row_id int FK,
seat_id int FK,
reserved bool
In general which approach is best first or second, if no, please tell me the correct one. It is believed that there is a predetermined number of places, rows and sectors. 5 rows, 3 sectors, and 8 seats in 1 row. Thank you in advance.
Answer the question
In order to leave comments, you need to log in
In general, everything is quite simple for you. I will offer you a variant of the scheme, and you compare it with your own.
Let's try to think in terms of sets - this is always useful when designing relational databases, because relations are sets.
So, you have a variety of places available for booking. We will assume that the database serves only ONE stadium. The sector, row and place number are all hierarchical coordinates of the place, uniquely defining this place. So the key for the place is (sector, row, number).
Next, we need to decide how we will represent the set of ALL places. You indicated that the number of seats is known in advance. In theory, in this case, we do not even need to store many of these places, because it is determined by the rule, but sooner or later you will come across the fact that the rule must be broken - for example, some of the seats at the moment turned out to be unsuitable for selling tickets (a chair was broken, a threshold fell off, etc.). Therefore, IMHO, it is still worth creating a "Place" relation to store a set of ALL available places, as you actually did. Those. we have the first relation: Seat(sector, row, number)
.
Now we want to store a lot of reserved seats. Because there will be many events at the stadium, for each of the events we will have our own set of reserved seats. This means that the primary key of the event must fall into the primary key of the "reserved seat" relationship. Let's assume that the event's primary key is id (because you didn't provide any other details). The remaining attributes of the reserved seat relationship must refer to one of the available seats, i.e. we must have a foreign key in the Seat relation. So we have a second relation: ReservedSeat(event_id, sector, row, number)
. When designing a relational database, it is very important to clearly understand what the PRESENCE or ABSENCE of a record in each of the relations means. The presence of an entry in relation to ReservedSeat tells usevent. No more, no less. If there is no entry in relation to ReservedSeat, then a specific seat for a specific event is still free .
And yes, it seems we have forgotten the main thing - and who is the place occupied by? We need one more attribute, a foreign key on the User relation. Let's add it:
ReservedSeat(event_id, sector, row, number, user_id)
. It is important that this attribute is not included in the primary key, because different customers cannot book the same seat (i.e. the seat is always booked by the same person). I would do this:
there is a set of places that are somehow characterized there (let it be a sector, a row and a place - it doesn’t really matter) and each such entry has its own ID:
[Seat]
id
sector
row
number
[Event]
id
datetime
[Reservation]
id
seat_id
event_id
// another fields
I myself often use enum, but you need to understand their limitations / features and not sculpt thoughtlessly. I recommend reading the book SQL Antipatterns . There's some mixed advice in there, but at least it gives food for thought.
Probably, nevertheless, I would make separate tables for sectors and rows of a sector, because now it is considered that there are so many sectors, rows and places, and then suddenly it will be considered differently . And in the table of places would refer only to the necessary row of the sector.
The fact that you took out the armor from the users is absolutely correct, they do not belong there.
But the armor table is strange:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question