MyUser
MyUser

Reputation: 321

Include row number as column in MySQL INSERT

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="[email protected]";

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

Thanks!

Upvotes: 0

Views: 1538

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521864

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="[email protected]"

Upvotes: 2

Related Questions