帮助mysql的数据库设计结构

发布于 2024-11-08 12:59:09 字数 1893 浏览 0 评论 0原文

我是数据库和 mysql 的新手,正在尝试了解如何设计数据库。我正在开展测试项目,人们可以在其中查看某些橱柜中的物品。因此,用户可以单击一个柜子并查看其中的所有架子以及每个架子的所有物品。

到目前为止,我已经弄清楚我需要下表:

Item Table  
Item ID, Item Name

Shelf Table 
Shelf ID, Shelf Number

Cabinet Table   
Cabinet ID, Cabinet Name

我所困惑的是我需要什么关系表?我确信这对某人来说真的很容易!

我可以有第四张桌子,里面有类似这样的东西:

ItemID, Shelf ID, Cabinet ID

这有意义吗,这样我就可以查询柜子中的所有架子及其每个架子的物品?我确信这个问题很容易回答,但我的脑子很痛!我被告知如果有帮助的话可以使用 MyIsam 作为存储引擎。

谢谢

编辑:

谢谢,如果您不介意的话,还有一个问题。你知道如何获取所有架子中的所有项目吗,这样它们就会像这样出现在 html 中:

<div id="shelf-1"><p>spoon</p><p>fork</p>
<div id="shelf-2"><p>knife</p></div>

我有以下似乎可行的内容,但通过阅读周围的内容,我认为在查询中循环不是一个好习惯:

$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>');

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>');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一人独醉 2024-11-15 12:59:09

在写 SQL 之前先用英语思考一下。

你可以有很多柜子;每个柜子有零个或多个搁板;每个架子上可以有零个或多个物品。

一个架子一次只能放在一个柜子里;一件商品一次只能放在一个货架上。

因此,我将其设计为三个表,其中两个表具有一对多关系。

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 
);

这是一个示例查询,它将为您提供特定柜子中的所有物品(刚刚尝试过 - 效果很好):

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)

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)
请远离我 2024-11-15 12:59:09

您将需要这三个表之间的两个表来保存与关系相对应的数据。

Shelf_Cabinet Table
ShelfID, CabinetID

Item_Shelf Table
ItemID, ShelfID

You would need two tables between those three tables to hold the data corresponding the relations.

Shelf_Cabinet Table
ShelfID, CabinetID

and

Item_Shelf Table
ItemID, ShelfID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文