This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Problem with autoincrememnted “id” column
My db table looks like this pic. http://prntscr.com/22z1n Recently i've created delete.php page. it works properly but when i deleted 21st user next registered user gets 24th id instead of 21. Is it possible to put newly registered users info to first empty row? (In this situation 21st row)
In my registration form, newly registering user can write names of existing users, and be friends with them after registration. For this friendship i have another table that associates id of newly registered user and existing user.For this purpose i'm using mysql_insert_id during registration to get id for new user. But after deletion of 21st row during next registration process mysql_insert_id gave me number 21. but stored in 24th row. And put to associations table 21 for new user. I wanna solve this problem
You received the answer the last time you posted this question. MySQL maintains an internal counter that is incremented every time a new row is inserted into a table with an auto-increment column. The increment value does not go down when a row is deleted.
To make things work the way you want, you will need to avoid using MySQL autoincrement, and implement your own solution to create and increment IDs.
mysql_insert_id() is a very very bad approach to get your id. You can always set the auto_increment value of your table, but I wouldn't suggest it. You should always do a SELECT of the latest record you insert based on some unique values, and order it by id DESC while limiting it to one result. Don't ever use mysql_insert_id().
It makes no difference - mysql_insert_id will return the autoincremented ID of the last inserted row. If the row gets ID 24, mysql_insert_id will return 24.
However, you can change the next value autoincrement will result it. Read up on it here.
DROP the field you are auto_incrementing
ALTER the table to ADD the field again with the same attributes
All existing rows are renumbered and the next auto_increment number will be equal to the row count plus 1
WARNING:
There really is no reason you would need to do this or should do this! If you have references to these ids anywhere, which I know you do from earlier questions - this can break every single link.
Related
I was just creating a new table using MySQL Query Browser, and noticed there's a tick under Auto Increment Column. How does that work?
When adding to the database programatically, do I just add a number, and then the database automatically increments that number?
Everytime a NEW user registers on my site, I want their Customer ID (integer only) to auto increment, so I don't have to try and randomly generate a unique number.
Can this be done simply?
Thank you!
When adding to the database programatically, do I just add a number, and then the database automatically increments that number?
Yes, that's the way auto_increment works.
The value will be incremented for each new row
The value is unique, duplicates are not possible
If a row is deleted, the auto_increment column of that row will not be re-assigned.
The auto_increment value of the last inserted row can be accessed using the mySQL function LAST_INSERT_ID() but it must be called right after the insert query, in the same database connection
mySQL Reference
1 more,
You can insert your own value also (ie your random value).
Yes. Auto_Increment columns work like they say on the tin. Tips
when INSERT - ing, use NULL or omit the column
Use LAST_INSERT_ID() (or API equivalents) to obtain the last generated value.
for security and business logic reasons, it's usually better form to not directly use a key value for a customer identifier. Consider using Hashed / randomised surrogate customer keys instead.
Ta
Yes, that's the exact purpose of AUTO_INCREMENT. It looks at whatever is the current increment value for that table, and stores that value plus 1 for the new row that comes in, automatically. You can omit that field from your INSERT statements and MySQL will handle it for you for every new row that comes in, giving each row its own unique ID.
When you enable Auto Increment an ID will always get automatically added whenever a new record is made.. Example:
If you have 1 record with ID 1 in your table and you add a new record, the ID will automatically be 2.
I have some tables in my phpmyadmin with one column that is auto incremented.
The problem is that when I delete some rows from the table (For example the element Car, with index auto incremented 1) and I create another row into the table, the new row have the index 2, but in the table there is only one row.
I want that this second element created to have the index equal to the position of the row, for example, if I have 3 rows that the third element will have the index equals to 3.
I was looking for a method that let me to use my phpMyAdmin like this but I couldn't find anything.
Is it possible? If it is true, what should I have to do? Do I have to create the table again?
This is generally a bad idea. Auto increment is used for creating unique ID of the row. Imagine you have a record "1 - John". Then you delete it and add another "1 - Jack". From the point of common database logic, it will seem that John was renamed to Jack (it has the same ID = it is the same entity) rather than it is another record. You should let DB assign new ID to each new record, even with leaving gaps after deleted records.
If you really want to do so, you can modify auto increment value using this query:
ALTER TABLE users AUTO_INCREMENT=123
but it is still not the way auto increment is designed for.
It is possible, but you shouldn't do that on production.
The SQL query is:
ALTER TABLE tablename AUTO_INCREMENT = 1
The most important part of this is to prevent overriding. For example you have an user and this user has id of 81 and if you delete this user and the database doesn't remember that this id 81 has ever been taken by an user (and for example, you have some relations - like friend lists) the user that is going to have the same ID will probably have the same data.
So basically, you don't want to reset auto increment values.
Execute this SQL sentence:
ALTER TABLE tablename AUTO_INCREMENT = 1
I struggled with this a bit too, then found in the "Operations" tab, there is a setting under "Auto Increment" where you can set the number it will start from, so you can roll it back if you want to do that.
It is possible. It is not a feature of phpmyadmin, but of mysql. Execute this
ALTER TABLE tablename AUTO_INCREMENT = 1
More info on this on stackoverflow
And in the mysql reference
I was searching for a solution to my problem and didn't find one. Basically the user can add/delete information which will delete the row. The user can also arrange this information up/down which would also switch the primary keys (called placeholder). The problems are is that if the user deletes a row it won't move the placeholder int up one. I was wondering If there's any way to move the rows up and arrange them starting from 1.
You shouldn't amend auto increment. It's part of the database design. You should use another column name as the identifier, ID perhaps?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to fill in the “holes” in auto-incremenet fields?
We are currently using auto-increment on a table where entries come and go constantly. The problem with this, is that eventually the auto-increment id becomes huge, as that is how auto-increment works.
We would like to have it always add +1 to the last entry.
For example:
We have 4 entries and id 4 is deleted. Next added entry should get id 4, and not 5.
I am not sure if this has been asked before. But after searching I was only able to find solutions on how to get the next auto-increment number, which is not what I am looking for at all.
YOU SHOULD NOT DO THAT because auto_increment is designed this way for good reasons (like if you have a backup and you want to restore it when it contains old deleted id that has been rewrote, how do you do ?)
But to answer your question:
You have to use
ALTER TABLE `table` AUTO_INCREMENT = MAX(id)+1;
After a delete, you can make a trigger
If you really want to do that (I personally encourage you to use auto-increment), you need to perform a transaction in order to get the last ID and insert the new row setting its ID incrementing it according to the other one.
It's not a good idea to do this, the ID should always stay unique, no matter if the record exists or is deleted.
However if you really want to do it, you can to it with something like
select max(id) + 1 of bla;
But you need the right transaction level for it because if you don't you have a possibility of duplicated ids.
You can choose maximum id value and just increment it.
SELECT MAX(`id`)+1 as `new id` FROM `table`;
If you have 5 entries and 3rd gets deleted, this will still get you 6 as next id.
My db table looks like this pic. http://prntscr.com/22z1n
Recently I've created delete.php page. it works properly but when i deleted 21th user next registered user gets 24th id instead of 21.
Is it possible to put newly registered users info to first empty row? (In this situation 21th row)
In my registration form, newly registering user can write names of existing users, and be friends with them after registration. For this friendship i have another table that associates id of newly registered user and existing user.
For this purpose i'm using mysql_insert_id during registration to get id for new user. But after deletion of 21th row during nex registration process mysql_insert_id gave me number 21. but stored in 24th row. And put to associations table 21 for new user. I wanna solve this problem
When you use an autoincrement id column, the value that the next entry will be assigned will not be reduced by deleting an entry. That is not what an autoincrement column is used for. The database engine will always increment that number on a new insert and never decrement that number on a delete.
A MySQL auto_increment column maintains a number internally, and will always increment it, even after deletions. If you need to fill in an empty space, you have to handle it yourself in PHP, rather than use the auto_increment keyword in the table definition.
Rolling back to fill in empty row ids can cause all sorts of difficulty if you have foreign key relationships to maintain, and it really isn't advised.
The auto_increment can be reset using a SQL statement, but this is not advised because it will cause duplicate key errors.
-- Doing this will cause problems!
ALTER table AUTO_INCREMENT=12345;
EDIT
To enforce your foreign key relationships as described in the comments, you should add to your table definition:
FOREIGN KEY (friendid) REFERENCES registration_table (id) ON DELETE SET NULL;
Fill in the correct table and column names. Now, when a user is deleted from the registration, their friend association is nulled. If you need to reassociate with a different user, that has to be handled with PHP. mysql_insert_id() is no longer helpful.
If you need to find the highest numbered id still in the database after deletion to associate with friends, use the following.
SELECT MAX(id) FROM registration_table;
Auto increment is a sequence key that's tracked as part of the table. It does not go back when you delete a row.
Easily, no. What you can do (but I don't suggest doing) is making an SQL function to determine the lowest number that isn't currently occupied. Or you can create a table of IDs that were deleted, and get the smallest number from there. Or, and this is the best idea, ignore the gaps and realize the database is fine.
What you want to do is achievable by adding an extra column to your table called something like user_order. You can then write code to manage inserts and deletions so that this column is always sequential with no gaps.
This way you avoid the problems you could have messing around with an auto_increment column.
It's not a good practice to reset auto_increment value, but if you really need to do it, so you can:
ALTER TABLE mytable AUTO_INCREMENT = 1;
Run this query after every delete. Auto_increment value will not be set to 1, this will set the lowest possible value automatically.