I am working on an e-commerce webapp and here's what I am thinking about.
To make it simple in this question, the user
table contains two columns:id
(int,PK,NN,AI),name
(varchar(45),NN). I want the employees have shorter ids, so I want to preserve short ids and assign them later. The id is divided in three categories.
- 1-1000 preserved for employees.
- 1001-5000 preserved for partners.
- 5001+ for users.
So is there a way I can manipulate the autoincrement id? My currently walkthrough is: fires create garbage records to fill up the ID, then allocate them for employers,partners.
Best Answer
To change the AUTO_INCREMENT value you would have to make the next query:
Where you would have to replace next_id with the value that is going to be set when you omit the id in the next insert statement.
Yet the property AUTO_INCREMENT value doesn't work like you want, at least not alone, you would have to search each time which is the next value you are looking, doing a sql query in the 3 cases you said.
For example, for the case you are looking for an employee next id, you would have to make the next query:
And then do a plus 1 to that value to get the id you are setting in your INSERT with out using AUTO_INCREMENT property, or make the first given statement to alter the table AUTO_INCREMENT value and then omit the id in the insert.
One approach to do this is by doing the procedure I just explained with stored procedures:
And every time you want to insert a user then write the next statement: