* Jianping Zhu
> I need to insert recond to mysql use servlets,
>
> insert into mytable values('key', 'f1');
>
> i need to chech if key is already in mytable.

why?

> what is a good efficient way to do that?

The most obvious answer, do a lookup to check if the record exist:

SELECT * FROM mytable WHERE key='key';

If you get no result, the record does not exist. However, there is a problem
with this approach in a multiuser environment: what if a different user
inserts this key a fraction of a second after you have found out it does not
exist...? You would get an duplicate key error, and your program must be
able to handle this. That is why it is normally better to just do the insert
in the first place, and then check if the insert resulted in an error, which
would indicate that the record already existed.

--
Roger


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to