简体   繁体   中英

Delete cascade MySQL

I have table prodottipublic :

表格1

and I have table roles_prodotti :

表2

I want, when delete record in prodottipublic with id: 10 for example, automatically delete all records with column prodotto_id same 10 in roles_prodotti table.

this is create table codes:

CREATE TABLE `prodottipublic` (  `id` int(11) UNSIGNED ZEROFILL NOT NULL,  `attivo` int(1) NOT NULL,  `nome` varchar(100) DEFAULT NULL,  `immagine` varchar(150) DEFAULT NULL,  `immaginesx` varchar(150) DEFAULT NULL,  `descrizione` text NOT NULL,  `sololistino` int(1) NOT NULL DEFAULT '0',  `listino` decimal(10,2) NOT NULL,  `sconto` int(2) DEFAULT '0',  `prezzo` decimal(10,2) DEFAULT '0.00',  `categoria_id` int(11) NOT NULL,  `meta_keywords`text,  `attributi` json NOT NULL,  `created_at` timestamp NULL DEFAULT NULL,  `updated_at` timestamp NULL DEFAULT NULL) ENGINE=InnoDB DEFAULTCHARSET=utf8;


CREATE TABLE `roles_prodotti` (  `prodotto_id` int(11)  UNSIGNED ZEROFILL NOT NULL,  `role_id` nt(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;

i have solved with this:

ALTER TABLE `roles_prodotti` ADD CONSTRAINT ID_prodotto FOREIGN KEY (`prodotto_id`) REFERENCES `prodottipublic`(`id`)ON DELETE CASCADE

and before have empty roles_prodotti

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM