Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5931647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:43:27+00:00 2026-05-22T14:43:27+00:00

I’m new to databases and mysql and am trying to get my head round

  • 0

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>');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:43:28+00:00Added an answer on May 22, 2026 at 2:43 pm

    Think about it in terms of English before you write SQL.

    You can have many cabinets; each cabinet has zero or more shelves; each shelf can have zero or more items on it.

    A shelf can only be in one cabinet at a time; an item can only be on one shelf at a time.

    So I’d design this as three tables, with one-to-many relationships for two of them.

    CREATE TABLE IF NOT EXISTS cabinet
    (
        cabinet_id bigint not null auto_increment,
        primary key(cabinet_id)
    );
    
    CREATE TABLE IF NOT EXISTS shelf
    (
        shelf_id bigint not null auto_increment,
        cabinet_id bigint,
        primary key(shelf_id), 
        foreign key(cabinet_id) references cabinet(cabinet_id) on delete cascade on update cascade 
    );
    
    CREATE TABLE IF NOT EXISTS item
    (
        item_id bigint not null auto_increment,
        shelf_id bigint,
        primary key(item_id), 
        foreign key(shelf_id) references shelf(shelf_id) on delete cascade on update cascade 
    );
    

    Here’s a sample query that will get you all the items in a particular cabinet (just tried – works great):

    SELECT item.item_id, cabinet.cabinet_id
    FROM item 
    INNER JOIN shelf ON (item.shelf_id = shelf.shelf_id)
    INNER JOIN cabinet ON (cabinet.cabinet_id = shelf.cabinet_id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.