As I said, the database itself (in the example, Mysql) makes this deletion impossible. Follow an example print:
The error that is displayed:
16:39:29 DELETE FROM tbprofissao WHERE id = 3 Error Code: 1451. Cannot
delete or update a Parent Row: a Foreign key Constraint fails
(wrock
.tbfuncionario
, CONSTRAINT tbfuncionario_ibfk_1
FOREIGN
KEY (profissao
) REFERENCES tbprofissao
(id
)) 0.328 sec
Edit (After Comment):
So do the following (3 is the desired id):
DELETE FROM tbprofissao WHERE tbprofissao.id NOT EXISTS ( SELECT tbfuncionario.profissao FROM tbfuncionario WHERE tbfuncionario.profissao = 3);
What this SQL does ?
A: It will delete the row from the table that does not exist in the working table
The correct is in the bank itself you do this treatment if the table depends on others. But what you can do is a
select
of allid
table, with awhere
ofid
you want to know if it’s being used, which is not a good practice, but each case is a case.– rbz
MYSQL itself already takes care of it.
– Felipe Junges
The idea would be to bring a code and explain to us your question/difficulty to help you. Which is not the case requested. Either way, a foreign key (when created) restricts the deletion of the record if it has a reference in another table.
– Gabriel Heming
https://answall.com/questions/152775/howto avoidr-a-excludes%C3%A3o-de-relational-data-atrav%C3%A9s-of-integrity-referential-n
– rbz