问题
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