Hi,
Great! This works, I did not use the 'null' and that is where I had a problem. One other questions: is this atomic? If I have several processes trying to do this at one time, will each one correctly update the table (assuming they have the same 'name' value.
Yes.
Also, in the part: unique (name(100)), what does the 100 do here? Is that saying the first 100 chars are considered unique?
Exactly.
Thanks, Cheers, Douglas
> 1 row in set (0.00 sec)-----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Monday, March 10, 2003 10:36 AM To: Douglas B Jones Cc: [EMAIL PROTECTED] Subject: Re: automatically incrementing an int value
As I read the manual, the REPLACE command will do what you want.
Make the name field UNIQUE, and the number field AUTO_INCREMENT NOT NULL. Replace dos a delete-if-present, insert. The insert generates a new ID.
See test below, and note two rows affected by second replace.
mysql> create table test (a int auto_increment not null, name tinytext not null, primary key (a), unique (name(100))) ; Query OK, 0 rows affected (0.02 sec)
mysql> replace into test values (null, "hello") ; Query OK, 1 row affected (0.01 sec)
mysql> select * from test ; +---+-------+ | a | name | +---+-------+ | 1 | hello | +---+-------+ 1 row in set (0.01 sec)
mysql> replace into test values (null, "hello") ; Query OK, 2 rows affected (0.02 sec)
mysql> select * from test ; +---+-------+ | a | name | +---+-------+ | 2 | hello | +---+-------+
--------------------------------------------------------------------- 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