Unix Technical Forum

INSERT IF NOT EXISTS?

This is a discussion on INSERT IF NOT EXISTS? within the MySQL forums, part of the Database Server Software category; --> Hi all. I have a table which looks like this: CREATE TABLE table_1 ( column_1 INTEGER AUTO_INCREMENT PRIMARY KEY, ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:31 AM
Mikhail Kovalev
 
Posts: n/a
Default INSERT IF NOT EXISTS?

Hi all.

I have a table which looks like this:

CREATE TABLE table_1 (
column_1 INTEGER AUTO_INCREMENT PRIMARY KEY,
column_2 TEXT NOT NULL
);

INSERT INTO table_1 (column_2) VALUES ('hehe');

column_2 needs to be TEXT (not VARCHAR) and thus I cannot use PRIMARY
KEY or UNIQUE there. This is a problem because I do not want to have
duplicates in column_2.

ON DUPLICATE UPDATE KEY doesn't seem to work. Is there a workaround
here?

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:31 AM
Kees Nuyt
 
Posts: n/a
Default Re: INSERT IF NOT EXISTS?

On Tue, 25 Dec 2007 13:57:55 -0800 (PST), Mikhail Kovalev
<mikhail_kovalev@mail.ru> wrote:

>Hi all.
>
>I have a table which looks like this:
>
>CREATE TABLE table_1 (
> column_1 INTEGER AUTO_INCREMENT PRIMARY KEY,
> column_2 TEXT NOT NULL
>);
>
>INSERT INTO table_1 (column_2) VALUES ('hehe');
>
>column_2 needs to be TEXT (not VARCHAR) and thus I cannot use PRIMARY
>KEY or UNIQUE there. This is a problem because I do not want to have
>duplicates in column_2.
>
>ON DUPLICATE UPDATE KEY doesn't seem to work. Is there a workaround
>here?


Use a hash on your TEXT column as a key:

CREATE TABLE table_1 (
column_1 INTEGER AUTO_INCREMENT PRIMARY KEY,
column_2 TEXT NOT NULL,
key_2 CHAR(32) UNIQUE
);

INSERT INTO table_1 (column_2,key_2)
VALUES ('hehe',MD5('hehe'));

>Thanks.


HTH
--
( Kees
)
c[_] Preudhomme's Law of Window Cleaning: It's on the other side. (#512)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:31 AM
Mikhail Kovalev
 
Posts: n/a
Default Re: INSERT IF NOT EXISTS?

On 25 Des, 22:57, Mikhail Kovalev <mikhail_kova...@mail.ru> wrote:
> Hi all.
>
> I have a table which looks like this:
>
> CREATE TABLE table_1 (
> * * column_1 INTEGER AUTO_INCREMENT PRIMARY KEY,
> * * column_2 TEXT NOT NULL
> );
>
> INSERT INTO table_1 (column_2) VALUES ('hehe');
>
> column_2 needs to be TEXT (not VARCHAR) and thus I cannot use PRIMARY
> KEY or UNIQUE there. This is a problem because I do not want to have
> duplicates in column_2.
>
> ON DUPLICATE UPDATE KEY doesn't seem to work. Is there a workaround
> here?
>
> Thanks.


Btw, how is a duplicate defined in MySQL? Do all the columns have to
have the same value? Or just the primary key?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:31 AM
Peter H. Coffin
 
Posts: n/a
Default Re: INSERT IF NOT EXISTS?

On Tue, 25 Dec 2007 13:57:55 -0800 (PST), Mikhail Kovalev wrote:
> Hi all.
>
> I have a table which looks like this:
>
> CREATE TABLE table_1 (
> column_1 INTEGER AUTO_INCREMENT PRIMARY KEY,
> column_2 TEXT NOT NULL
> );
>
> INSERT INTO table_1 (column_2) VALUES ('hehe');
>
> column_2 needs to be TEXT (not VARCHAR) and thus I cannot use PRIMARY
> KEY or UNIQUE there. This is a problem because I do not want to have
> duplicates in column_2.
>
> ON DUPLICATE UPDATE KEY doesn't seem to work. Is there a workaround
> here?


Do you mean "ON DUPLICATE KEY UPDATE"? Because that does the opposite of
what it sounds like you want: If there's a duplicate key (column_1),
then change the value of column_2 to the incoming data instead of
throwing out a "duplicate key" error and skipping the row.

It sounds like you want duplicates in column_2 flagged and can't
because the whole column data may not be indexed and thus checked for
duplication. This is something that will have to be at least partly
handled at the application level, but you can do a large chunk of it via
MySQL. One thing (semi-automatic) would be to also store in column_3 a
hash of the value in column_2, perhaps using the MD5() function, and the
unique index built over column_3 instead of column_2. If rolled into a
Stored Procedure and that used instead of an insert, very little code
would remain outside of MySQL.



--
"Friendship is born at that moment when one person says to another, 'What!
You too? I thought I was the only one!'"
--C.S. Lewis
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 10:31 AM
Gordon Burditt
 
Posts: n/a
Default Re: INSERT IF NOT EXISTS?

>> I have a table which looks like this:
>>
>> CREATE TABLE table_1 (
>> * * column_1 INTEGER AUTO_INCREMENT PRIMARY KEY,
>> * * column_2 TEXT NOT NULL
>> );
>>
>> INSERT INTO table_1 (column_2) VALUES ('hehe');
>>
>> column_2 needs to be TEXT (not VARCHAR) and thus I cannot use PRIMARY
>> KEY or UNIQUE there. This is a problem because I do not want to have
>> duplicates in column_2.
>>
>> ON DUPLICATE UPDATE KEY doesn't seem to work. Is there a workaround
>> here?


It's "ON DUPLICATE KEY UPDATE". Note: DUPLICATE KEY.

>Btw, how is a duplicate defined in MySQL? Do all the columns have to
>have the same value? Or just the primary key?


If inserting the record causes a "duplicate key error", the update
happens instead. (If you just did a straight insert of the same
record, the insert would fail.) A record is a duplicate if all the
columns in any PRIMARY KEY or UNIQUE INDEX match that of an existing
record. You can have more than one such index, so a duplicate in
any one of the indexes counts as a duplicate.

Example: fields: A, B, C, D, and E
PRIMARY KEY: A
UNIQUE INDEX: B
UNIQUE INDEX: C,D

If the inserted record's A field matches an existing record, it's a duplicate.
If the inserted record's B field matches an existing record, it's a duplicate.
If the inserted record's C and D fields both match the same existing
record, it's a duplicate.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 10:31 AM
Mikhail Kovalev
 
Posts: n/a
Default Re: INSERT IF NOT EXISTS?

On 28 Des, 00:57, gordonb.sm...@burditt.org (Gordon Burditt) wrote:
> >> I have a table which looks like this:

>
> >> CREATE TABLE table_1 (
> >> * * column_1 INTEGER AUTO_INCREMENT PRIMARY KEY,
> >> * * column_2 TEXT NOT NULL
> >> );

>
> >> INSERT INTO table_1 (column_2) VALUES ('hehe');

>
> >> column_2 needs to be TEXT (not VARCHAR) and thus I cannot use PRIMARY
> >> KEY or UNIQUE there. This is a problem because I do not want to have
> >> duplicates in column_2.

>
> >> ON DUPLICATE UPDATE KEY doesn't seem to work. Is there a workaround
> >> here?

>
> It's "ON DUPLICATE KEY UPDATE". *Note: DUPLICATE KEY.
>
> >Btw, how is a duplicate defined in MySQL? Do all the columns have to
> >have the same value? Or just the primary key?

>
> If inserting the record causes a "duplicate key error", the update
> happens instead. *(If you just did a straight insert of the same
> record, the insert would fail.) *A record is a duplicate if all the
> columns in any PRIMARY KEY or UNIQUE INDEX match that of an existing
> record. *You can have more than one such index, so a duplicate in
> any one of the indexes counts as a duplicate.
>
> Example: *fields: A, B, C, D, and E
> PRIMARY KEY: A
> UNIQUE INDEX: B
> UNIQUE INDEX: C,D
>
> If the inserted record's A field matches an existing record, it's a duplicate.
> If the inserted record's B field matches an existing record, it's a duplicate.
> If the inserted record's C and D fields both match the same existing
> record, it's a duplicate.


Ok, thanks.

The only reason I use PRIMARY KEY in the first column is for automatic
increment, which doesn't seem to work without being PRIMARY. I could
use max(column_1) + 1 on insert and use md5 hash as key, but I don't
know how it perform speed wise, especially as the number of columns
grows...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 10:31 AM
Willem Bogaerts
 
Posts: n/a
Default Re: INSERT IF NOT EXISTS?

>>>> ON DUPLICATE UPDATE KEY doesn't seem to work. Is there a workaround
>>>> here?

>> It's "ON DUPLICATE KEY UPDATE". Note: DUPLICATE KEY.



Looking at the subject: if there is nothing to update, but you really
want to insert only if something does not exist, you can use the INSERT
IGNORE syntax also.

Regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:38 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com