I have the SIMULATE and QUESTION tables, where a simulation can have several questions, but one question can only be for a simulated (1-n relationship).
SIMULATED TABLE:
simuladoId
simuladoNome
QUESTION TABLE
questaoId
questaoPergunta
questaoIdSimulado
I'd like to know how to prevent these relational data from being deleted. That is, when I delete a dummy that has its foreign key propagated to one or more question, an error is issued, preventing.
For the little bit that I researched I understood that, at least in MYSQL, it is possible through Triggers .. But I do not understand much of the subject, so I would like some other option, or even some trigger examples.
I've 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 there was no error in SQL.
When I tried to delete it, I was shown:
Deletion Error: SQLSTATE [23000]: Integrity constraint violation: 1451 Can not delete or update parent row: a foreign key constraint fails (% with%.% of%, CONSTRAINT% with% FOREIGN KEY (
simulado_anglo
) REFERENCESang_questao
(FK_ang_questao
))Line Error: # 10 :: Can not modify header information - headers already sent by (output started at C: \ xampp \ htdocs \ simulate-general \ config.php: 33) C: \ xampp \ htdocs \ dummy \ admin \ php \ deletar.php