MySQL 1tbl 中的 2 个外键引用 1 个主键

发布于 2024-12-02 23:02:03 字数 863 浏览 0 评论 0原文

我的数据库中有 2 个表,我无法完全正确地定义关系:

  1. tbl_users 保存有关系统用户和管理员的一些信息(我“打包”了 2 个不同的表 - tbl_users 和 tbl_admins - 在一起,因为属性之间大约有 90% 的重叠。属性之间的主要区别是通过引入布尔数据类型的属性来排序的)*

  2. < 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:

  1. 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)*

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别想她 2024-12-09 23:02:04

这可以通过再创建两个表来解决:

***tbl_user_order *** 
user_id --> foreign key: tbl_users.id 
order_id --> foreign key: tbl_orders.id


*** tbl_admin_order ***
user_id --> foreign key: tbl_users.id 
order_id --> foreign key: tbl_orders.id

它们可能看起来相同,但实际上并非如此。第一个保存有关用户创建哪个订单的数据,第二个保存有关管理员传递订单的数据。

或者,您可以将上面的两个表聚合成一个表,如下所示:

***tbl_user_order *** 
user_id --> foreign key: tbl_users.id 
order_id --> foreign key: tbl_orders.id
admin boolean

在上面的表中,admin 布尔值表示 tbl_user 中的一个特定用户实际上是处理订单的管理员(对于 admin = 1)。对于 admin = 0,用户 ID 表示创建订单的用户。

希望这有帮助。

This can be solved by creating two more table:

***tbl_user_order *** 
user_id --> foreign key: tbl_users.id 
order_id --> foreign key: tbl_orders.id


*** tbl_admin_order ***
user_id --> foreign key: tbl_users.id 
order_id --> foreign key: tbl_orders.id

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:

***tbl_user_order *** 
user_id --> foreign key: tbl_users.id 
order_id --> foreign key: tbl_orders.id
admin boolean

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.

提笔书几行 2024-12-09 23:02:03

另一种可能性:

将“组合用户”表命名为“人”,保存有关任何用户/管理员/其他用户类型的信息。

**tbl_persons**
id int pk
name varchar
address varchar

然后,有另一个表只存储一个人实际上是用户的信息(还有另一个表用于管理员):

**tbl_users**
id int pk (NOT auto_incremented), fk references tbl_persons(id)

**tbl_admins**
id int pk (NOT auto_incremented), fk references tbl_persons(id)

然后,很容易定义外键约束并强制执行您所描述的完整性:

**tbl_orders**
id int pk 
amount int
processed boolean
user_id fk references tbl_users(id)
admin_id fk references tbl_admins(id)

Another possibility:

Name that "combined users" table to "persons", holding the info about any user/admin/whatever-other-usertype has.

**tbl_persons**
id int pk
name varchar
address varchar

Then, have another table that stores only the info that a person is actually a user (and another table for admins):

**tbl_users**
id int pk (NOT auto_incremented), fk references tbl_persons(id)

**tbl_admins**
id int pk (NOT auto_incremented), fk references tbl_persons(id)

Then, it's easy to define the foreign key constraints and enforce the integrity you describe:

**tbl_orders**
id int pk 
amount int
processed boolean
user_id fk references tbl_users(id)
admin_id fk references tbl_admins(id)
夜声 2024-12-09 23:02:03

通过引用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文