Delphi 2009、MyDAC 和关系数据库
关于在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
多对多关系。 如果你
只需要一对多(许多存储
与一项相关,但您
不需要反之亦然),那么你
可能只是添加另一个字段
存储表(item_id)
是 Items 表的外键。
然后你创建一个索引
存储表中的item_id,以及
连接
中的两个表
主从关系。
可以使用 SQL 添加查询组件
(从 StorageItem 中选择 *,其中
item_id := :current_storage_id),以及
current_storage_id 是您的查询的
范围。
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.
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.
上面的查询将返回 StorageItem 表中的所有项目及其名称,现在,如果您想过滤它以仅返回特定项目的项目,请添加 where 子句,就像
您可以使用带有参数的 where ,例如:
并分配上面的查询对于 dbGrid
,您还可以使用 MasterSource 属性来建立关系,而无需使用“where”部分
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
you can use where with parameters such as:
and assign the query above to dbGrid
also you can use MasterSource property to make the relations without using the "where" part
我个人对 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, (notitem_id
sometimes andid_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 ifmaster
is assigned to the wrong dataset, for example.