Q5. What is the difference between a primary key and a foreign key?
A primary key uniquely identifies each row in a table: its values are unique and never NULL, and a table has exactly one. A foreign key is a column that references a primary key in another table, linking the two and enforcing that the referenced row exists.
Foreign keys give referential integrity: you can't insert a child row pointing at a missing parent, and ON DELETE rules decide what happens when a parent is removed. Both need InnoDB, since MyISAM ignores foreign keys.
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
employee_id INT,
total DECIMAL(10, 2),
FOREIGN KEY (employee_id) REFERENCES employees(id)
ON DELETE CASCADE
) ENGINE = InnoDB;Key point: The follow-up is usually 'what does ON DELETE CASCADE do?'. Have the answer ready: it deletes child rows when the parent goes.