I have two entities proposition
and department
. A proposition has to target at least one department (1-N) and a department can be targeted by 0-N propositions.
So in my database, I have 3 tables : PROPOSITION
, TARGET
and DEPARTEMENT
.
I try to find a way to make sure that after inserting a proposition in PROPOSITION
, an entry is inserted in TARGET
. In other word I want to make sure the (1-N) relationship does not become a (0-N) relationship.
Is there a way to do that with a trigger ? Or should I change the structure of my database?
Thanks in advance
[EDIT] Solution :
A trigger can't do the job as the insertions in TARGET
depends on a value that is entered asynchronously by the user. For example, if the proposition 1 targets department 2 and 3, in my tables the data are :
PROPOSITION
1
TARGET
1 2
1 3
DEPARTMENT
1
2
3
Trigger idea : Set a trigger on PROPOSITION
, after something is inserted .... ?? What do we do ? Wait for an insertion in TARGET
? Insert a default value in TARGET
for the department ? So a trigger on PROPOSITION
is not going to work.
The only solution I found is to restructure my database :
DEPARTMENT_NUMBER
in proposition that can't be NULL. TARGET
table to check before insertion that the proposition is not already targeting the department in its attribute. The method slows the database but it ensures the (1-N) relationship to be respected
The best way of doing this in my opinon would be to create a trigger on Proposition that inserts into Target.
Check this link it should get you started. http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.