Note that there are some explanatory texts on larger screens.

plurals
  1. POTable Structure for alternative to storing an array in a single MySQL field
    text
    copied!<p>I am currently building a car booking system using PDO PHP and MySQL but am struggling with how to structure my tables for any car extras the user may have added to their booking. </p> <p>I currently have the following tables at the moment which use the reservation_table to link the booking_id with the car_id (taken from their separate tables):</p> <p>car_table:</p> <pre><code>ID | car_make | car_name 1 | Ford | Focus 2 | BMW | Z3 3 | Audi | A5 </code></pre> <p>booking_table:</p> <pre><code>booking_ID | booking_time | total_cost | etc.. 125674 | 2013-02-02 | 91.55 887463 | 2013-01-19 | 52.00 209930 | 2013-01-11 | 23.99 </code></pre> <p>reservation_table:</p> <pre><code>ID | booking_ID | car_ID 1 | 125674 | 2 2 | 887463 | 2 3 | 209930 | 1 </code></pre> <p>extras_table:</p> <pre><code>ID | car_extra | extra_price 1 | GPS | 22.99 2 | Baby Seat | 12.99 3 | Car Charger | 15.99 </code></pre> <p>Originally I was going to add an extra column into the reservation_table using an array of the selected car extras to link them with the booking as so: booking_table:</p> <pre><code>ID | booking_ID | car_ID | car_extras 1 | 125674 | 2 | [2,3] 2 | 887463 | 2 | [1] 3 | 209930 | 1 | [1,3] </code></pre> <p>but I have read that this is bad practice. How can I structure my tables so that I can assign the selected car extra ID's (which can range from the user selecting 0 to 12) to a particular booking?</p> <p>Would the following work by creating a new table that linked just the booking_ID with the extra_ID (this would mean multiple rows of the same booking id though):</p> <p>selected_extras_table:</p> <pre><code>ID | booking_ID | car_extra_ID 1 | 125674 | 2 2 | 125674 | 3 3 | 887463 | 1 4 | 209930 | 1 5 | 209930 | 3 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload