Weston Reed Weston Reed - 1 month ago 12
SQL Question

SQL Insert statement with auto-incrementing unique id

I have a table that stores a few values, one being an INT id, which is unique, and should increment by one for every new query, but I can't figure out how to process the query without providing a value.

Here's the SQL Query I'm using:

INSERT INTO db VALUES (0, $username, $score);


0 is what I have put in place of having it increment automatically. What should I put instead?

Answer

The simplest way is just to leave it out:

INSERT INTO db(username, score) -- or whatever the columns are
    VALUES ($username, $score);

When using INSERT, you should always list the columns. This is good for maintainability, readability, and debugging.