设计多对多实体关系的最佳方式
The best way for designing many to many entities relationship
我有两个table权限和多对多关系组
CREATE TABLE `permissions` (
`Permission_Id` int(11) NOT NULL AUTO_INCREMENT,
`Permission_Name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`Permission_Id`)
)
群组table
CREATE TABLE `groups` (
`Group_Id` int(11) NOT NULL AUTO_INCREMENT,
`Group_Desc` varchar(100) DEFAULT NULL,
PRIMARY KEY (`Group_Id`)
)
我很困惑如何实现多对多关系
在新的table
中创建Group_id和Permission_id的复合主键哪个更好
或者使用 join 关键字从两个 table 中创建一个新的 table & select 列。
我知道解决方法。
在这种情况下,我需要创建 "junction" table 来保持 many-to-many 关系。
CREATE TABLE Groups_Permissions
(
Group_Id INT,
Permission_Id INT,
)
Group_Id 和 Permmission_Id 的组合应该是唯一的,并且对组具有 FK 和权限 tables。
来自 my blog:
这样做。
CREATE TABLE XtoY (
# No surrogate id for this table
x_id MEDIUMINT UNSIGNED NOT NULL, -- For JOINing to one table
y_id MEDIUMINT UNSIGNED NOT NULL, -- For JOINing to the other table
# Include other fields specific to the 'relation'
PRIMARY KEY(x_id, y_id), -- When starting with X
INDEX (y_id, x_id) -- When starting with Y
) ENGINE=InnoDB;
备注:
⚈ Lack of an AUTO_INCREMENT id for this table -- The PK given is the 'natural' PK; there is no good reason for a surrogate.
⚈ "MEDIUMINT" -- This is a reminder that all INTs should be made as small as is safe (smaller ⇒ faster). Of course the declaration here must match the definition in the table being linked to.
⚈ "UNSIGNED" -- Nearly all INTs may as well be declared non-negative
⚈ "NOT NULL" -- Well, that's true, isn't it?
⚈ "InnoDB" -- More effecient than MyISAM because of the way the PRIMARY KEY is clustered with the data in InnoDB.
⚈ "INDEX(y_id, x_id)" -- The PRIMARY KEY makes it efficient to go one direction; this index makes the other direction efficient. No need to say UNIQUE; that would be extra effort on INSERTs.
⚈ In the secondary index, saying just INDEX(y_id) would work because it would implicit include x_id. But I would rather make it more obvious that I am hoping for a 'covering' index.
要有条件地插入新链接,请使用 IODKU
请注意,如果您在此 table 中有一个 AUTO_INCREMENT,IODKU 会很快 "burn" ID。
更多
A FOREIGN KEY
在涉及的列上隐式创建索引。
PRIMARY KEY(a,b)
(1) 表示组合 (a,b)
是 UNIQUE
,并且 (2) 按 (a,b)
.
对数据进行排序
INDEX(a), INDEX(b)
(无论是由FOREIGN KEY
生成还是手动生成)与INDEX(a,b)
.
不相同
InnoDB 确实需要一个 PRIMARY KEY
,所以你不妨说 PRIMARY KEY (a,b)
而不是 UNIQUE(a,b)
.
我有两个table权限和多对多关系组
CREATE TABLE `permissions` (
`Permission_Id` int(11) NOT NULL AUTO_INCREMENT,
`Permission_Name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`Permission_Id`)
)
群组table
CREATE TABLE `groups` (
`Group_Id` int(11) NOT NULL AUTO_INCREMENT,
`Group_Desc` varchar(100) DEFAULT NULL,
PRIMARY KEY (`Group_Id`)
)
我很困惑如何实现多对多关系
在新的table
中创建Group_id和Permission_id的复合主键哪个更好或者使用 join 关键字从两个 table 中创建一个新的 table & select 列。
我知道解决方法。
在这种情况下,我需要创建 "junction" table 来保持 many-to-many 关系。
CREATE TABLE Groups_Permissions
(
Group_Id INT,
Permission_Id INT,
)
Group_Id 和 Permmission_Id 的组合应该是唯一的,并且对组具有 FK 和权限 tables。
来自 my blog:
这样做。
CREATE TABLE XtoY (
# No surrogate id for this table
x_id MEDIUMINT UNSIGNED NOT NULL, -- For JOINing to one table
y_id MEDIUMINT UNSIGNED NOT NULL, -- For JOINing to the other table
# Include other fields specific to the 'relation'
PRIMARY KEY(x_id, y_id), -- When starting with X
INDEX (y_id, x_id) -- When starting with Y
) ENGINE=InnoDB;
备注:
⚈ Lack of an AUTO_INCREMENT id for this table -- The PK given is the 'natural' PK; there is no good reason for a surrogate.
⚈ "MEDIUMINT" -- This is a reminder that all INTs should be made as small as is safe (smaller ⇒ faster). Of course the declaration here must match the definition in the table being linked to.
⚈ "UNSIGNED" -- Nearly all INTs may as well be declared non-negative
⚈ "NOT NULL" -- Well, that's true, isn't it?
⚈ "InnoDB" -- More effecient than MyISAM because of the way the PRIMARY KEY is clustered with the data in InnoDB.
⚈ "INDEX(y_id, x_id)" -- The PRIMARY KEY makes it efficient to go one direction; this index makes the other direction efficient. No need to say UNIQUE; that would be extra effort on INSERTs.
⚈ In the secondary index, saying just INDEX(y_id) would work because it would implicit include x_id. But I would rather make it more obvious that I am hoping for a 'covering' index.
要有条件地插入新链接,请使用 IODKU
请注意,如果您在此 table 中有一个 AUTO_INCREMENT,IODKU 会很快 "burn" ID。
更多
A FOREIGN KEY
在涉及的列上隐式创建索引。
PRIMARY KEY(a,b)
(1) 表示组合 (a,b)
是 UNIQUE
,并且 (2) 按 (a,b)
.
INDEX(a), INDEX(b)
(无论是由FOREIGN KEY
生成还是手动生成)与INDEX(a,b)
.
InnoDB 确实需要一个 PRIMARY KEY
,所以你不妨说 PRIMARY KEY (a,b)
而不是 UNIQUE(a,b)
.