How to define Primary Key and Foreign Key constraints in MySQL?
Establishes referential integrity with FOREIGN KEY and ON DELETE CASCADE constraints.
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
employee_id INT NOT NULL,
order_date DATE NOT NULL,
total_amount DECIMAL(10,2),
CONSTRAINT fk_employee
FOREIGN KEY (employee_id)
REFERENCES employees(employee_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
INSERT INTO orders (employee_id, order_date, total_amount)
VALUES (1, '2026-09-01', 249.99);Query OK, 0 rows affected (0.03 sec) Query OK, 1 row affected (0.01 sec)