PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > INSERT IF NOT EXISTS?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
INSERT IF NOT EXISTS?

Réponse
 
LinkBack Outils de la discussion
Vieux 25/12/2007, 22h57   #1
Mikhail Kovalev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 26/12/2007, 01h45   #2
Kees Nuyt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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)
  Réponse avec citation
Vieux 27/12/2007, 15h32   #3
Mikhail Kovalev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?
  Réponse avec citation
Vieux 27/12/2007, 17h45   #4
Peter H. Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 28/12/2007, 00h57   #5
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.


  Réponse avec citation
Vieux 28/12/2007, 10h10   #6
Mikhail Kovalev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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...
  Réponse avec citation
Vieux 28/12/2007, 10h48   #7
Willem Bogaerts
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17008 seconds with 15 queries