简体   繁体   English

数据库表设计多表外键

[英]database table design foreign key to multiple table

In my PHP and psql application, I have large number of customers and service providers, both can login to the system, for login details we created a table USER but how can we map both CUSTOMER table and SERVICE_PROVIDER table in the USER table as foreign key. 在我的PHP和psql应用程序中,我有大量的客户和服务提供商,都可以登录到系统,对于登录详细信息,我们创建了一个表USER,但是如何将USER表中的CUSTOMER表和SERVICE_PROVIDER表映射为外键。 The structure Skeleton is 骨架的结构是

**CUSTOMER:** 
id    bigserial, 
name  character varying(100),
address character varying(350),
phone character varying(50),
etc....

**SERVICE_PROVIDER:**
id bigserial, 
name  character varying(100),
email  character varying(100),
phno  character varying(50),
first_esc  character varying(100),
sec  character varying(100),
etc


**USER**
id bigserial, 
username  character varying(100),
password  character varying(100),
customer_type  bigint 

In USER Table column customer_type may either Customer or Service Provider 在“用户表”列中,customer_type可以是“客户”或“服务提供商”

How to design the table structure.... 如何设计表格结构。

Please help me 请帮我

Thanking You Anju 谢谢你安居

What is needed is FKs from multiple other groups (subtypes) to (supertype) user. 需要从多个其他组(子类型)到(超类型)用户的FK。

Every table has a statement. 每个表都有一个语句。 The rows that make the statement true go in the table. 使语句为真的行进入表。 Customers are users and service providers are users. 客户是用户,服务提供者是用户。 So when CUSTOMER & SERVICE_PROVIDER (etc) say something is true about a person, USER says something about them. 因此,当CUSTOMER&SERVICE_PROVIDER(等)说出某个人的说法是正确的时,USER就说出了他们的某些话。

// "customer [id] has ..."
CUSTOMER(id,...)
    fk id references USER -- a customer is a user

// "service provider [id] has ..."
SERVICE_PROVIDER(id,....)
    fk id references USER -- a service provider is a user

// "user [id] has ..."
USER(id,...)

See this . 看到这个

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM