Visualizzazione dei risultati da 1 a 3 su 3

Discussione: mysql - tabelle con FK

  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2004
    Messaggi
    323

    mysql - tabelle con FK

    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

  2. #2
    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.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2004
    Messaggi
    323
    grazie, ciao.

    Devo leggere di + la doc

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.