I have a DB on a server, and I would create these tables with constraints. This is the code for the "child table" of the relation N:M
CREATE TABLE IF NOT EXISTS `Sql183209_2`.`ArtistiXGruppi` (
`idArtistaXGruppo` INT NOT NULL AUTO_INCREMENT,
`Artista` INT NOT NULL,
`Gruppo` INT NOT NULL,
`CapoOrchestra` TINYINT(1) NULL,
`KmDaSede` FLOAT NULL,
PRIMARY KEY (`idArtistaXGruppo`),
INDEX `Gruppo_idx` (`Gruppo` ASC),
UNIQUE INDEX `uniqe` (`Artista` ASC, `Gruppo` ASC),
INDEX `Artista_idy` (`Artista` ASC),
CONSTRAINT `Artista`
FOREIGN KEY (`Artista`)
REFERENCES `Sql183209_2`.`Artisti` (`idArtista`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `Gruppo`
FOREIGN KEY (`Gruppo`)
REFERENCES `Sql183209_2`.`Gruppi` (`idGruppo`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
and here is the code of the other two tables.
CREATE TABLE IF NOT EXISTS `Sql183209_2`.`Artisti` (
`idArtista` INT NOT NULL AUTO_INCREMENT,
`Codice` VARCHAR(45) NULL,
`Matricola` VARCHAR(45) NULL,
`CodiceFiscale` VARCHAR(16) NULL,
PRIMARY KEY (`idArtista`),
UNIQUE INDEX `CodiceFiscale_UNIQUE` (`CodiceFiscale` ASC),
UNIQUE INDEX `Codice_UNIQUE` (`Codice` ASC),
UNIQUE INDEX `Matricola_UNIQUE` (`Matricola` ASC))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `Sql183209_2`.`Gruppi` (
`idGruppo` INT NOT NULL,
`Codice` VARCHAR(45) NOT NULL,
`Nome` VARCHAR(100) NULL,
PRIMARY KEY (`idGruppo`),
UNIQUE INDEX `Codice_UNIQUE` (`Codice` ASC),
CONSTRAINT `agenzia`
FOREIGN KEY (`Agenzia`)
REFERENCES `Sql183209_2`.`Agenzie` (`idAgenzia`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I have made some inserts in tables Artisti and Gruppi. When I insert some data in the ArtistiXGruppi table and after I'm deleting one of Artist or Group rows in the related table I expect that in the ArtistiXGruppi table will be deleted the connected rows. But it doesn't work! Why?
Thanks to all and sorry for my English!
if you want to delete the related record in foreign key user
ON DELETE CASCADE
may be because you used
ON DELETE NO ACTION
mysql> drop table user;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE USER
-> (
-> id int,
-> name VARCHAR(50),
-> gender VARCHAR(2),
-> dateofbirth DATE,
-> song_id int,
-> ratings int
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> drop table song;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE song
-> (
-> id int primary key ,
-> name VARCHAR(50),
-> lengthofsong int,
-> album_id int
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> ALTER TABLE USER ADD CONSTRAINT fk_song_id FOREIGN KEY(song_id) REFERENC
ES song(id) on delete cascade;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into song values(1,'AA',1,1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into user values (1,'UU','ma',null,1,1);
Query OK, 1 row affected (0.00 sec)
mysql> delete from song where id = 1;
Query OK, 1 row affected (0.00 sec)
mysql> select * from user;
Empty set (0.00 sec)
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.