数据库设计:使用非键作为FK?
假设我有下表:
TABLE: widget
- widget_id (not null, unique, auto-increment)
- model_name (PK)
- model_year (PK)
model_name
和 model_year
组成一个复合键。在另一个表中使用widget_id
作为FK有什么问题吗?
Say I have the following table:
TABLE: widget
- widget_id (not null, unique, auto-increment)
- model_name (PK)
- model_year (PK)
model_name
and model_year
make up a composite key. Is there any problem to using widget_id
as a FK in another table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
键是任意数量的列,可用于唯一标识表中的每一行。
在您显示的示例中,您的小部件表有两个键:
在标准 SQL 中,外键可以引用引用表上的任何声明键(主键或唯一键)。我需要检查 MySQL 的合规性。
来自MySQL参考手册中的外键 :
作为替代方案,如果您希望使用引用表中的复合键,则该表中将有两列对应于 model_name 和 model_year,然后将外键约束声明为:
Re InnoDB vs MyISAM,在ALTER TABLE 的文档
A key is any number of columns that can be used to uniquely identify each row within the table.
In the example you've shown, your widget table has two keys:
In standard SQL, a foreign key may reference any declared key on the referenced table (either primary key or unique). I'd need to check MySQLs compliance.
From MySQL reference manual on foreign keys:
As an alternative, if you wish to use the composite key from your referencing table, you'd have two columns in that table that correspond to model_name and model_year, and would then declare your foreign key constraint as:
Re InnoDB vs MyISAM, in the docs for ALTER TABLE
我没有特定于 my-sql 的经验,但对于一般的数据库建模来说,
了解主键和辅助键之间的区别非常重要。
即使许多数据库(我确信 Oracle 确实如此)允许指定唯一(简单或复合)键作为 FK 目标,但这也不被认为是最佳实践。改用PK。
在我看来,辅助键的 FK 仅应用于与不受您控制的表相关。
在你的具体情况下,我肯定会FK到widget_id:那是因为widget_id应该是你的PK,并且复合材料只是唯一的(当然不是空的)。这会在鬃毛情况下带来更好的性能,因为您在查询中只加入一列,并且通常被认为是最佳实践(谷歌“代理键”以获取更多信息)
I have no experience specific to my-sql, but with database-modeling in general
It is really important to understand the difference between primary and secondary keys.
Even if many db (I know for sure Oracle does) permit to specify an unique (simple or composite) key as the FK target, this is not considered a best practice. Use the PK instead.
FK to a secondary key should be used imo only to relate to tables that are not under your control.
In your specific case, I would certainly FK to widget_id: that is because the widget_id should be your PK, and the composite only made unique (and not null of course). This leads to better performance in mane cases, as you join only one column in queries, and is generally considered a best practice (google 'surrogate key' for more info)
如果没有可用的索引,MySQL 将在该列上创建一个索引:
http://dev.mysql.com/doc /refman/5.1/en/innodb-foreign-key-constraints.html
MySQL will create an index on the column if there isn't one it can use:
http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
假设您有两张桌子。第一个表如下所示:
如果您想创建另一个表来引用第一个表中的唯一记录,它应该如下所示:
使用复合主键,您必须在 FK 引用中包含所有关键字段以确保您唯一指定一条记录。
Let's say you have two tables. The first table looks like this:
If you want to make another table that refers to unique records in the first table, it should look something like this:
With compound primary keys, you have to include all key fields in your FK references to make sure that you are uniquely specifying a record.