Hello can someone help me here.
I would like to delete from multiple tables.
I want to delete id_grupo = 6
from grupos
.
But when I delete it I would like to delete from gerir_posts where id_grupo = 6
. But from posts
, id_post = 4
, that's the same id from gerir_posts id_post
; I want delete it too.
So what i realy want is when i remove a grup , the posts that are in that grup most be deleted too.
This is a example of my db.
CREATE TABLE Utilizadores
(
id_utilizador int auto_increment not null primary key,
Nome varchar(50)
);
INSERT INTO utilizadores VALUES (1,'Admin');
CREATE TABLE Grupos
(
id_grupo int auto_increment not null primary key,
Nome_grupo varchar(50)
);
INSERT INTO grupos VALUES (1,'Grupo');
CREATE TABLE Gerir_grupos
(
id_gerir_grupo int auto_increment not null primary key,
id_grupo int,
id_utilizador int,
FOREIGN KEY (id_utilizador) references Utilizadores(id_utilizador),
FOREIGN KEY (id_grupo) references Grupos(id_grupo) on delete cascade
);
INSERT INTO gerir_grupos VALUES (1,1,1);
CREATE TABLE Posts
(
id_post int auto_increment not null primary key,
id_utilizador int,
Titulo_do_post varchar(50),
Corpo_do_post varchar(500),
FOREIGN KEY (id_utilizador) references Utilizadores (id_utilizador)
);
INSERT INTO posts VALUES (1,1,"teste","grupo teste");
CREATE TABLE Gerir_posts
(
id_gerir_post int auto_increment not null primary key,
id_post int,
id_grupo int,
FOREIGN KEY (id_post) references Posts (id_post) on delete cascade,
FOREIGN KEY (id_grupo) references Grupos (id_grupo)on delete cascade
);
INSERT INTO gerir_posts VALUES (1,1,1);
If this can help