How to insert ON DELETE and ON UPDATE

1

I have a MySQL database structure ready, but ALTER TABLE of FOREIGN KEY's has not specified settings for ON DELETE and ON UPDATE . How do I enter these settings now, with the tables and keys already created?

    
asked by anonymous 31.03.2018 / 14:08

1 answer

1

First of all you would need to Dropar its Foreign Key .

ALTER TABLE [Nome da sua Tabela] DROP FOREIGN KEY [Nome da sua Foreign Key];

Then you need to change the Tabela by adding your key FK to ON DELETE CASCADE

ALTER TABLE TABELA_1 
  ADD CONSTRAINT FK_NOME_SUA_FK 
  FOREIGN KEY (NOME_DA_COLUNA_QUE_SERA_FK_NA_TABELA_1) 
  REFERENCES NOME_DA_TABELA_2(NOME_DA_COLUNA_ESTRANGEIRA_DA_TABELA_2) 
  ON DELETE CASCADE;
    
31.03.2018 / 14:36