Hello I want to create a before insert that looks if the value is on another table before inserting [closed]

dba.stackexchange https://dba.stackexchange.com/questions/283885

  •  14-03-2021
  •  | 
  •  

Question

I have a database for hospitals and I have a table for patients and one table for appointments. I need to create a trigger that if someone wants to add a person on the appointments table that person must be on the patient table before inserting on the appointments table. Thank you

Était-ce utile?

La solution

This is precisely what foreign key constraints were invented for. You'd want to create a foreign key on the appointments table's field that references the primary key field of the patient table. This will enforce that the person is a patient before there can be an appointment created for them.

Note that a foreign key constraint will likely be more performant than a trigger in this scenario as well. For more information and examples on foreign key constraints, please read this.

Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange
scroll top