There are two tables, User(int id) Message(sender_id, adresee_id) .

In the message table, two foreign keys, one of which points to the sender, and the second to the recipient of the message.

How to configure the function, procedure and trigger, so that when a user is deleted, the message table foreign key associated with the user being deleted and is set to NULL ?

    1 answer 1

    This can be set in the description of the external prickly. Register ON DELETE SET NULL . For example:

     CONSTRAINT my_fkey FOREIGN KEY (fk_id) REFERENCES fk_table (id) ON DELETE SET NULL 
    • Thanks, but can this be implemented using functions? - Artem
    • You can implement it, but the point is to do what the DBMS itself can do for you? - Streletz
    • I am doing backend andes on hibernate and with it I could not achieve the desired result when I figured out that cascade at the database level and objects are different things, but on the instructions we still have a requirement to implement procedures and functions, and decided that this task can be perform using procedures. - Artem