How to delete a table that has foreign keys in MYSQL Workbench?

Asked

Viewed 152 times

1

I tried to use the DROP CASCADE and ALTER TABLE <nome_tabela> DROP FOREIGN KEY <chave_estrangeira>, DROP KEY <chave_estrangeira> to delete a table with foreign keys in MYSQL, but it does not work.

CREATE TABLE forma_pagamento (
  cd_forma_pagamento INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  tipo_pagamento VARCHAR(30),
  CHECK (tipo_pagamento IN ('Pagamento á vista'))
);

-- Quero apagar a tebela pagamento, mas a chave da tabela forma_pagamento não permite
CREATE TABLE pagamento (
  cd_pagamento INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  cd_forma_pagamento INT,
  FOREIGN KEY (cd_forma_pagamento) REFERENCES forma_pagamento (cd_forma_pagamento)
);
  • 1

    What error is appearing? Because you should first delete the foreign keys and then delete the table alter table pagamento drop foreign key cd_forma_pagamento

  • Error Code: 1091. Can't DROP FOREIGN KEY `cd_forma_pagamento`; check that it exists&#xA;

  • Are you saying that this foreign key does not exist, which error gives when you try to delete the table?

  • Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails

  • 1

    This question may contain the answer you are looking for https://answall.com/questions/67510/erro-ao-deletar-fk

  • This answers your question? Error while deleting FK

Show 1 more comment
No answers

Browser other questions tagged

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