This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-14. For the latest, interactive version please visit https://mariadb.com/kb/.

CONSTRAINT

Spiegazione

InnoDB supporta i vincoli di integrità (CONSTRAINT) per le chiavi esterne. La sintassi per definirli è la seguente:

[CONSTRAINT [simbolo]] FOREIGN KEY
    [nome_indice] (nome_colonna, ...)
    REFERENCES nome_tabella (nome_colonna,...)
    [ON DELETE opzione]
    [ON UPDATE opzione]

opzioni:
    RESTRICT | CASCADE | SET NULL | NO ACTION

Esempi:

CREATE TABLE prodotto (categoria INT NOT NULL, id INT NOT NULL,
                      prezzo DECIMAL,
                      PRIMARY KEY(categoria, id)) ENGINE=INNODB;
CREATE TABLE cliente (id INT NOT NULL,
                     PRIMARY KEY (id)) ENGINE=INNODB;
CREATE TABLE ordine_prodotto (no INT NOT NULL AUTO_INCREMENT,
                             categoria_prodotto INT NOT NULL,
                             id_prodotto INT NOT NULL,
                             id_cliente INT NOT NULL,
                             PRIMARY KEY(no),
                             INDEX (categoria_prodotto, id_prodotto),
                             FOREIGN KEY (categoria_prodotto, id_prodotto)
                               REFERENCES prodotto(categoria, id)
                               ON UPDATE CASCADE ON DELETE RESTRICT,
                             INDEX (id_cliente),
                             FOREIGN KEY (id_cliente)
                               REFERENCES cliente(id)) ENGINE=INNODB;
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.