Questions
26 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?
26 / 26

What 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?

Deferred Constraints, Many-to-Many Key Design, and Performance Impacts in MySQL

These topics relate to how MySQL handles constraint timing, relational schema design, and performance behavior in systems with many keys and relationships.

1. Deferred Constraint Checks — Does MySQL Support Them?
  1. 1

    Deferred constraints allow validation to occur at the end of a transaction instead of immediately.

  2. 2

    This feature is common in PostgreSQL and Oracle for complex multi-step updates.

  3. 3

    MySQL does NOT support deferred constraint checks — all foreign key checks are immediate.

  4. 4

    Foreign key validation in MySQL happens on each INSERT, UPDATE, and DELETE statement.

  5. 5

    This means MySQL can fail statements earlier, but cannot temporarily allow inconsistent states within a transaction.

In summary: MySQL enforces all foreign key constraints immediately and does not support DEFERRABLE constraints.

2. Designing Keys in a Many-to-Many Relationship (Junction / Bridge Table)
  1. 1

    A many-to-many relationship is implemented using a junction table.

  2. 2

    The junction table typically contains two foreign keys referencing parent tables.

  3. 3

    These two foreign keys together usually form a composite primary key to enforce uniqueness of the relationship.

  4. 4

    Each foreign key column must be indexed (automatically handled by MySQL when part of the primary key).

Example: Standard Many-to-Many Key Design
3. Performance Implications of Multiple Foreign Keys and Composite Keys
  1. 1

    Write performance cost: Each insert/update/delete must maintain multiple indexes.

  2. 2

    More foreign keys = more validation work on every DML operation.

  3. 3

    Composite keys enlarge the size of secondary indexes (because InnoDB stores PK columns inside secondary indexes).

  4. 4

    Large composite primary keys slow down indexing, since all secondary index entries become wider.

  5. 5

    Cascading operations (ON UPDATE/ON DELETE CASCADE) can lead to large update or delete chains.

  6. 6

    Join performance often improves when tables are well-indexed, but this comes with a write overhead.

  7. 7

    If foreign keys reference large parent tables, locking contention may increase during writes.

  8. 8

    With many foreign keys, referential checks can become a significant cost in high-volume OLTP systems.

4. Practical Recommendations
  1. 1

    Use small, numeric primary keys to minimize composite key size.

  2. 2

    Avoid unnecessary foreign keys on very high-write tables (consider application-level integrity).

  3. 3

    Keep composite primary keys narrow; otherwise, secondary indexes grow too large.

  4. 4

    Use ON DELETE/UPDATE CASCADE sparingly, especially on high-traffic parent tables.

  5. 5

    Consider surrogate primary keys (INT/BIGINT) for performance, but enforce logical uniqueness using UNIQUE keys.

In summary, MySQL does not support deferred constraints, many-to-many tables use a composite key of foreign keys, and heavy use of foreign keys and composite keys can significantly impact write performance and index size in large-scale systems.

Difficulty: 8/10
Topics: deferred constraints, junction table design, foreign key performance

Scenario Questions

0-2 years experience
  1. 1

    You need to model a many‑to‑many relationship between users and roles in MySQL. How would you define the bridge table and its primary and foreign keys?

  2. 2

    If you insert a row into that bridge table with a user_id that doesn't exist, what error does MySQL return and why?

  3. 3

    MySQL doesn't have deferred constraints. If you try to insert a parent row and its child row in the same transaction, what happens?

2-5 years experience
  1. 1

    During a bulk CSV import of order‑item pairs into a junction table, inserts start failing with foreign‑key errors. How would you troubleshoot and resolve the issue?

  2. 2

    You notice occasional deadlocks when inserting into three related tables within a transaction. Explain how the order of foreign‑key checks could be contributing, and whether deferring constraints would help in MySQL.

  3. 3

    What are the trade‑offs of using a composite primary key (user_id, role_id) versus an auto‑increment surrogate key in the bridge table?

5-8 years experience
  1. 1

    Your service processes millions of user‑role assignments and insert latency on the junction table has risen. Which indexes and foreign‑key configurations would you examine, and how would you benchmark improvements?

  2. 2

    Design a strategy to enforce referential integrity for a many‑to‑many relationship when you need to temporarily disable constraints for bulk loads, given MySQL's lack of deferred checks.

  3. 3

    Discuss the performance implications of having several foreign keys on a large composite key in a high‑traffic table, and propose a schema redesign to reduce write contention.

8+ years experience
  1. 1

    At a company‑wide redesign you consider replacing multiple many‑to‑many tables with a single polymorphic association to cut down foreign‑key count. How would you evaluate the impact on query performance, data integrity, and future migrations?

  2. 2

    Explain how you would create a migration path for a legacy MySQL system that currently relies on application‑level checks because MySQL lacks deferred constraints, moving to a platform that does support them.

  3. 3

    How would you establish organization‑wide guidelines for choosing composite keys versus surrogate keys across services to ensure consistent performance and maintainability at scale?

Follow-up Questions

  • What metrics would you watch after changing the key strategy?
  • How would you handle existing rows that violate the new foreign‑key rules?
  • Can you compare the pros and cons of a surrogate key versus a composite primary key in this bridge table?