我有2个表:user_account
和email_validation
。
user_account
有user_id
,email
和email_validation_id
。
email_validation
有email_validation_id
,user_account_id
和email
。
以正常和正确的方式,对于user_account
中的每个用户(例如[user_id="123", email="user@domain.com", email_validation_id="456"]
),email_validation
中都有一行(例如[email_validation_id="456", user_account_id="123", email="user@domain.com"]
)。
但是,有些事情出错了,现在我的用户没有电子邮件验证。所以,我想手动添加行:user_account
中的每个用户email_validation
中没有行(即没有行user_id
)插入[email_validation_id=<The current max of (email_validation.email_validation_id) + 1>, user_account_id=<user_account.user_id>, email=<user_account.email>]
并将user_account.email_validation_id
更新为<email_validation.email_validation_id>
。
我不知道如何解决这个问题:\
感谢您的时间!
答案 0 :(得分:0)
如果我理解了一切,这可能会有所帮助。
要创建/插入缺少的电子邮件验证,您可以这样做。
INSERT INTO email_validation(user_account_id, email)
SELECT user_id, email
FROM user_account WHERE email_validation_id is null;
要自动增加您可以执行的ID。
表格定义:
CREATE TABLE tablename
(
id NUMBER(10)
);
ALTER TABLE tablename ADD (CONSTRAINT tbn_pk PRIMARY KEY (id));
CREATE SEQUENCE tbn_seq START WITH 1;
或者您可以使用{12} +中的Identity
列。
CREATE TABLE tablename
(
id NUMBER GENERATED ALWAYS AS IDENTITY
);
触发定义:
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;
/
要更新user_account.email_validation_id,您可以这样做。
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;
/
我希望我能帮到你。