Delphi 2009、MyDAC 和关系数据库

发布于 2024-07-21 23:42:12 字数 464 浏览 5 评论 0原文

关于在 Delphi 2009 中使用 MyDAC 的关系数据库概念,我遇到了很大的问题。

我有一个看起来像下面这样的数据库结构:


Item

  • id
  • name

Storage

  • id
  • name

StorageItem

  • id
  • item_id
  • storage_id
  • place

现在,当我有来自“Item”的活动数据集时,如何在 DBGrid 中显示所有关联的存储?

顺便说一句:最好不要在每个表中使用“id”,而是更改它并使用“id_item”或“id_storage”之类的内容?

先感谢您 :)

I have quite a problem concerning the use of relational database concepts in Delphi 2009 with MyDAC.

I have a database structure that looks somehow like the following:


Item

  • id
  • name

Storage

  • id
  • name

StorageItem

  • id
  • item_id
  • storage_id
  • place

Now when I have an active dataset from "Item" how can I display all associated Storages in for example a DBGrid?

By the way: Would it be better to not use "id" in every table but to alter it and use something like for example "id_item" or "id_storage"?

Thank you in advance :)

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

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

发布评论

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

评论(3

私野 2024-07-28 23:42:12
  • 使用 StorageItem 您创建了一个
    多对多关系。 如果你
    只需要一对多(许多存储
    与一项相关,但您
    不需要反之亦然),那么你
    可能只是添加另一个字段
    存储表(item_id)
    是 Items 表的外键。
    然后你创建一个索引
    存储表中的item_id,以及
    连接
    中的两个表
    主从关系。
  • 如果您确实需要多对多,那么您
    可以使用 SQL 添加查询组件
    (从 StorageItem 中选择 *,其中
    item_id := :current_storage_id),以及
    current_storage_id 是您的查询的
    范围。
  • With StorageItem you created a
    many-to-many relationship. If you
    need just one-to-many (many storages
    are related to one item, but you
    don't need the vice versa), then you
    may just add another field to the
    Storage table (item_id) that would
    be a foreign key for Items table.
    Then you create an index on
    item_id in Storage table, and
    connect the two tables in
    master-detail relationship.
  • If you do need many-to-many then you
    may add a query component with SQL
    (select * from StorageItem where
    item_id := :current_storage_id), and
    current_storage_id is your query's
    parameter.
哑剧 2024-07-28 23:42:12
Select a.ID, b.Name, a.Place
from StorageItem a
inner join Storage b
on (a.id = b.id)

上面的查询将返回 StorageItem 表中的所有项目及其名称,现在,如果您想过滤它以仅返回特定项目的项目,请添加 where 子句,就像

Select a.ID, b.Name, a.Place
from StorageItem a
inner join Storage b
on (a.id = b.id)
where a.item_id = 1 -- place the item id here

您可以使用带有参数的 where ,例如:

MyQuery.Sql.Text := ' Select a.ID, b.Name, a.Place from StorageItem a
+ ' inner join Storage b on (a.id = b.id) '
+ ' where a.item_id = :ItemNo ';
MyQuery.ParamByName('ItemNo').asInteger := 1;
MyQuery.Open;

并分配上面的查询对于 dbGrid

,您还可以使用 MasterSource 属性来建立关系,而无需使用“where”部分

Select a.ID, b.Name, a.Place
from StorageItem a
inner join Storage b
on (a.id = b.id)

the above query will return all the items in StorageItem table with it's name, now if you want to filter it to return only items for a specific item add where clause to be like

Select a.ID, b.Name, a.Place
from StorageItem a
inner join Storage b
on (a.id = b.id)
where a.item_id = 1 -- place the item id here

you can use where with parameters such as:

MyQuery.Sql.Text := ' Select a.ID, b.Name, a.Place from StorageItem a
+ ' inner join Storage b on (a.id = b.id) '
+ ' where a.item_id = :ItemNo ';
MyQuery.ParamByName('ItemNo').asInteger := 1;
MyQuery.Open;

and assign the query above to dbGrid

also you can use MasterSource property to make the relations without using the "where" part

掩耳倾听 2024-07-28 23:42:12

我个人对 MyDAC 并不熟悉,但大多数数据集组件都有某种方法来建立主从关系。 检查数据集上是否有 MasterSource 属性,或者是否存在将详细数据集链接到主数据集的类似方法。 如果没有,您可以使用 TDatasetField 建立链接,并过滤嵌套数据集以仅显示正确的记录。

至于 ID 列名称,最好为每个字段指定一个描述性名称,这样您就可以通过查看代码来判断链接是否正确。 如果您将 id 列称为“id”,那么它可以是任何 id 列,如果您开始传递对数据集的引用,则可能会造成混乱。 但是,如果每次都调用 item_id(有时不是 item_id,有时是 id_item),那么您总是确切地知道自己在看什么。 它也让您更容易知道您的代码是正确的。 过滤器显示“master.item_id =Detail.item_id”更容易阅读“master.id=detail.item_id”。 例如,如果将 master 分配给错误的数据集,这可能是错误的并且会默默失败。

I'm not familiar with MyDAC personally, but most dataset components have some way to establish master-detail relationships. Check if there's a MasterSource property on your dataset, or some similar way to link a detail dataset to a master dataset. If not, you could use a TDatasetField to establish a link, and filter the nested dataset to only display the right records.

As for ID column names, it's a good idea to give a descriptive name to each field, so you can tell by looking at the code that you've got your links right. If you call your id column "id", that could be any id column, and that could get confusing if you start passing around references to datasets. But if it's called item_id every time, (not item_id sometimes and id_item sometimes) then you always know exactly what you're looking at. It makes it easier to know that your code is right, too. A filter that says "master.item_id = detail.item_id" is easier to read that "master.id = detail.item_id". That could be wrong and fail silently if master is assigned to the wrong dataset, for example.

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