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

What is a key in MySQL and why is it used?

In MySQL, a key is a column or a set of columns that helps identify, organize, and optimize access to data within a table. Keys are essential for ensuring data integrity and improving query performance.

Why Keys Are Used
  1. 1

    To uniquely identify rows (e.g., PRIMARY KEY).

  2. 2

    To enforce relationships between tables (e.g., FOREIGN KEY).

  3. 3

    To speed up data retrieval using indexes (any key automatically becomes an index).

  4. 4

    To prevent duplicate values when required (e.g., UNIQUE KEY).

Types of Keys in MySQL
  1. 1

    Primary Key – Uniquely identifies each row and cannot contain NULL.

  2. 2

    Unique Key – Ensures all values are distinct but can contain NULLs.

  3. 3

    Foreign Key – Enforces referential integrity between related tables.

  4. 4

    Index Key – Used to improve search performance on selected columns.

  5. 5

    Composite Key – A key made of more than one column.

Example: Defining Keys

Keys are central to relational database design—they ensure correctness of data and significantly improve query efficiency by allowing MySQL to quickly locate rows instead of scanning entire tables.