| Attribute | Description |
|---|---|
| CONSTRAINT | The name of the constraint. Can only be coded at the table level. |
| FOREIGN KEY | The names of the columns that make up the foreign key. Can only be coded at the table level. |
| REFERENCES | The name of the related table and the column or columns. Can be coded at the column or table level. When coded at the column level, it can only refer to a single column. |
CREATE TABLE orders (
orderID PRIMARY KEY,
customerID NOT NULL
REFERENCES customers (customerID),
orderDate NOT NULL
)
CREATE TABLE orders (
orderID INT PRIMARY KEY,
customerID INT NOT NULL,
orderDate DATETIME NOT NULL,
CONSTRAINT
ordersFkCustomers FOREIGN KEY (customerID)
REFERENCES customers (customerID)
)
INSERT INTO orders
VALUES (1, 999, '2017-08-03');
Error Code: 1452.
Cannot add or update a child row: a foreign key con-straint fails ('ex'.'orders', CONSTRAINT 'ordersFkCustomers' FOREIGN KEY ('customerID') REFERENCES 'customers' ('customerID'))
CONSTRAINT ordersFkCustomers
FOREIGN KEY (customerID) REFERENCES customers (customerID)
ON DELETE CASCADE