MySQL 1tbl 中的 2 个外键引用 1 个主键
我的数据库中有 2 个表,我无法完全正确地定义关系:
tbl_users 保存有关系统用户和管理员的一些信息(我“打包”了 2 个不同的表 - tbl_users 和 tbl_admins - 在一起,因为属性之间大约有 90% 的重叠。属性之间的主要区别是通过引入布尔数据类型的属性来排序的)*
< strong>tbl_orders *保存有关用户创建的订单的一些信息,并引用 tbl_users,以查看哪个用户创建了哪个订单。此外,该表应包含对执行订单处理的管理员的引用。有关管理的信息存储在 tbl_users 中。*
表描述中:
**tbl_users**
id int pk
name varchar
address varchar
**tbl_admins**
id int pk
name varchar
address varchar
这两个打包在一起看起来像这样:
**tbl_users**
id int pk
name varchar
address varchar
user boolean
admin boolean
问题从这里开始:
**tbl_orders**
id int pk
amount int
processed boolean
user_id - references tbl_users, but only the user that is user boolean=1
admin_id - references tbl_users, but only the user that is admin boolean=1
I have 2 tables in my DB, and I can't quite define the relations correctly:
tbl_users holds some info about system users AND admins (I "packed" 2 different tables - tbl_users and tbl_admins - together, because there was about 90% overlapping of attributes. Main difference between attributes is sorted by introducing an attribute with boolean data type)*
tbl_orders *holds some info about orders that users have created, and reference to tbl_users, to see which user created which order. Also, this table should hold the reference to admin who did the order processing. Info on admin is stored in tbl_users.*
table description:
**tbl_users**
id int pk
name varchar
address varchar
**tbl_admins**
id int pk
name varchar
address varchar
these two packed together look like this:
**tbl_users**
id int pk
name varchar
address varchar
user boolean
admin boolean
problems start here:
**tbl_orders**
id int pk
amount int
processed boolean
user_id - references tbl_users, but only the user that is user boolean=1
admin_id - references tbl_users, but only the user that is admin boolean=1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可以通过再创建两个表来解决:
它们可能看起来相同,但实际上并非如此。第一个保存有关用户创建哪个订单的数据,第二个保存有关管理员传递订单的数据。
或者,您可以将上面的两个表聚合成一个表,如下所示:
在上面的表中,admin 布尔值表示 tbl_user 中的一个特定用户实际上是处理订单的管理员(对于 admin = 1)。对于 admin = 0,用户 ID 表示创建订单的用户。
希望这有帮助。
This can be solved by creating two more table:
Thay might look the same, but they aren't. The first keeps data on which user created which order, and the second on which admin passed the order.
Or, you can aggregate the two tables above into one table, saying like this:
In the table above, the admin boolean says that one particular user from tbl_user is in fact the admin which processed the order (for admin = 1). For admin = 0, the user id indicates the user creating the order.
Hope this helps.
另一种可能性:
将“组合用户”表命名为“人”,保存有关任何用户/管理员/其他用户类型的信息。
然后,有另一个表只存储一个人实际上是用户的信息(还有另一个表用于管理员):
然后,很容易定义外键约束并强制执行您所描述的完整性:
Another possibility:
Name that "combined users" table to "persons", holding the info about any user/admin/whatever-other-usertype has.
Then, have another table that stores only the info that a person is actually a user (and another table for admins):
Then, it's easy to define the foreign key constraints and enforce the integrity you describe:
通过引用 tbl_admins 可以轻松地在数据库上强制执行第二个外键。必须在应用程序代码上强制执行仅用户的外键。
The second foreign key can be easly enforced on the database by referencing the tbl_admins. The foreign key to only users will have to be enforced on application code.