OsakaWebbie OsakaWebbie - 3 years ago 124
MySQL Question

MariaDB/MySQL foreign key constraint: possible to request cascade at time of delete?

I have used PHP code to preserve database integrity for years, but now I am switching from MyISAM to InnoDB and thought it might be nice to utilize foreign key constraints, letting the DB carry more of the load. But I want to confirm with the user before doing a cascade, so the constraints would be declared as

ON DELETE RESTRICT
. When I get the error, I would let the user know that there are dependent records and how many, and if they say, "Sure, delete them," it would be nice to let the database do a cascading delete. Is it possible to tell a specific
DELETE
statement to go ahead and cascade? I expected an option or something on the
DELETE
command (e.g. pseudocode
DELETE FROM table WHERE ... CASCADE TO child-table
), but I didn't see anything.

Example (very standard many-to-many):

CREATE TABLE `person` (
`PersonID` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`FullName` varchar(100) CHARACTER SET utf8mb4 NOT NULL DEFAULT '',
<many other fields>,
PRIMARY KEY (`PersonID`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `category` (
`CategoryID` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`Category` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`CategoryID`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `percat` (
`PersonID` mediumint(8) unsigned NOT NULL DEFAULT 0,
`CategoryID` mediumint(8) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`PersonID`,`CategoryID`),
FOREIGN KEY (`PersonID`) REFERENCES `person`(`PersonID`) ON DELETE RESTRICT,
FOREIGN KEY (`CategoryID`) REFERENCES `category`(`CategoryID`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=ascii COLLATE=ascii_bin;


I found How to cascade-delete temporarily or on-demand? but: (a) it's for SQLServer, not MySQL (well, technically I'm using MariaDB 10.2.4, if that makes a difference), so I don't know if I have additional options available to me, and (b) such stored procedure code wouldn't be any simpler than the PHP code I already have (and less visible when I'm developing), so I don't see the point in swapping one for the other.

Answer Source

Short answer: No.

Longer answer:

The answer is simple-minded: FKs are simple-minded. When you ask for more than trivial actions, you are asking for too much of FKs, and you need to build the "business logic" into your application.

Ditto for Triggers.

MySQL (and MariaDB) have always been "lean and mean" compared to the heavy hitters. FKs exist as a check on a feature list "yes, we have FKs, too". So, anything esoteric in the details of FKs are quite likely missing.

Sometimes the syntax is implemented without any real code behind it -- CHECK; INDEX(x DESC). (The latter is finally being implemented in 8.0, but I would estimate the number of use cases to be somewhere around one in a thousand.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download