How to properly create composite primary keys - MYSQL(如何正确创建复合主键 - MYSQL)
问题描述
这是我正在使用的密集设置的粗略过度简化.table_1
和 table_2
都有自增代理主键作为 ID.info
是一个包含 table_1
和 table_2
信息的表格.
Here is a gross oversimplification of an intense setup I am working with. table_1
and table_2
both have auto-increment surrogate primary keys as the ID. info
is a table that contains information about both table_1
and table_2
.
table_1 (id, field)
table_2 (id, field, field)
info ( ???, field)
我正在尝试决定是否应该让 info
的主键成为来自 table_1
和 table_2
的 ID 的组合.如果我要这样做,以下哪个最有意义?
(在这个例子中,我将 ID 11209 与 ID 437 结合起来)
I am trying to decided if I should make the primary key of info
a composite of the IDs from table_1
and table_2
. If I were to do this, which of these makes most sense?
( in this example I am combining ID 11209 with ID 437 )
INT(9)
11209437 (我可以想象为什么这很糟糕)VARCHAR (10)
11209-437十进制 (10,4)
11209.437
INT(9)
11209437 (i can imagine why this is bad)
VARCHAR (10)
11209-437
DECIMAL (10,4)
11209.437
或者别的什么?
将其用作 MYSQL MYISAM 数据库上的主键是否可以?
Would this be fine to use this as the Primary Key on a MYSQL MYISAM DB?
推荐答案
我会使用复合(多列)键.
I would use a composite (multi-column) key.
CREATE TABLE INFO (
t1ID INT,
t2ID INT,
PRIMARY KEY (t1ID, t2ID)
)
通过这种方式,您也可以将 t1ID 和 t2ID 作为外键指向它们各自的表.
This way you can have t1ID and t2ID as foreign keys pointing to their respective tables as well.
这篇关于如何正确创建复合主键 - MYSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何正确创建复合主键 - MYSQL


基础教程推荐
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在多列上分布任意行 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01