Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autoincrement last digits in SQL?

Tags:

sql

mysql

I am making a website and giving each user a unique id after registration. The unique id must start from 80001.But Now I want second user to have id 80002,third user 80003.Could this be possible in SQL because I only want to update last 3 digits only.I tried doing it via sequence but couldn't able to do it.

like image 527
TruePS Avatar asked Mar 21 '23 02:03

TruePS


1 Answers

The way to do this is:

ALTER TABLE tbl AUTO_INCREMENT = 80000;

I think you have a bad plan though. For one thing, if you have multiple key fields then use multiple fields for them because that will work much faster. Secondly, you could never have more than 999 users on your site using this before it'll break. Third, noone else is going to have any idea of what you are doing. This will be hard to maintain. Three good reasons should be enough to change your mind.

Update

To alter the auto increment table:

ALTER tbl MODIFY `rowid` int(11) NOT NULL AUTO_INCREMENT;

To update the table:

UPDATE tbl SET `rowid` = `rowid`-80000;
like image 106
krowe Avatar answered Mar 22 '23 20:03

krowe