How can foreign keys be properly implemented in MySQL tables referencing multiple primary keys?
When implementing foreign keys in MySQL tables referencing multiple primary keys, you can use a composite key in the child table that includes all the primary keys from the parent table. This ensures that each combination of primary keys in the parent table has a corresponding reference in the child table.
CREATE TABLE parent_table (
primary_key1 INT,
primary_key2 INT,
PRIMARY KEY (primary_key1, primary_key2)
);
CREATE TABLE child_table (
primary_key1 INT,
primary_key2 INT,
FOREIGN KEY (primary_key1, primary_key2) REFERENCES parent_table(primary_key1, primary_key2)
);
Related Questions
- What role does PHP play in fetching data from a database and updating it on a webpage using jQuery and Ajax?
- How can PHP developers avoid the "parse error" mentioned in the forum thread while working on login scripts?
- How can a two-dimensional array be created in PHP with one index representing column names and the other index representing row IDs?