4

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.

9
  • 3
    What do you mean by "last 3 digits only"? Which is your database? SQL Server, MySql, Oracle etc...
    – TechDo
    Mar 26, 2014 at 5:35
  • 3
    what id to give after 80999 ? Mar 26, 2014 at 5:40
  • 1
    What is your database?
    – Madhawas
    Mar 26, 2014 at 5:41
  • 1
    Is there a reason you don't want more than 999 users?
    – Prix
    Mar 26, 2014 at 5:46
  • 1
    And how to you plan on controlling it to change it again
    – Prix
    Mar 26, 2014 at 5:47

2 Answers 2

5

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;
5
  • after 80999 I want i digit increment that is after 80999 I want 801000.but currently I want only 999 users
    – TruePS
    Mar 26, 2014 at 5:51
  • 1
    Another option: If the 80 will truly ALWAYS be 80 then just store the trailing part in the db as a normal integer. In your application code you can then just concatenate it on at runtime (eg in PHP: $id='80'.$result['id'];).
    – krowe
    Mar 26, 2014 at 5:54
  • I am using java actually and secondly How can i update an autoinc field.
    – TruePS
    Mar 26, 2014 at 6:00
  • 1
    Ok, well the same idea would apply for any language. The answer would depend on what you want to alter. To change the structure use: dev.mysql.com/doc/refman/5.1/en/alter-table.html . To change the data use: dev.mysql.com/doc/refman/5.0/en/update.html
    – krowe
    Mar 26, 2014 at 6:04
  • 1
    @TruePS I added examples of both above.
    – krowe
    Mar 26, 2014 at 6:09
2

If you already have table Try this

ALTER TABLE student AUTO_INCREMENT=80000;

or if you are going to create the table for the first time then you can use the below query

CREATE TABLE `student ` (
  `rowid` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`rowid`),
  UNIQUE KEY `rowid_UNIQUE` (`rowid`)
) ENGINE=InnoDB AUTO_INCREMENT=80000 DEFAULT CHARSET=latin1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.