英文:
Data succesfully inserted, but table remains empty
问题
我目前正在使用SQL,特别是pgAdmin4,与PostgreSQL一起工作。我有一个名为'acetami'的表,是使用以下查询创建的:
CREATE TABLE ACETAMI (
PRIMARYID BIGINT,
DRUG VARCHAR(3000),
Gender VARCHAR(3),
AGE FLOAT,
Adverse_event VARCHAR(100),
ROLE_COD VARCHAR(2)
);
它应该包含与其他表相关的数据,我使用以下查询插入了PRIMARYID、DRUG和Adverse_event:
INSERT INTO acetami (PRIMARYID, DRUG, Adverse_event)
SELECT PRIMARYID, DRUG, Adverse_Event
FROM drug_adverse_reactions_pairs
WHERE DRUG = 'acetaminophen' AND Adverse_Event = 'Nausea';
这个查询工作得很好。
但后来我尝试从与acetami匹配的主要ID的人口统计表中插入age和gender,以下是我使用的查询:
INSERT INTO acetami (gender, age)
SELECT demographics.gender, demographics.age
FROM demographics
WHERE demographics.primaryid IN (SELECT primaryid FROM acetami);
它返回:
INSERT 0 41493
查询在2秒251毫秒内成功返回。
我不太确定这是什么意思,但当我检查acetami表时,所有的age和gender仍然是NULL。
以下是我创建人口统计表的方式:
DROP TABLE IF EXISTS DEMOGRAPHICS;
CREATE TABLE DEMOGRAPHICS (
caseid BIGINT,
primaryid BIGINT,
caseversion INT,
fda_dt INT,
I_F_COD VARCHAR(1),
event_dt INT,
AGE FLOAT,
Gender VARCHAR(3),
COUNTRY_CODE VARCHAR(2),
Period VARCHAR(50)
);
我认为问题可能是由于所有primaryid在人口统计表中没有数据,但当我检查acetami中的特定primaryid时,它确实有gender和age。
我不确定我在这里缺少什么,就查询而言,据我所知是正确的。或者也许我错过了我不知道的东西。有人知道我可能在这里做错了什么吗?
英文:
Im currently working with SQL, and im currently using postgreSQL, sepcifically pgAdmin4. i have this table i called 'acetami' created with the following query:
CREATE TABLE ACETAMI (
PRIMARYID BIGINT,
DRUG VARCHAR(3000),
Gender VARCHAR(3),
AGE FLOAT,
Adverse_event VARCHAR(100),
ROLE_COD VARCHAR(2)
);
it supposed to contain data with other table, i inserted primaryid, drug, and adverse_event with the following query:
INSERT INTO acetami (PRIMARYID, DRUG, Adverse_event)
SELECT PRIMARYID, DRUG, Adverse_Event
FROM drug_adverse_reactions_pairs
WHERE DRUG = 'acetaminophen' AND Adverse_Event = 'Nausea';
it works just fine.
But then i tried to insert age and gender from demographics table where the primary id matches with acetami, here is the query i used:
INSERT INTO acetami (gender, age)
SELECT demographics.gender, demographics.age
FROM demographics
WHERE demographics.primaryid IN (SELECT primaryid FROM acetami);
it returns:
INSERT 0 41493
Query returned successfully in 2 secs 251 msec.
im not really sure what i means but when i checked the acetami table, all age and gender is still NULL
here is how i create the demographics table:
DROP TABLE IF EXISTS DEMOGRAPHICS;
CREATE TABLE DEMOGRAPHICS (
caseid BIGINT,
primaryid BIGINT,
caseversion INT,
fda_dt INT,
I_F_COD VARCHAR(1),
event_dt INT,
AGE FLOAT,
Gender VARCHAR(3),
COUNTRY_CODE VARCHAR(2),
Period VARCHAR(50)
);
I thought the problem is due to all the primaryid not having data in the demographics table, but when i checked with specific primaryid in acetami, it does have gender and age.
im not sure what im missing here, query wise it's correct as far as i know. or maybe im missing something im not aware of. Does anyone know what i might be doing wrong here?
答案1
得分: 0
如果您想要更新现有primaryid的年龄和性别信息,您需要使用UPDATE语句,而不是INSERT语句。INSERT语句用于插入新的行。
您已经使用INSERT语句从drug_adverse_reactions_pairs表中插入了行。在第二步中,您试图更新年龄和性别的列值。
请尝试以下UPDATE语句:
UPDATE acetami SET Gender = b.gender, Age = b.age
FROM acetami a INNER JOIN DEMOGRAPHICS b ON a.PRIMARYID = b.primaryid
英文:
If you want to Update the Age & Gender of the existing primaryid then you need to use UPDATE statement, not INSERT statement. Insert statement is used to insert new rows.
You already Inserted rows using INSERT statement from drug_adverse_reactions_pairs table. In the second step you are trying the update the column value of Age & Gender.
Try UPDATE statements as below
UPDATE acetami SET Gender = b.gender, Age = b.age
FROM acetami a INNER JOIN DEMOGRAPHICS b ON a.PRIMARYID = b.primaryid
答案2
得分: 0
在从demographics
插入时,acetami
列primaryid
没有赋值,并且该列没有默认值,因此插入的任何行都将具有NULL
的primaryid
。看起来您打算使用demographics
中的值更新acetami
中的现有行。可以通过以下方式实现:
UPDATE acetami
SET gender = demographics.gender,
age = demographics.age
FROM demographics
WHERE acetami.primaryid = demographics.primaryid;
acetami
和demographics
都没有主键约束/索引,这可能会提高查询执行速度。
英文:
There's no value assigned to column primaryid
in acetami
when inserting from demographics
, and there's no default for that column, so any rows inserted would have NULL
for primaryid
. It appears that you indended to update existing rows in acetami
with values from demographics
. This can be achieved with the following:
UPDATE acetami
SET gender = demographics.gender,
age = demographics.age
FROM demographics
WHERE acetami.primaryid = demographics.primaryid;
Neither acetami
nor demographics
have primary key constraints/indexes, which would improve the query execution times.
答案3
得分: 0
如前所指出,这两个都是插入新记录集的 insert
操作。第二个不会更新之前放入表中的记录。您可以将它们合并为一个单独的 insert
操作:
insert into acetami
( primaryid, drug, gender, age, adverse_event)
select ar.primaryid, ar.drug, d.gender, d.age, ar.adverse_event
from drug_adverse_reactions_pairs as ar,
demographics as d
where ar.primaryid = d.primaryid --隐式连接
and ar.drug = 'acetaminophen'
and ar.adverse_event = 'Nausea';
这里有关于 表连接 (join
) 的文档以及为什么 显式表连接 (explicit join
) 有助于的信息:
insert into acetami
( primaryid, drug, gender, age, adverse_event)
select ar.primaryid, ar.drug, d.gender, d.age, ar.adverse_event
from drug_adverse_reactions_pairs as ar
join demographics as d on ar.primaryid = d.primaryid --显式连接
where ar.drug = 'acetaminophen'
and ar.adverse_event = 'Nausea';
PostgreSQL 15 还提供了 merge
操作,它可以实现您期望的功能:将 insert
与 update
结合在一起。如果在目标表中找不到匹配的记录,将作为新记录插入。如果找到匹配的记录,将使用该记录来更新它。
如果您的 primaryid
是一个实际的 primary key
,或者至少是一个 unique
值,您可以使用 insert...on conflict do update
结构来实现类似的效果。
英文:
As already pointed out, these are both insert
s of new sets of records. The second one won't update
the ones previously placed in the table. You can reduce them to a single insert
:
insert into acetami
( primaryid, drug, gender, age, adverse_event)
select ar.primaryid, ar.drug, d.gender, d.age, ar.adverse_event
from drug_adverse_reactions_pairs as ar,
demographics as d
where ar.primaryid = d.primaryid --implicit join
and ar.drug = 'acetaminophen'
and ar.adverse_event = 'Nausea';
Here's documentation on join
ing tables and why explicit join
s can help:
insert into acetami
( primaryid, drug, gender, age, adverse_event)
select ar.primaryid, ar.drug, d.gender, d.age, ar.adverse_event
from drug_adverse_reactions_pairs as ar
join demographics as d on ar.primaryid = d.primaryid --explicit join
where ar.drug = 'acetaminophen'
and ar.adverse_event = 'Nausea';
PostgreSQL 15 also offers merge
, which does what you expected: it combines an insert
with an update
. If no matching record is found in the target table, the payload is inserted as new. If a matching record is found, the payload is used to update it.
If your primaryid
was an actual primary key
, or at least a unique
value, you could use an insert...on conflict do update
construct to get a similar effect.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论