Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

incrementing a column without having to query the db first

Status
Not open for further replies.

trandrus

Programmer
Jan 17, 2021
1
US
I'm fairly new to mysql. I have a table with 1 unique/primary column and another called user_id that is unique but doesn't auto increment. When I add someone to the database, I don't know how to increment user_id without querying the db and find the last value first.
 
Set up that field/column as auto increment in the table definition. When you insert a record, just poke a zero in the field/column and the correct auto-incremented value will appear.
 
I agree with spamjim, but also wonder whether your primary key already is an auto_increment column and you try to have two auto_increment columns - MySQL doesn't support that.

Chriss
 
Oops...Chris is correct to notice that. I skimmed the question too quickly and misread. What's the point of having two unique columns? You've already got your unique user_id in the primary column. Get rid of the second user_id column.
 
The primary key might be something else, I just thought I also cover that case. But I also can't imagine a case where you want or need that.


Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top