I have a ‘look-up’ table with pre-set values (was informed it’s better than ENUM)
CREATE TABLE tbl_payment_type (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
description VARCHAR(25)
);
INSERT INTO tbl_payment_type
(description)
VALUES
('PAYPAL'),
('DEBIT CARD'),
('CREDIT CARD'),
('CASH ON DELIVERY');
Now, each of my Orders contains a payment type.
CREATE TABLE tbl_order (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
...
payment_type INT,
FOREIGN KEY(payment_type) REFERENCES tbl_payment_type(id)
) AUTO_INCREMENT = 1000;
I am currently using MyISAM but might try out InnoDB in the future (I’m unsure if that affects the question).
And finally, the question is, if I try to delete a record from tbl_payment_type, will that cause MySQL to automatically delete all records from tbl_order that contained this payment type?
MyISAM doesn’t support foreign keys so nothing happens while using this storage provider.
InnoDB supports foreign keys, and by default it will restrict DELETE and UPDATES in no foreign matches. This behavior can be changed with by adding ON DELETE and ON UPDATE options. See FOREIGN KEY Constraints on the MySQL website.