5
I have the tables SIMULATED and QUESTAO, where a simulated can have several questions, but a question can only be for a simulated (relationship 1-n).
MOCK TABLE:
simuladoId
simuladoNome
TABLE QUESTION
questaoId
questaoPergunta
questaoIdSimulado
I would like to know how to prevent these relational data from being erased. That is, when I delete a simulated one that has its foreign key propagated for one or more question, is issued an error, preventing.
From the little I researched I understood that, at least in MYSQL, it is possible through Triggers.. But I understand little of the subject, so I would like some other option, or even some examples of Trigger.
I tried the following:
alter table ang_questao add constraint FK_ang_questao foreign key (questao_id_simulado) references ang_simulado (simulado_id) ON DELETE RESTRICT
I ran and no error was displayed in SQL.
When I tried to erase it, it was shown to me:
Error while Deleting: SQLSTATE[23000]: Integrity Constraint Violation: 1451 Cannot delete or update a Parent Row: a Foreign key Constraint fails (
simulado_anglo
.ang_questao
, CONSTRAINTFK_ang_questao
FOREIGN KEY (questao_simulado_id
) REFERENCESang_simulado
(simulado_id
))Line Error: #10 :: Cannot Modify header information - headers already sent by (output Started at C: xampp htdocs simulado-geral config.php:33) C: xampp htdocs simulado-geral admin php deletar.php
Thiago, do you want to REALLY delete, or prevent it from being deleted? In the case of foreign keys in other tables, as in the case you have a Simulated 10 questions, but when you delete from this error, the way is you through Trigger remove the foreign key from the issues where it was his ID put NULL or direct to another simulated, or a Trigger deleting the questions first and then deleting the questionnaire OR when creating the table places the ON DELETE CASCADE in Foreign key. That then when deleting the simulated questions are deleted.
– Gustavo Tinoco
I want to know what it would look like if I wanted both options: When to delete a simulated, cascade out the related questions, or else not allow the simulated one to be erased.
– Cobra
You can give a SHOW TRIGGERS on the console. Expensive depending on the use of the simulated instead of you delete REALLY from the bank you can create a column in the simulated give the active and inactive update, is a tip. But there in the case to set the on delete Check out here http://stackoverflow.com/a/18616544/6405917 that’s it.
– Gustavo Tinoco
A status column would actually be much more viable, because if you think about it soon, it would be possible to reuse it.. Thanks for the tip! And regarding Trigger, I managed to give a read on the link. Thanks.
– Cobra
Yes if you think for this side is interesting to reuse instead of delete even the data. It’s like the facebook account. There is the ACCOUNT table and it is not allowed to delete the account in its entirety, only disabling, if you want the same delete has to send email and such and there is a whole bureaucracy because of the data. Serves information also worth.
– Gustavo Tinoco
@Gustavotinoco, I left with RESTRIC, and now it looks like this: When I TRY to delete a question the same error appears. The idea of logic should be as follows: When deleting a simulated one that contains questions, make a mistake. However, when deleting a question, that’s fine.. Because a question depends on a simulated one, but the same does not depend on a question.
– Cobra
Just like @Gustavotinoco, I encourage the creation of a statusative or inactive column, so it is better to manage, can reuse questions and if you are dealing with programming in a general and not just database, has how in your application to make a page to manage inactive questions, to be able to edit them and whatever you need.
– Diéfani Favareto Piovezan