What is FOREIGN KEY ?
What is FOREIGN KEY ?
A foreign key (FK) is a column (or columns) whose values must match a primary or unique key in another table. It enforces referential integrity: no order can point at a customer that does not exist.
CREATE TABLE customers (
id BIGINT PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
id BIGINT PRIMARY KEY,
customer_id BIGINT NOT NULL,
CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id) REFERENCES customers (id)
ON DELETE RESTRICT
ON UPDATE CASCADE
);
| Action | When the parent row is deleted/updated |
|---|---|
RESTRICT / NO ACTION |
The statement fails (default). |
CASCADE |
Child rows are deleted/updated too. |
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.