I have a table called usuario
. The code you use to create it is the following:
CREATE TABLE IF NOT EXISTS 'cumplimiento'.'usuario' (
'idusuario' VARCHAR(10) NOT NULL,
'pass' VARCHAR(100) NOT NULL,
'rol' INT NOT NULL,
'activo' TINYINT(1) NOT NULL,
'pregunta' VARCHAR(55) NULL,
'respuesta' VARCHAR(200) NULL,
PRIMARY KEY ('idusuario'),
INDEX 'fk_usuario_rol1_idx' ('rol' ASC),
CONSTRAINT 'fk_usuario_rol1'
FOREIGN KEY ('rol')
REFERENCES 'cumplimiento'.'rol' ('rol')
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
The table is created correctly and everything is fine. The situation is the following. I need to make UPDATE
to idusuario
and that change be reflected in the tables where this field is a foreign key.
I found that the way to do that is to use the instruction:
ON UPDATE CASCADE
For example, I have this other person table, whose code is as follows:
CREATE TABLE IF NOT EXISTS 'cumplimiento'.'persona' (
'cif' INT NOT NULL,
'idusuario' VARCHAR(10) NOT NULL,
'numero_impresiones' INT NULL,
INDEX 'fk_persona_usuario1_idx' ('idusuario' ASC),
PRIMARY KEY ('cif'),
CONSTRAINT 'fk_persona_usuario1'
FOREIGN KEY ('idusuario')
REFERENCES 'cumplimiento'.'usuario' ('idusuario')
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
And I understand that under the REFERENCES
is my field idusuario
. I also understand that it is in this table that I have to change the ON UPDATE NO ACTION
to ON UPDATE CASCADE
, and so on in other tables where I have this field as a foreign key.
Now, basically I have not done it because I have a little fear that something could happen to my tables and I intuit that there is an easy way to do it. How do I change that? Do I have to use the same script and only change that part of each table? or maybe, can it be done in some way in Workbench?