请参阅本说明末尾的重要新发现1和2.
我正在运行Postgres 9.1.3并且有一个奇怪的左连接问题。
我有一个名为 consistent.master 的表格,行数超过200万。它有一个名为 citation_id 的列,该列没有空值。我可以用这个验证:
SELECT COUNT(*)
FROM consistent.master
WHERE citation_id IS NULL
返回 0 。
这里有点奇怪:如果我 LEFT JOIN 这个表到临时表,我得到一个错误,我试图在 citation_id 字段中插入一个null :
错误:“citation_id”列中的空值违反了非空约束 SQL状态:23502
以下是查询:
WITH stops AS (
SELECT citation_id,
rank() OVER (ORDER BY offense_timestamp,
defendant_dl,
offense_street_number,
offense_street_name) AS stop
FROM consistent.master
WHERE citing_jurisdiction=1
)
INSERT INTO consistent.masternew (arrest_id, citation_id, defendant_dl, defendant_dl_state, defendant_zip, defendant_race, defendant_sex, defendant_dob, vehicle_licenseplate, vehicle_licenseplate_state, vehicle_registration_expiration_date, vehicle_year, vehicle_make, vehicle_model, vehicle_color, offense_timestamp, offense_street_number, offense_street_name, offense_crossstreet_number, offense_crossstreet_name, offense_county, officer_id, offense_code, speed_alleged, speed_limit, work_zone, school_zone, offense_location, id, source, citing_jurisdiction, the_geom)
SELECT stops.stop, master.citation_id, defendant_dl, defendant_dl_state, defendant_zip, defendant_race, defendant_sex, defendant_dob, vehicle_licenseplate, vehicle_licenseplate_state, vehicle_registration_expiration_date, vehicle_year, vehicle_make, vehicle_model, vehicle_color, offense_timestamp, offense_street_number, offense_street_name, offense_crossstreet_number, offense_crossstreet_name, offense_county, officer_id, offense_code, speed_alleged, speed_limit, work_zone, school_zone, offense_location, id, source, citing_jurisdiction, the_geom
FROM consistent.master LEFT JOIN stops
ON stops.citation_id = master.citation_id
我在这个问题上摸不着头脑。如果这是 LEFT JOIN ,并且 consistent.master 是联接的左表,则此查询如何在 citation_id 列中创建空值什么时候开始没有?
这是我用来创建表的SQL代码:
CREATE TABLE consistent.masternew
(
arrest_id character varying(20),
citation_id character varying(20) NOT NULL,
defendant_dl character varying(20),
defendant_dl_state character varying(2),
defendant_zip character varying(9),
defendant_race character varying(10),
defendant_sex character(1),
defendant_dob date,
vehicle_licenseplate character varying(10),
vehicle_licenseplate_state character(2),
vehicle_registration_expiration_date date,
vehicle_year integer,
vehicle_make character varying(20),
vehicle_model character varying(20),
vehicle_color character varying,
offense_timestamp timestamp without time zone,
offense_street_number character varying(10),
offense_street_name character varying(30),
offense_crossstreet_number character varying(10),
offense_crossstreet_name character varying(30),
offense_county character varying(10),
officer_id character varying(20),
offense_code integer,
speed_alleged integer,
speed_limit integer,
work_zone bit(1),
school_zone bit(1),
offense_location point,
id serial NOT NULL,
source character varying(20), -- Where this citation came from--court, PD, etc.
citing_jurisdiction integer,
the_geom geometry,
CONSTRAINT masternew_pkey PRIMARY KEY (id ),
CONSTRAINT citing_jurisdiction FOREIGN KEY (citing_jurisdiction)
REFERENCES consistent.jurisdictions (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT offenses FOREIGN KEY (offense_code)
REFERENCES consistent.offenses (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2),
CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL),
CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 3081)
)
WITH (
OIDS=FALSE
);
ALTER TABLE consistent.masternew
OWNER TO postgres;
COMMENT ON COLUMN consistent.masternew.source IS 'Where this citation came from--court, PD, etc.';
CREATE INDEX masternew_citation_id_idx
ON consistent.masternew
USING btree
(citation_id COLLATE pg_catalog."default" );
CREATE INDEX masternew_citing_jurisdiction_idx
ON consistent.masternew
USING btree
(citing_jurisdiction );
CREATE INDEX masternew_defendant_dl_idx
ON consistent.masternew
USING btree
(defendant_dl COLLATE pg_catalog."default" );
CREATE INDEX masternew_id_idx
ON consistent.masternew
USING btree
(id );
CREATE INDEX masternew_offense_street_name_idx
ON consistent.masternew
USING btree
(offense_street_name COLLATE pg_catalog."default" );
CREATE INDEX masternew_offense_street_number_idx
ON consistent.masternew
USING btree
(offense_street_number COLLATE pg_catalog."default" );
CREATE INDEX masternew_offense_timestamp_idx
ON consistent.masternew
USING btree
(offense_timestamp );
CREATE INDEX masternew_the_geom_idx
ON consistent.masternew
USING gist
(the_geom );
重要发现1
我刚刚发现了一些有趣的东西。这个查询:
SELECT COUNT(*)
FROM consistent.master
WHERE citation_id IS NOT NULL
UNION
SELECT COUNT(*)
FROM consistent.master
UNION
SELECT COUNT(*)
FROM consistent.master
WHERE citation_id IS NULL
结果是:
2085344
2085343
0
我怎么能解释这个? WHERE citation_id IS NOT NULL
的计数如何可能高于没有WHERE
子句的同一查询?
重要发现2
好的,根据下面的评论,我发现我有一行包含所有空值,这是尽管这个表有一个串行id
列和一些{{1}约束。
我删除了屁股行。现在我没有得到null错误。相反,我得到了这个:
NOT NULL
所以只是为了确保,我做了这个查询:
ERROR: duplicate key value violates unique constraint "masternew_pkey"
DETAIL: Key (id)=(1583804) already exists.
********** Error **********
ERROR: duplicate key value violates unique constraint "masternew_pkey"
SQL state: 23505
Detail: Key (id)=(1583804) already exists.
猜猜是什么? SELECT COUNT(id)
FROM consistent.master
WHERE id=1583804;
只有1个这样的实例!因此,consistent.master
只有LEFT JOIN
1583804 <{1>} 中的左侧表格 列中的左侧表格 id 只能来自左表,这个错误怎么可能发生?这样的citation_id
不应该导致最终结果的行数多于左表,对吗?
答案 0 :(得分:3)
使用INSERT,尤其是复杂的INSERT,您应该始终定义目标列。所以说:
INSERT INTO consistent.masternew (citation_id, col1, col2, ...)
如果随附的SELECT语句出现任何问题 - 就像这样:
the_geom geometry
(使用类型名称重命名列是没有意义的 - 我认为这是无意的) - 或者如果基础表定义发生更改,则没有定义目标列的INSERT语句可能会出现严重错误。
PostgreSQL不会在SELECT语句中强制执行与目标表中相同数量的列。我引用the fine manual on that:
显式或隐式列列表中不存在的每个列都将 填充默认值,或者声明默认值或 如果没有,则为null 。
(大胆强调我的。)如果列列表中存在不匹配,则可能会使“无处不在”的NULL值出现。
此外,SELECT语句中列的顺序必须与要插入的列的顺序相匹配。如果目标列没有拼写出来,那么这将是表格中创建的列的顺序 您似乎希望列自动匹配名称,但事实并非如此。 SELECT语句中的列名与INSERT的最后一步完全无关。只有他们从左到右的顺序才是重要的。
与其他人所暗示的相反, WITH子句完全合法。我引用the manual on INSERT:
查询(SELECT语句)也可以包含WITH 条款。在这种情况下,可以引用两组with_query 在查询中,但第二个优先,因为它更多 紧密嵌套。
您的陈述可能如下所示:
WITH stops AS (
SELECT citation_id
,rank() OVER (ORDER BY
offense_timestamp
,defendant_dl
,offense_street_number
,offense_street_name) AS stop
FROM consistent.master
WHERE citing_jurisdiction = 1
)
INSERT INTO consistent.masternew (citation_id, col1, col2, ...) -- add columns
SELECT m.citation_id -- order colums accordingly!
,s.stop
,m.defendant_dl
-- 27 more columns
,m.citing_jurisdiction
,m.the_geom
FROM consistent.master m
LEFT JOIN stops s USING (citation_id);
答案 1 :(得分:2)
猜测我会说你在citation_id列中插入了stops.stop,它可能是null,但是在不知道表结构的情况下我无法确定:)
编辑:尝试@ vol7ron的建议并命名列......