Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp with database design structure for mysql
    primarykey
    data
    text
    <p>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.</p> <p>So far I've worked out I'll need to following tables:</p> <pre><code>Item Table Item ID, Item Name Shelf Table Shelf ID, Shelf Number Cabinet Table Cabinet ID, Cabinet Name </code></pre> <p>The bit I'm stuck on is what relational tables I'd need? I'm sure this is really easy for somebody!</p> <p>Could I have a fourth table which holds something like:</p> <pre><code>ItemID, Shelf ID, Cabinet ID </code></pre> <p>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.</p> <p>Thanks</p> <p>Edit:</p> <p>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:</p> <pre><code>&lt;div id="shelf-1"&gt;&lt;p&gt;spoon&lt;/p&gt;&lt;p&gt;fork&lt;/p&gt; &lt;div id="shelf-2"&gt;&lt;p&gt;knife&lt;/p&gt;&lt;/div&gt; </code></pre> <p>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:</p> <pre><code>$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('&lt;div&gt;'); while($item = mysql_fetch_assoc($resultItem)) { echo('&lt;p&gt;' . $item['item_name'] . '&lt;/p&gt;' . PHP_EOL); } echo('&lt;/div&gt;'); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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