I'm new to databases and mysql and am trying to get my head round how to design my database. I'm working on test project where people can view what items they have in some cabinets. So a user can click on a cabinet and see all the shelves inside it and all the items for each shelf.
So far I've worked out I'll need to following tables:
Item Table
Item ID, Item Name
Shelf Table
Shelf ID, Shelf Number
Cabinet Table
Cabinet ID, Cabinet Name
The bit I'm stuck on is what relational tables I'd need? I'm sure this is really easy for somebody!
Could I have a fourth table which holds something like:
ItemID, Shelf ID, Cabinet ID
Would that make sense so I could query all shelves in a cabinet and their items for each shelf? I'm sure this is very easy to answer but my brain hurts! I've been told to use the MyIsam as the storage engine if it helps.
Thanks
Edit:
Thanks, another question if you don't mind. Do you know how to get all the items in all the shelves, so they appear in the html like this:
<div id="shelf-1"><p>spoon</p><p>fork</p>
<div id="shelf-2"><p>knife</p></div>
I have the following which seems to work but from reading around I don't think it is good practice to loop inside a query:
$cabinetID = $_GET['cabinetid'];
$sqlShelf = sprintf(' SELECT shelf_id
FROM shelf
INNER JOIN cabinet ON (cabinet.cabinet_id = shelf.cabinet_id)
WHERE cabinet.cabinet_id = %s', $cabinetID);
$resultShelf = mysql_query($sqlShelf);
while($shelf = mysql_fetch_assoc($resultShelf)) {
$shelfID = $shelf['shelf_id'];
$sqlItem = sprintf('SELECT *
FROM item
INNER JOIN shelf ON (item.shelf_id = shelf.shelf_id)
INNER JOIN cabinet ON (cabinet.cabinet_id = shelf.cabinet_id)
WHERE cabinet.cabinet_id = %s
AND shelf.shelf_id = %s', $cabinetID, $shelfID);
$resultItem = mysql_query($sqlItem);
echo('<div>');
while($item = mysql_fetch_assoc($resultItem)) {
echo('<p>' . $item['item_name'] . '</p>' . PHP_EOL);
}
echo('</div>');