When I'm going to create a foreign key type relationship between two tables in MySQL, I can specify some additional options in the ON UPDATE and ON DELETE events that are associated with the change and deleting records.
The options are:
RESTRICT
CASCADE
SET NULL
NO ACTION
A practical illustration example using the NO ACTION
option, see below:
CREATE TABLE 'usuariorelsupermercado' (
'idUsuario' INT(11) NOT NULL,
'idSupermercado' INT(11) NOT NULL,
INDEX 'fk_usuario_rel' ('idUsuario'),
INDEX 'fk_supermercado_rel' ('idSupermercado'),
CONSTRAINT 'fk_supermercado_rel' FOREIGN KEY ('idSupermercado') REFERENCES 'supermercado' ('idSupermercado') ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT 'fk_usuario_rel' FOREIGN KEY ('idUsuario') REFERENCES 'usuario' ('idUsuario') ON UPDATE NO ACTION ON DELETE NO ACTION
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
I have some questions about these options.
Questions
- What is the purpose of each of these options?
- What do these options influence in my database?