Purpose of FOREIGN KEY in Maintaining Referential Integrity
A FOREIGN KEY ensures that values in one table correspond to valid entries in another table. Its purpose is to maintain referential integrity — meaning relationships between tables always remain valid and consistent.
Prevents inserting a child row that references a non-existing parent row.
Prevents deleting a parent row if child rows still depend on it (unless cascading is defined).
Ensures updates to a referenced key are handled safely (restricted or cascaded).
Guarantees that relationships between tables remain logically correct.
Avoids orphan records in child tables.
Keeps data consistent across related tables.
Provides structured, reliable, and predictable relationships in the database.
Supports cleaner database design with safe relational links.
With a FOREIGN KEY in place, MySQL actively enforces that any referenced data must be valid, preventing broken or inconsistent relationships.
You have a customers table and an orders table. How would you define a foreign key so each order must reference an existing customer, and what does MySQL do if you try to insert an order with a non‑existent customer_id?
If a foreign key is defined with ON DELETE RESTRICT and you attempt to delete a customer that still has orders, what will happen?
What kind of data inconsistency could arise if you forget to add a foreign key between posts and users?
After adding an ON DELETE CASCADE foreign key from orders to customers, deletes started failing. Walk me through why this might happen and how you would debug it.
Our high‑traffic orders table saw increased insert latency after we added a foreign key to customers. What factors could cause this slowdown and what options do you have to mitigate it?
During a data migration we need to temporarily disable foreign key checks. How would you safely do this in MySQL and what risks should you watch for?
Design a strategy for sharding a MySQL database that has many tables linked by foreign keys. How would you preserve referential integrity across shards, and what compromises are acceptable?
Explain the impact of foreign key constraints on bulk data loads (e.g., LOAD DATA INFILE) in a large data warehouse. How would you balance integrity versus load performance?
Our service needs to support soft deletes while maintaining referential integrity. How would you implement this using foreign keys, and what pitfalls might you encounter?
We are moving a monolithic MySQL instance with many inter‑table foreign keys to a microservices architecture with separate databases per service. How would you approach the migration while ensuring referential integrity is not broken?
Over years, foreign key relationships have created tight coupling that hinders independent scaling of services. Propose a long‑term plan to decouple data while preserving consistency.
When introducing a new global identifier system across multiple legacy MySQL schemas, how would you redesign foreign key usage to support multi‑region replication and eventual consistency?