Complete the code to create a foreign key constraint linking the orders table to the customers table.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES [1](customer_id)
);The foreign key in orders references the customer_id in the customers table to ensure data integrity.
Complete the code to add a foreign key constraint to the payments table referencing the orders table.
ALTER TABLE payments
ADD CONSTRAINT fk_order FOREIGN KEY (order_id) REFERENCES [1](order_id);The foreign key order_id in payments must reference the order_id in the orders table.
Fix the error in the foreign key constraint definition by completing the code.
CREATE TABLE order_items (
item_id INT PRIMARY KEY,
order_id INT,
CONSTRAINT fk_order FOREIGN KEY (order_id) REFERENCES [1](order_id)
);The foreign key order_id in order_items must reference the order_id in the orders table, not itself or unrelated tables.
Fill both blanks to create a foreign key constraint with cascading delete from employees to departments.
CREATE TABLE employees ( emp_id INT PRIMARY KEY, dept_id INT, CONSTRAINT fk_dept FOREIGN KEY ([1]) REFERENCES departments([2]) ON DELETE CASCADE );
The foreign key column in employees is dept_id, which references department_id in departments. The ON DELETE CASCADE ensures related employees are deleted if a department is removed.
Fill all three blanks to create a foreign key constraint on enrollments referencing students with cascading update and delete.
CREATE TABLE enrollments ( enrollment_id INT PRIMARY KEY, student_id INT, CONSTRAINT fk_student FOREIGN KEY ([1]) REFERENCES [2]([3]) ON UPDATE CASCADE ON DELETE CASCADE );
The foreign key student_id in enrollments references student_id in the students table. Cascading update and delete keep data consistent when student records change or are removed.