Home > Database > Mysql Tutorial > Can a Foreign Key Reference Multiple Tables in SQL?

Can a Foreign Key Reference Multiple Tables in SQL?

Mary-Kate Olsen
Release: 2024-10-24 01:51:29
Original
1050 people have browsed it

Can a Foreign Key Reference Multiple Tables in SQL?

Polymorphic Foreign Key Constraints: A Versatile Approach

The conventional foreign key constraint establishes a direct link between two specified tables. However, what if you need a polymorphic relationship where a column refers to one of several possible parent tables?

Is a Foreign Key to Multiple Tables Possible?

Unfortunately, the answer is negative. A foreign key constraint can only reference a single parent table. This limitation arises from the need for data integrity and consistency in database systems.

Practical Considerations

Despite the inherent limitation, there are practical approaches that emulate the behavior of a foreign key to multiple tables:

  1. Union Table: Create a single union table that incorporates columns from all possible parent tables. The foreign key column in the child table references the union table. When inserting or updating data, the union table determines which actual parent table to insert or update into.
  2. Type Column: Introduce a type column in the child table to differentiate between possible parent table types. The foreign key column then references the parent table corresponding to the type specified in the type column.

Examples in MySQL and PostgreSQL

In MySQL, the following statement creates a union table:

<code class="sql">CREATE TABLE union_table AS
SELECT * FROM subordinates
UNION ALL
SELECT * FROM products;</code>
Copy after login
Copy after login

The foreign key constraint in the child table would then be:

<code class="sql">ALTER TABLE images
ADD FOREIGN KEY (person_id) REFERENCES union_table(id);</code>
Copy after login
Copy after login

In PostgreSQL, a similar approach is used:

<code class="sql">CREATE TABLE union_table AS
SELECT * FROM subordinates
UNION ALL
SELECT * FROM products;</code>
Copy after login
Copy after login

The foreign key constraint becomes:

<code class="sql">ALTER TABLE images
ADD FOREIGN KEY (person_id) REFERENCES union_table(id);</code>
Copy after login
Copy after login

Conclusion

While a direct foreign key to multiple tables is not feasible, alternative strategies allow for a polymorphic foreign key relationship in SQL database systems. These strategies preserve data integrity while providing the flexibility to accommodate multiple parent tables.

The above is the detailed content of Can a Foreign Key Reference Multiple Tables in SQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template