I have 2 tables: user_account
and email_validation
. user_account
has user_id
, email
and email_validation_id
. email_validation
has email_validation_id
, user_account_id
, and email
.
In normal and correct way, for each user in user_account
(for example [user_id="123", email="user@domain.com", email_validation_id="456"]
) there is a row in email_validation
(for example [email_validation_id="456", user_account_id="123", email="user@domain.com"]
).
But, some things got wrong and now I have users with no email validation. So, I would like to add rows manually: for each user from user_account
which don't have a row in email_validation
(ie no row with it's user_id
) insert [email_validation_id=<The current max of (email_validation.email_validation_id) + 1>, user_account_id=<user_account.user_id>, email=<user_account.email>]
and update the user_account.email_validation_id
to <email_validation.email_validation_id>
.
I have no idea how to approach this :\\
Thank you for your time!
If I had understood everything correctly this could help.
To create/insert missing email validation you can do.
INSERT INTO email_validation(user_account_id, email)
SELECT user_id, email
FROM user_account WHERE email_validation_id is null;
To auto increase the id you can do.
Table definition:
CREATE TABLE tablename
(
id NUMBER(10)
);
ALTER TABLE tablename ADD (CONSTRAINT tbn_pk PRIMARY KEY (id));
CREATE SEQUENCE tbn_seq START WITH 1;
Or you can use Identity
Columns is available from Oracle 12c+.
CREATE TABLE tablename
(
id NUMBER GENERATED ALWAYS AS IDENTITY
);
Trigger definition:
CREATE SEQUENCE tbn_seq START WITH 1;
CREATE OR REPLACE TRIGGER tbn_id_insert BEFORE INSERT ON departments
FOR EACH ROW
BEGIN
SELECT tbn_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
/
To update the user_account.email_validation_id you can do.
DECLARE
v_email_validation_id user_account.email_validation_id%TYPE;
BEGIN
FOR user_account_rec IN (SELECT * FROM user_account WHERE email_validation_id is NULL)
LOOP
SELECT email_validation_id
INTO v_email_validation_id
FROM email_validation
WHERE user_account_id = user_account_rec.user_id;
UPDATE user_account
SET email_validation_id = v_email_validation_id
where user_id = user_account_rec.user_id;
END LOOP;
END;
/
I hope I was able to help you.
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.