Q1
True / Falseforeign key in MySQL can reference a primary key in another table.
MySQL, a foreign key is used to link two tables together. It refers to the primary key in another table, establishing a relationship between the tables.
Q2
True / FalseMySQL, a foreign key can only reference columns in the same table.
foreign key in MySQL is designed to reference columns in another table, not the same table.
Q3
True / FalseYou cannot create a foreign key constraint if the referenced column does not have a unique index in MySQL.
MySQL requires that the referenced column must be indexed, typically as a primary key or unique key, for a foreign key constraint to be created.
Q4
True / FalseMySQL, a table can have multiple foreign key constraints.
MySQL allows a table to have multiple foreign key constraints, each referencing different tables or different columns within the same table.
Q5
True / FalseDropping a referenced table automatically removes the foreign key constraints in MySQL.
MySQL, you must first drop the foreign key constraints before dropping the referenced table to avoid errors.
Q6
True / FalseMySQL supports cascading updates and deletes for foreign key constraints.
MySQL supports cascading operations, such as ON DELETE CASCADE and ON UPDATE CASCADE, which automatically propagate changes to foreign key references.
Q7
True / FalseMySQL, foreign key constraints can be temporarily disabled.
You can temporarily disable foreign key constraints in MySQL by setting FOREIGN_KEY_CHECKS = 0
Q8
True / FalseForeign key constraints in MySQL cannot be defined in InnoDB tables.
MySQL supports foreign key constraints primarily in InnoDB tables, which is a transactional storage engine.
Q9
True / FalseMySQL, the REFERENCES keyword is mandatory when defining a foreign key constraint.
When defining a foreign key constraint in MySQL, the REFERENCES keyword is used to specify the table and column being referenced.
Q10
True / FalseMySQL's foreign key constraints ensure that the referenced data remains consistent, and it prevents operations that would result in an invalid state.
MySQL uses foreign key constraints to enforce referential integrity, ensuring that relationships between tables remain consistent and valid.
Q21
Multiple ChoiceWhich 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)
);
A foreign key is used to establish a link between two tables by referencing the primary key of another table. The correct SQL statement defines this relationship clearly.
Q22
Multiple ChoiceIdentify 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
);
In MySQL, the 'ON DELETE CASCADE' clause ensures that when a record in the parent table (customers) is deleted, the corresponding records in the child table (orders) are automatically deleted.
Q23
Multiple ChoiceWhich 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)
);
Referential integrity ensures that a foreign key value in the child table must match a primary key value in the parent table, preventing orphaned records.
Q25
Multiple ChoiceWhat 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
);
The 'ON DELETE RESTRICT' clause ensures that if there are any dependent records in the child table, the corresponding record in the parent table cannot be deleted.
Q26
Multiple ChoiceWhich 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)
);
In a many-to-many relationship, a junction table is used to link the two tables. The primary key of the junction table is typically a composite key, made up of the foreign keys from both related tables.
Q27
Multiple ChoiceSelect 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
);
The 'ON DELETE SET NULL' clause ensures that when a record in the parent table is deleted, the corresponding foreign key in the child table is set to NULL, maintaining referential integrity without deleting the record in the child table.
Q28
Multiple ChoiceWhich 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)
);
In MySQL, you can define a custom name for a foreign key constraint using the CONSTRAINT keyword followed by the custom name. This helps in better identifying and managing constraints in large databases.
Q29
Multiple ChoiceIdentify 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
);
Orphaned records occur when a record in a child table references a non-existent record in the parent table. The 'ON DELETE CASCADE' clause ensures that related records are deleted together, preventing orphaned records.
Q30
Multiple ChoiceWhich 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)
);
Foreign keys are essential for enforcing referential integrity, ensuring that data in one table matches data in another. The SQL code provided correctly sets up these relationships in the 'reviews' table.