在数据库中,我有一个用户名表,我有管辖权表。我还有中间表来将用户分配给一个或多个管辖区。
user table
--------------
userID
first_name
jurID
jurisdiction table
-----------------
jurId
region
user_jurisdiction
------------------
userID
jurID
这会导致类似:
user_jurisdiction
+--------+-------+
| userID | jurID |
+--------+-------+
| 6 | 2 |
| 6 | 3 |
| 11 | 2 |
| 12 | 1 |
+--------+-------+
对于拥有1个以上权限的用户,是否可以重复使用userdID行?或者,如果没有使用额外的JurID列并将它们设置为null会更好吗?如:
user_jurisdiction
+--------+-------++--------+-------+
| userID | jurID | jurID | jurID |
+--------+-------++--------+-------+
| 6 | 2 | 3| null|
| 11 | 2 | null| null|
| 12 | 1 | null| null|
+--------+-------+--------+--------+
答案 0 :(得分:1)
您有一个user_jurisdiction
表,因为您在用户和管辖区之间存在多对多关系。
因此,对于单个用户,user_jurisdiction
中有多行,每个管辖区有一行:
6 2
6 3
11 2
12 1
为您的例子。
此外,您应该在该表中包含自动递增UserJurisdictionId
。每个表都有一个主键是个好主意。
并且,您可以从user.jurID
表中删除user
。有关司法管辖区的所有信息都应在user_jurisdictions
中。您可以使用join
获取特定用户的管辖区信息。