简体   繁体   English

如何通过多个表列主键引用一列

[英]How to reference one column by multiple tables columns primary keys

I have a database consist of 3 tables each table indicate one user like this:我有一个由 3 个表组成的数据库,每个表表示一个用户,如下所示:

  1. Admin table 'User 1'.管理员表“用户 1”。

  2. Staff table 'User 2'.员工表“用户 2”。

  3. Student table 'user 3'.学生表“用户 3”。

I want to implement a common table between them called 'POST' WHERE each user can post content to the post table content column, however I need to Identify the type of posters ' the one who posted the post ' so I'm adding a column user_type to the table POST the column user_type should hold the ID of the USER ' Admin, Staff, Student '我想在它们之间实现一个名为“POST”的公用表,每个用户都可以将内容发布到发布表内容列,但是我需要确定海报的类型“发布帖子的人”,所以我要添加一列user_type 到表 POST 列 user_type 应该包含 USER 'Admin, Staff, Student' 的 ID

I'm in a case where I want to be able to refer to this column ( user_type) in the POST table to multiple table columns Admin table, Staff table, Student table.我希望能够将 POST 表中的此列(user_type)引用到多个表列 Admin 表、Staff 表、Student 表。 So I Can identify the USER who posted the post.所以我可以识别发布帖子的用户。

All these kind of users are persons that you need to store in one parent table called "Users" or "Persons" from which the parent's table are "User 1", "User 2", "User 3"...所有这些类型的用户都是您需要存储在一个名为“Users”或“Persons”的父表中的人,其中父表是“User 1”、“User 2”、“User 3”......

This is called inheritance in data modeling.这在数据建模中称为 inheritance。

Thus you can join the post table to the father table.因此,您可以将 post 表加入到 parent 表中。

As an example:举个例子:

CREATE TABLE T_PERSON_PRS           (PRS_ID INT IDENTITY PRIMARY KEY, PRS_NAME...)
CREATE TABLE T_PERSON_ADMIN_PAM     (PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ... )
CREATE TABLE T_PERSON_STAFF_PSF     (PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ... )
CREATE TABLE T_PERSON_STUDENT_PSD   (PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ... )
CREATE TABLE T_POST_PST             (PST_ID INT IDENTITY PRIMARY KEY, PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何合并没有主键的MySQL表,并基于3列优先于另一个? - How to Merge MySQL tables without Primary Keys and by prioritizing one over the other based on 3 columns? 如何在作为主键和候选键的表中引用多个外键? - How to reference multiple foreign keys in a table that is a primary key and candidate keys? 是否可以将一列引用为多个外键? - Is it possible to reference one column as multiple foreign keys? 如何使用一个表单中的自动增量主键将相关实体插入到多个表中 - How to insert related entities to multiple tables with auto-increment primary keys from one form 如何比较两个表的主键,这是五列的组合? - How to compare primary keys two tables, which is combination of five columns? 如何在phpmyadmin表中删除多个主键? - How can I drop multiple primary keys in phpmyadmin tables? 两个具有相似列但主键不同的表 - Two tables with similar columns but different primary keys 可以将数据库中所有表的主键作为一个表的外键引用吗? - Is it okay to reference the primary keys of all tables in database as a foreign key of one table? 不同表上多个主键的外键 - Foreign key to multiple primary keys on different tables 将多个表中的多个列添加到查询MYSQL中的一列 - Adding Multiple Columns from Multiple Tables to One column in a Query MYSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM