MySQL Database Quiz Questions

Course Name:MySQL
Chapter Name:Chapter 8 - RDBMS Concepts
Lesson Content Link:Foreign Key
Current Quiz Count:30
Progress
0%
Q1
True / False

foreign key in MySQL can reference a primary key in another table.

Q2
True / False

MySQL, a foreign key can only reference columns in the same table.

Q3
True / False

You cannot create a foreign key constraint if the referenced column does not have a unique index in MySQL.

Q4
True / False

MySQL, a table can have multiple foreign key constraints.

Q5
True / False

Dropping a referenced table automatically removes the foreign key constraints in MySQL.

Q6
True / False

MySQL supports cascading updates and deletes for foreign key constraints.

Q7
True / False

MySQL, foreign key constraints can be temporarily disabled.

Q8
True / False

Foreign key constraints in MySQL cannot be defined in InnoDB tables.

Q9
True / False

MySQL, the REFERENCES keyword is mandatory when defining a foreign key constraint.

Q10
True / False

MySQL's foreign key constraints ensure that the referenced data remains consistent, and it prevents operations that would result in an invalid state.

Q11
Single Choice

Which SQL keyword is used to define a foreign key in MySQL?

Q12
Single Choice

What is the purpose of a foreign key in MySQL?

Q13
Single Choice

Which MySQL statement is used to add a foreign key to an existing table?

Q14
Single Choice

In MySQL, what happens if you try to delete a record that is referenced by a foreign key in another table without specifying ON DELETE behavior?

Q15
Single Choice

Which MySQL option can be used to automatically delete or update rows in a child table when the corresponding row in the parent table is deleted or updated?

Q16
Single Choice

How do you define a foreign key constraint with an ON DELETE CASCADE action in MySQL?

Q17
Single Choice

Which MySQL storage engine supports foreign key constraints?

Q18
Single Choice

What must be true about the columns involved in a foreign key relationship in MySQL?

Q19
Single Choice

Which MySQL error is returned if you try to insert a value into a child table that does not exist in the parent table?

Q20
Single Choice

When creating a foreign key constraint in MySQL, which clause ensures that the changes made to the parent table are automatically reflected in the child table?

Q21
Multiple Choice

Which SQL statement correctly creates a foreign key relationship between the 'orders' table and the 'customers' table?

SQL Code
CREATE TABLE orders (
 order_id INT AUTO_INCREMENT,
 customer_id INT,
 order_date DATE,
 PRIMARY KEY (order_id),
 FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
Q22
Multiple Choice

Identify the correct SQL code that defines a foreign key with cascading delete behavior.

SQL Code
CREATE TABLE orders (
 order_id INT AUTO_INCREMENT,
 customer_id INT,
 PRIMARY KEY (order_id),
 FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
 ON DELETE CASCADE
);
Q23
Multiple Choice

Which SQL code demonstrates the correct use of foreign keys to enforce referential integrity between 'employees' and 'departments' tables?

SQL Code
CREATE TABLE employees (
 employee_id INT AUTO_INCREMENT,
 department_id INT,
 employee_name VARCHAR(100),
 PRIMARY KEY (employee_id),
 FOREIGN KEY (department_id) REFERENCES departments(department_id)
);
Q24
Multiple Choice

Choose the SQL statement that correctly alters an existing table to add a foreign key constraint.

SQL Code
ALTER TABLE orders
ADD CONSTRAINT fk_customer
FOREIGN KEY (customer_id)
REFERENCES customers(customer_id);
Q25
Multiple Choice

What SQL code would you use to define a foreign key that prevents deletion in the parent table if dependent records exist?

SQL Code
CREATE TABLE orders (
 order_id INT AUTO_INCREMENT,
 customer_id INT,
 PRIMARY KEY (order_id),
 FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
 ON DELETE RESTRICT
);
Q26
Multiple Choice

Which SQL code correctly sets up a foreign key relationship in a junction table for a many-to-many relationship?

SQL Code
CREATE TABLE student_courses (
 student_id INT,
 course_id INT,
 PRIMARY KEY (student_id, course_id),
 FOREIGN KEY (student_id) REFERENCES students(student_id),
 FOREIGN KEY (course_id) REFERENCES courses(course_id)
);
Q27
Multiple Choice

Select the SQL code that defines a foreign key with a SET NULL action on delete in the 'payments' table.

SQL Code
CREATE TABLE payments (
 payment_id INT AUTO_INCREMENT,
 order_id INT,
 amount DECIMAL(10, 2),
 PRIMARY KEY (payment_id),
 FOREIGN KEY (order_id) REFERENCES orders(order_id)
 ON DELETE SET NULL
);
Q28
Multiple Choice

Which SQL code is used to create a foreign key with a custom constraint name in the 'enrollments' table?

SQL Code
CREATE TABLE enrollments (
 enrollment_id INT AUTO_INCREMENT,
 student_id INT,
 course_id INT,
 PRIMARY KEY (enrollment_id),
 CONSTRAINT fk_student FOREIGN KEY (student_id) REFERENCES students(student_id)
);
Q29
Multiple Choice

Identify the SQL code that prevents orphaned records by using foreign key constraints in the 'borrowings' table.

SQL Code
CREATE TABLE borrowings (
 borrowing_id INT AUTO_INCREMENT,
 book_id INT,
 member_id INT,
 PRIMARY KEY (borrowing_id),
 FOREIGN KEY (book_id) REFERENCES books(book_id)
 ON DELETE CASCADE,
 FOREIGN KEY (member_id) REFERENCES members(member_id)
 ON DELETE CASCADE
);
Q30
Multiple Choice

Which SQL code correctly defines a foreign key relationship and enforces referential integrity using a primary key and foreign key in the 'reviews' table?

SQL Code
CREATE TABLE reviews (
 review_id INT AUTO_INCREMENT,
 product_id INT,
 customer_id INT,
 review_text TEXT,
 PRIMARY KEY (review_id),
 FOREIGN KEY (product_id) REFERENCES products(product_id),
 FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);