【发布时间】:2013-07-19 04:06:24
【问题描述】:
我使用以下查询来创建我的表。
create table t1 (url varchar(250) unique);
然后我插入大约 500 个网址,两次。我希望第二次获得的 URL 没有新条目出现在我的表中,而是我的计数值加倍:
select count(*) from t1;
我想要的是,当我尝试添加一个已经在我的表中的 url 时,它会被跳过。 我是否在我的表减速中声明了不正确的内容?
我正在使用 AWS 的 RedShift。
样本
urlenrich=# insert into seed(url, source) select 'http://www.google.com', '1';
INSERT 0 1
urlenrich=# select * from seed;
url | wascrawled | source | date_crawled
-----------------------+------------+--------+--------------
http://www.google.com | 0 | 1 |
(1 row)
urlenrich=# insert into seed(url, source) select 'http://www.google.com', '1';
INSERT 0 1
urlenrich=# select * from seed;
url | wascrawled | source | date_crawled
-----------------------+------------+--------+--------------
http://www.google.com | 0 | 1 |
http://www.google.com | 0 | 1 |
(2 rows)
\d 种子的输出
urlenrich=# \d 种子
Table "public.seed"
Column | Type | Modifiers
--------------+-----------------------------+-----------
url | character varying(250) |
wascrawled | integer | default 0
source | integer | not null
date_crawled | timestamp without time zone |
Indexes:
"seed_url_key" UNIQUE, btree (url)
【问题讨论】:
-
我对无法复制这种行为并不感到惊讶。什么版本的 PostgreSQL?
select count(*) from t1;返回什么?你用的是什么接口? (pgAdminIII、psql 等)有多少行是 NULL? (您可以在声明为 UNIQUE 的列中插入多个 NULL 值。将其声明为 PRIMARY KEY 可能是一个更好的主意。) -
一些证明唯一性违规的示例数据怎么样?甚至可能是 sqlfiddle.com 演示。
-
如果你
insert into seed(url,source) values('http://www.google.com',1);两次,它会做同样的事情吗? -
\d seed的输出是什么? -
@MikeSherrill'Catcall' 尝试了 PRIMARY KEY,仍然没有
标签: amazon-web-services amazon-redshift