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

How do you disable and re-enable foreign key checks in MySQL? Why would you do that?

Disabling and Re-Enabling Foreign Key Checks in MySQL

MySQL allows disabling foreign key checks temporarily, which is useful when performing bulk operations that would otherwise violate referential integrity.

1. Disable Foreign Key Checks
  1. 1

    Disables enforcement of foreign key constraints.

  2. 2

    Allows operations that would normally fail (e.g., deleting parent rows first, loading data out of order).

Command
2. Re-Enable Foreign Key Checks
  1. 1

    Restores the enforcement of foreign key constraints.

  2. 2

    MySQL will start validating new operations again.

Command
3. Why Disable Foreign Key Checks?
  1. 1

    Bulk data import → Load data faster without constraint checks.

  2. 2

    Dropping or truncating tables that are referenced by foreign keys.

  3. 3

    Reordering inserts → Useful when parent rows are inserted after child rows during migration.

  4. 4

    Changing schema (e.g., altering columns involved in foreign keys).

Foreign key checks should only be disabled temporarily. Leaving them off can break referential integrity and create invalid relationships.

Difficulty: 4/10
Topics: foreign key constraints, session variables, data migration

Scenario Questions

0-2 years experience
  1. 1

    You need to bulk load a CSV into a table that has foreign key constraints. How would you temporarily disable the foreign key checks, load the data, and then re‑enable them?

  2. 2

    What command would you run to turn off foreign key checks for the current session, and why might you need to do that before running an ALTER TABLE that drops a foreign key?

  3. 3

    If you forget to re‑enable foreign key checks after a data import, what could happen when you run subsequent inserts?

2-5 years experience
  1. 1

    During a nightly ETL job, inserts into a fact table fail due to foreign key violations, but you know the source data is correct. How would you investigate and decide whether to disable foreign key checks for that job?

  2. 2

    You are refactoring a legacy schema by splitting a large table into two, yet the existing data has circular references. Explain how you would use disabling foreign key checks to perform the migration safely, and what steps you would take to ensure data integrity.

  3. 3

    While running a data migration script, you encounter a deadlock caused by foreign key cascades. How could disabling foreign key checks help, and what are the trade‑offs of doing so?

5-8 years experience
  1. 1

    Our platform processes billions of rows nightly and we need to perform bulk updates across multiple tables with foreign keys. Design a strategy that uses disabling/re‑enabling foreign key checks to minimize downtime while guaranteeing consistency, and discuss any additional safeguards you would add.

  2. 2

    We have a multi‑tenant MySQL cluster where each tenant can run schema migrations independently. How would you coordinate disabling foreign key checks across tenants to avoid cross‑tenant data corruption, and what monitoring would you implement?

  3. 3

    Explain the performance implications of leaving foreign key checks disabled for an extended period in a high‑traffic service. How would you detect and mitigate any risks introduced by this practice?

8+ years experience
  1. 1

    Our organization is planning a phased migration from MySQL to a distributed SQL system. Part of the plan involves temporarily disabling foreign key checks during data syncs. How would you architect the migration process to ensure that disabling constraints does not lead to long‑term referential integrity issues across services?

  2. 2

    Several teams rely on shared reference tables with foreign keys. Propose a governance model for when and how foreign key checks can be disabled in production, including policy, tooling, and audit trails.

  3. 3

    During a major feature rollout, you need to backfill data across dozens of tables with complex relationships. How would you design an automated pipeline that safely toggles foreign key checks, validates the data post‑load, and rolls back if inconsistencies are found?

Follow-up Questions

  • What is the difference between disabling foreign key checks at the session level versus the global level?
  • After re‑enabling the checks, how would you confirm that the data still satisfies all foreign key constraints?
  • Can you describe a situation where disabling foreign key checks might introduce more problems than it solves?