How to insert ON DELETE and ON UPDATE

Asked

Viewed 435 times

1

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

1 answer

1


First you would need Dropar your Foreign Key.

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

Then you need to change the Tabela adding your key FK with the 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.