0

Is there any way to insert the row number of a row as it is inserted in MySQL? Something like:

INSERT INTO users SET uid=ROWNUMBER(), email="joe@example.com";

If not, is there any other way that you would suggest to create a uid based on the entry number?

Thanks!

MyUser
  • 311
  • 2
  • 11

1 Answers1

2

MySQL (along with most RDBMS) does not have any internal row number or order. However, you can create an uid column which will auto increment every time a new record is inserted.

ALTER TABLE users ADD uid INT NOT NULL AUTO_INCREMENT PRIMARY KEY

Now when you do an INSERT, MySQL will automatically increment the uid column.

INSERT INTO users SET email="joe@example.com"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks! Originally when I first came across this problem, that is the exact solution I was looking for. I'm glad you found it even though I'd forgotten about it. – MyUser Nov 20 '15 at 04:28