Create a database table where entries can come from another table or entirely new input

给你一囗甜甜゛ 提交于 2019-12-25 02:18:40

问题


I have a table of customers, employees, and suppliers. Each of them has some common fields like name, address, contact_no, and email along with other fields. Now, I want a new table called investors. However, investors can be from employees, suppliers, customers, or entirely new people. Investors also have the aforementioned common fields as well as their own fields. How do I go about designing the table?


回答1:


How about having a generic table people that would contain the common fields (name, address, etc.) and JOIN the appropriate specific table?

To read a customer (by customer ID) you would SELECT * FROM people p JOIN customers c ON p.id = c.person_id WHERE c.id=...

To read an investor (by investor ID) you would SELECT * FROM people p JOIN investors i ON p.id = i.person_id WHERE i.id=...

This way the same person can be a customer and an investor, it just depends on the point of view.



来源:https://stackoverflow.com/questions/51175248/create-a-database-table-where-entries-can-come-from-another-table-or-entirely-ne

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!