By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,779 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,779 IT Pros & Developers. It's quick & easy.

insert duplicated unique/PK with no errors

P: n/a

....what is the best way to force duplicated unique
or primary key'ed row inserts not to raise errors?

duplicated rows can be ignored or updated as well
- it really doesn't matter.

to be specific, i've got:

$ psql -d mydb -c "copy mytable from 'mytable.tsv'

and, with duplicated rows there is an error:

ERROR: copy: line 1, Cannot insert a duplicate key into unique index my_unique

---

ps. i've tried:

CREATE TRIGGER my_trig
BEFORE INSERT OR UPDATE ON mytable FOR EACH
ROW EXECUTE PROCEDURE
check_primary_key('uid', 'mytable', 'uid');

but this function is missing:

ERROR: CreateTrigger: function check_primary_key() does not exist

---

....should i just code my own function?
my guess is that it will be slow
i feel there's better way...

(pgsql v7.x)

/Mirek Rusin
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies

P: n/a
Mirek Rusin wrote:
...what is the best way to force duplicated unique
or primary key'ed row inserts not to raise errors?


Doesn't make sense, am I missing the point ?

Regards
Gaetano Mendola
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2

P: n/a
>>...what is the best way to force duplicated unique
or primary key'ed row inserts not to raise errors?

Remove the columns' UNIQUE or PRIMARY KEY constraints.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #3

P: n/a
On Thu, 25 Sep 2003, Gaetano Mendola wrote:
Mirek Rusin wrote:
...what is the best way to force duplicated unique
or primary key'ed row inserts not to raise errors?


Doesn't make sense, am I missing the point ?


I think what he may want is:

create table test (id serial primary key, info text unique);
begin;
insert into test values (DEFAULT,'abc');
INSERT 1
insert into test values (22,'def');
INSERT 1
insert into test values (22,'def');
INSERT 0
commit;

In order to do this, you'll need triggers that fire on the unique / pk
before insertion, and if the values would be dups, to simply drop the
insert silently.

Can that be done, by the way?
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.