Answer the question
In order to leave comments, you need to log in
How does ON DELETE CASCADE work in MySQL?
Hello!
I can't find and understand exactly how referential integrity works on the example of ON DELETE CASCADE in MySQL.
There is a parent table:
CREATE TABLE ReportDate
(
report_id int unsigned auto_increment NOT NULL,
report_date smalldatetime NOT NULL DEFAULT CURRENT_SMALLDATETIME(),
PRIMARY KEY (report_id)
);
CREATE TABLE Report
(
report_id int unsigned NOT NULL,
user_id int unsigned NOT NULL,
PRIMARY KEY (report_id),
FOREING KEY (report_id) REFERENCES ReportDate(report_id)
ON DELETE CASCADE,
FOREING KEY (user_id) REFERENCES UserLogin(user_id)
);
Answer the question
In order to leave comments, you need to log in
ON DELETE CASCADE - will delete a row in the child table when deleting in the main one.
SET NULL - set the ID value to NULL if you delete a row in the main table. To do this, the subtable must allow a NULL value for that column.
Constraints are set on each sub-table. Whatever you set ON DELETE CASCADE or SET NULL will happen.
Described in detail in Russian https://metanit.com/sql/mysql/2.5.php Helped
me...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question