问题描述
我在potgres数据库中有以下两种类型的表。
userid | bigint (PK) NOT NULL
username | character varying(255)
businessname | character varying(255)
收件箱
messageid | bigint (PK) NOT NULL
username | character varying(255)
businessname | character varying(255)
我要在这里实现的是我想向userRefId
表添加一个名为inBox
的新字段,并将user
表的userid
数据上的数据迁移到每个{ {1}}和username
在两个表中都匹配。
这些是我用来执行此操作的查询。
businessname
现在,我想验证数据是否已正确迁移。我可以采取哪些方法来实现这一目标? (注意:ALTER TABLE inBox ADD userRefId bigint;
UPDATE inBox
SET userRefId = u.userid
from "user" u
WHERE u.username = inBox.username
AND u.businessname = inBox.businessname;
上的username
可以为空)
这足以验证吗?
inBox
等于
select count(*) from inBox where username is not null;
解决方法
数据传输正确吗?首先,更新看起来正确,因此您不必担心。
您可以在consumer_inbox
中获得用户名不匹配的所有行
select ci.*. -- or count(*)
from consumer_inbox ci
where not exists (select 1
from user u
where ci.userRefId = u.userId
);
这并不意味着update
不起作用。只是consumer_inbox
中的值不匹配。
在您的代码的情况下,这等效于:
select ci.*
from consumer_inbox ci
where userId is null;
尽管这不会获得设置为不匹配记录(宇宙射线,有人吗?)的userId
。
您还可以验证用于匹配的其他字段:
select ci.*. -- or count(*)
from consumer_inbox ci
where not exists (select 1
from user u
where ci.userRefId = u.userId and
ci.username = u.username and
ci.businessname = u.businessname
);
但是,除非您在表或已知的不匹配记录上触发,否则所有这些检查似乎都是不必要的。