MariaDB/MySQL foreign key constraint: possible to request cascade at time of delete? -
i have used php code preserve database integrity years, switching myisam innodb , thought might nice utilize foreign key constraints, letting db carry more of load. want confirm user before doing cascade, constraints declared on delete restrict
. when error, let user know there dependent records , how many, , if say, "sure, delete them," nice let database cascading delete. possible tell specific delete
statement go ahead , cascade? expected option or on delete
command (e.g. pseudocode delete table ... cascade child-table
), 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 cascade-delete temporarily or on-demand? but: (a) it's sqlserver, not mysql (well, technically i'm using mariadb 10.2.4, if makes difference), don't know if have additional options available me, , (b) such stored procedure code wouldn't simpler php code have (and less visible when i'm developing), don't see point in swapping 1 other.
short answer: no.
longer answer:
the answer simple-minded: fks simple-minded. when ask more trivial actions, asking of fks, , need build "business logic" application.
ditto triggers.
mysql (and mariadb) have been "lean , mean" compared heavy hitters. fks exist check on feature list "yes, have fks, too". so, esoteric in details of fks quite missing.
sometimes syntax implemented without real code behind -- check
; index(x desc)
. (the latter being implemented in 8.0, estimate number of use cases somewhere around 1 in thousand.)
Comments
Post a Comment