Salve a tutti, io ho 5 tabelle e tutte relazionate tra di loro con Foreign Key. La mia domanda é: se io voglio cancellare un record in una tabella, vorrei che i record vengono cancellati anche nelle altre tabelle?
Come posso fare?
grazie a tutti
![]()
Salve a tutti, io ho 5 tabelle e tutte relazionate tra di loro con Foreign Key. La mia domanda é: se io voglio cancellare un record in una tabella, vorrei che i record vengono cancellati anche nelle altre tabelle?
Come posso fare?
grazie a tutti
![]()
Dovresti innanzitutto leggere la documentazione in MySQL sulle chiavi esterne: infatti se l'avessi fatto sicuramente non avresti postato.
Cmq. nel manuale dice:
The syntax for a foreign key constraint definition in InnoDB looks like this:
[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)
REFERENCES tbl_name (index_col_name, ...)
[ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
[ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
praticamente quando crei una chiave esterna devi impostare ON DELETE su CASCADE
Infatti sempre nel MANUALE e sottolineo MANUALE c'è scritto:
CASCADE: Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column in the parent table or in the child table
Ciao.
grazie, ciao.
Devo leggere di + la doc![]()