We have a postgres table with a foreign key to another table. We need a system, that if the foreign key value is set to NULL, the row it referenced is deleted.

Can it be done using table constraints or do i need to create a trigger for that?

Example:
TABLE P
id: Primary key
child\_id: FOregin key(c.id)

Table C
id: PRimary KEy

If P row is deleted, referenced C row must be deleted. If Row in P is updated and the referece is set to NULL the row that was referencedin C must be deleted. If reference in P is set to reference another object. previous reference in C must be deleted.