Questions
10 of 26
1What is a key in MySQL and why is it used?
2What is the difference between a key and an index?
3What is a primary key? Can a table have more than one primary key?
4What is a foreign key and why is it important in relational databases?
5How do you define a primary key when creating a table?
6Can a primary key column contain NULL values? Why or why not?
7What is the difference between a UNIQUE key and a PRIMARY key?
8What is an AUTO_INCREMENT key, and how does it work with the primary key?
9Can two tables have the same primary key name? Explain.
10What is the purpose of the FOREIGN KEY constraint in maintaining referential integrity?
11What is a composite key? How do you define one in MySQL?
12What are candidate keys, and how do they differ from alternate keys?
13What happens if you try to insert a duplicate value into a UNIQUE key column?
14How do foreign key constraints behave on DELETE and UPDATE operations (CASCADE, SET NULL, etc.)?
15What are the differences between ON DELETE CASCADE and ON DELETE SET NULL? What is a super key, and how does it relate to candidate keys? Can a foreign key reference a non-primary key column in another table?
16How do you disable and re-enable foreign key checks in MySQL? Why would you do that?
17How do you drop a primary key or foreign key from an existing table?
18How can you find all foreign key relationships in a MySQL database?
19How does InnoDB enforce foreign key constraints internally?
20What is the impact of defining multiple UNIQUE keys on the same table?
21Can you define a foreign key constraint referencing a table in a different database schema?
22Explain how key definitions affect indexing and query optimization.
23What are the differences between logical keys (like candidate or composite keys) and physical indexes?
24Can a composite key contain a foreign key as one of its columns? Provide an example.
25How does MySQL handle updates to parent keys that are referenced by foreign keys?
26What are deferred constraint checks, and does MySQL support them? How do you design keys in a many-to-many relationship using a junction (bridge) table? What are the potential performance implications of using multiple foreign keys and composite keys in large-scale systems?
10 / 26

What is the purpose of the FOREIGN KEY constraint in maintaining referential integrity?

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.

1. How FOREIGN KEY Maintains Referential Integrity
  1. 1

    Prevents inserting a child row that references a non-existing parent row.

  2. 2

    Prevents deleting a parent row if child rows still depend on it (unless cascading is defined).

  3. 3

    Ensures updates to a referenced key are handled safely (restricted or cascaded).

  4. 4

    Guarantees that relationships between tables remain logically correct.

2. Benefits of Referential Integrity
  1. 1

    Avoids orphan records in child tables.

  2. 2

    Keeps data consistent across related tables.

  3. 3

    Provides structured, reliable, and predictable relationships in the database.

  4. 4

    Supports cleaner database design with safe relational links.

Example: FOREIGN KEY Maintaining Integrity

With a FOREIGN KEY in place, MySQL actively enforces that any referenced data must be valid, preventing broken or inconsistent relationships.

Difficulty: 5/10
Topics: referential integrity, cascade actions, performance impact

Scenario Questions

0-2 years experience
  1. 1

    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?

  2. 2

    If a foreign key is defined with ON DELETE RESTRICT and you attempt to delete a customer that still has orders, what will happen?

  3. 3

    What kind of data inconsistency could arise if you forget to add a foreign key between posts and users?

2-5 years experience
  1. 1

    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.

  2. 2

    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?

  3. 3

    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?

5-8 years experience
  1. 1

    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?

  2. 2

    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?

  3. 3

    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?

8+ years experience
  1. 1

    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?

  2. 2

    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.

  3. 3

    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?

Follow-up Questions

  • What are the trade‑offs between using ON DELETE CASCADE and handling deletes in application code?
  • How would you monitor or detect referential integrity violations in a production MySQL cluster?
  • If you needed to drop a large foreign key with minimal downtime, what steps would you take?