Re: mySQL, Best Way to Increment Records



number1.email@xxxxxxxxx wrote:
I have a question regarding incrementing records in a mySQL Database
Table. For example, if the User Table has a UserID field as the
primary index, I could always Select the MAX(userid) field, increment
it, and add a new user this way.

Why trouble yourself with php-code which is handled by the database itself?

Primary key should be an autoincrement field (integer), for reference to new
primary key if further operations on the database are needed, use
mysql_insert_id($resource). Inserts can be done without setting the primary
key to an explicit value, this is handled by MySQL.

If you're looking for a function updating existing records look into the
"REPLACE" posssibility of MySQL.

Problem is, what if someone also
requests this same userid before I "insert" my record?

Why would thid happen? It seems to me this is not a database problem but a
code problem. If a request from a client requires a certain primary field,
it should be a result from earlier code, and exist. If a request from a user
asks for a non-existent ID, it would be an illegal request if you'd code
correctly, and as such an exception which wouldn't need catching, as it's a
client try for vulnerabilities, not a legel request.

This would
cause a problem as it is not unique... In Oracle you can "Select
<val> from dual", which is automatically updated...so no person gets
the same number. Does mySQL have something similar, like a counter
field? How do I go about this?

autoincrement your primary key field.
use REPLACE instead of INSERT.

Grtz,
--
Rik Wasmus


.



Relevant Pages

  • Re: Help for very bad perf for MySQL
    ... I've a bi-proc single core Xeon 3.2ghz with FreeBSD 6.2, ... running Mysql 5.X on this server and the performance of MySQL ... Heure local/Local time: ... At 6 sec for one request it's ...
    (freebsd-questions)
  • Re: XML DB
    ... way to request this? ... I'm looking for a free database if possible that can handle this. ... decent relational schema and do normal SQL instead. ... Database Workbench - tool for InterBase, Firebird, MySQL, NexusDB, Oracle & ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Array caching and optimising the array manipulation process
    ... I'm trying to get the best out of an application, powered by MySQL by ... PHP applications, but I believe since php 5 became popular I can do ... PHP doesn't have persistance at the application level. ... First, you'll have to pass those values from request to request, more ...
    (comp.lang.php)
  • Re: is there an equivavlent to auto_increment in ingres ?
    ... System A drops a request for SYSTEM B to do something into a table. ... It spawns information out ... only usefulness is to act as the primary key. ... spare with the apparent multiplicity of join conditions. ...
    (comp.databases.ingres)
  • Re: Executing PHP files on remote web server
    ... MySQL users for a full e-commerce applicaton. ... PHP on the web server without help. ... the master database to do updates, and this other request is coming ... particular tables: If you have been so dumb as to store unencrypted credit card data in the same table that you are passing out to all and sundry, I suppose you have a small point: I try not to be that stupid. ...
    (comp.lang.php)