Asume I have schema like this:
CREATE TABLE "user"
(
id BIGSERIAL PRIMARY KEY,
sid VARCHAR(32) NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
login VARCHAR(32) NOT NULL,
);
CREATE TABLE user_authority_relation
(
id BIGSERIAL PRIMARY KEY,
user_id int8 NOT NULL,
authority_id int8 NOT NULL,
CONSTRAINT user_fk FOREIGN KEY (user_id)
REFERENCES "user" (id) ON DELETE CASCADE,
CONSTRAINT authority_fk FOREIGN KEY (authority_id)
REFERENCES "authority" (id) ON DELETE CASCADE,
UNIQUE (user_id, authority_id)
);
CREATE TABLE note (
id BIGSERIAL PRIMARY KEY,
content text NOT NULL,
sender_id int8 NOT NULL,
sent_date timestamp without time zone NOT NULL,
important BOOLEAN NOT NULL,
type VARCHAR(16) NOT NULL,
CONSTRAINT user_fk FOREIGN KEY (sender_id)
REFERENCES "user" (id) ON DELETE CASCADE DEFERRABLE INITIALLY IMMEDIATE
);
Now, I must delete user X from db, but instead of delete all related objects I want replace id to my new user Y. So for example if some note.sender_id is X.id i want set it too Y.id, same for user_authority_relation.user_id I want change from X.id to Y.id.
Is it possible in sql/postgresql?
EDIT:
I have written too broadly. I know I can write update like:
update user_authority_relation
set user_id = y.id
but what I want achive is one update on all objects that refers to my X by constraint. I my db I have over a dozen tables that refer to user. I don't want to write update for all of them separately but one update.