http://tinypic.com/view.php?pic=2hd2ush&s=8#.U7vlf_mSzNg
I created the table which have auto_increment in id field, when i delete some row & insert another row it having a gaps between them. For example id is 1,2,3,4,5. Deleted 3,5 and insert another row, the id will be 1,2,4,6, it should be 1,2,3,4,5. Please help.
That behaviour is normal and desired. You don't want to "reset" auto_increment counter, its job is not to provide you with nice, sequential numbers.
Thanks for the clarify N.B
Related
i have a field id in my table which is autoincrement field. It starts from 1,2,3,........ Now every year April 1st i need to reset this id. What ever record i enter from April 1st should start again from 1. It doesnt need to be updated automatically. I can do it manually every year. What are the options available to do the same? Can anyone please suggest a simple way through which i can achieve the same.
Table tender
id (autoincrement)
tender_id
...
...
Use alter table to modify the auto_increment for table.
alter table t1 auto_increment = 1;
I made a table in a mysql database for testing purposes.
The id is auto incremented. After doing this(not together)
delete from test where id=4;
alter table test auto_increment = 4;
insert into test(nume) values('dan');
It does not give any errors. But the last id is 5, not 4. Should not this be working?
After delete write this query
ALTER TABLE tablename AUTO_INCREMENT = 1
Question yourself, whether you need to alter the primary key. In most legitimate cases - no.
This will partially work, once you insert a row, the ID will be 4, but auto_increment will change to 5.
As a result, next row insertion will give you a primary key duplication error.
This whole procedure has only use in case you do delete the record with the highest id.
Should you delete where id=2, you cannot change the autoincrement id. For whatever reason you do not like to get a gap in the id line at id=4, in case of deleting id =2 there will be a gap. So why mess with the ids when after adding a new record id =5 you would have the same kind of gap.
But what about racing conditions. You delete record id=4. two millisec later I add a record, getting id=5. What will happen to the auto increment id?
What legit reason is there for avoiding these gaps?
right, i have a table with the fields: id, title, description, keywords and link. In the table i have hundreds of websites for a search engine. I am struggling though in this section. The id is set to auto-increment and i would like to be able to add a line of code that embeds itself in the table, in the middle somewhere, with the id tag (auto-increment) to adjust in the whole table. So if i was to add 'Google' to id '81', 81 will go to 82 to adjust to it. is there a way to do that with SQL?
AUTO_INCREMENT is not really meant to be used the way you want to use it. It is mainly just a reference to any one row in your table for the main purpose of being used as a foreign key in other tables. If you want to have your rows sorted in a certain way you should have another column that you can influence.
EDIT:
But now just in case you really want to renumber and your autoincrement ID is not used in any other table for reference and nobody is currently using the table you can
remove the ID column
add the ID column without auto_increment
update the ID for 'Google' as 81
modify the ID column to be auto_increment again
Maybe like this
alter table mywebsitetable drop column id;
alter table mywebsitetable add column id bigint;
update mywebsitetable set id = 81 where websitename = 'Google';
... do more numbering as you like and this does give numbers to all the other rows:
alter table ify.test modify id bigint primary key auto_increment;
And if you want to keep the all the IDs then you can even add another column to save the ID column, copy them over with an two update statements. One update for the IDs before 81 and another one after the 81 one where it is incrementing the ID. Then you add the drop and add the column, copy over the IDs from the save column, drop the save column, add google and switch on auto_increment again. But this will take long when your table is large and I do not recommend this because as I said before, auto increment is not meant for ordering or as the mentioned in that one comment, auto_increment is not supposed to be treated like lines in a spread sheet.
I have a problem that whenever I delete a row, the row ID corresponding to that row gets deleted, but I don't want this. What I want is if any row is deleted, then other rows after that row should shift one (the no. of rows deleted) position up.
Example:
Suppose there is a user table(id and name)
id(auto incremented primary key) name
1 xyz
2 aaa
3 ray
4 mark
5 allen
now delete row with id=3 and table should look like
id(auto incremented primary key) name
1 xyz
2 aaa
3 mark
4 allen
Is there any way to accomplish this?
No! Don't do this!
Your Autoincrement ID is the IDENTITY of a row. Other tables use this ID to refer to a certain row. If you update the ID, you would have to update all other tables referencing this row, which is not at all the point of a relational database.
Furthermore, there never is a need to do this: you won't run out of autoincrement columns fast (and if you do, just pick a bigger datatype).
An autoincrement ID is a purely technical number, your application users should never see or use it. If you want to display an identificator to your users, add another column!
You've completely got the wrong end of the stick. Auto numbers should not be changed as this would break the link between any other referencing tables.
What you want, by the sounds of it, is a row counter, not a primary key.
While its generally not recommended to change these values, there do exists instances where you may need to change them. If you have the appropriate Foreign Key relationships setup to cascade on UPDATE then you could do this. Granted you need to be 100% all FK relationships are defined as expected.
I have a table with an auto_increment field and sometimes rows get deleted so auto_increment leaves gaps. Is there any way to avoid this or if not, at the very least, how to write an SQL query that:
Alters the auto_increment value to be the max(current value) + 1
Return the new auto_increment value?
I know how to write part 1 and 2 but can I put them in the same query?
If that is not possible:
How do I "select" (return) the auto_increment value or auto_increment value + 1?
Renumbering will cause confusion. Existing reports will refer to record 99, and yet if the system renumbers it may renumber that record to 98, now all reports (and populated UIs) are wrong. Once you allocate a unique ID it's got to stay fixed.
Using ID fields for anything other than simple unique numbering is going to be problematic. Having a requirement for "no gaps" is simply inconsistent with the requirement to be able to delete. Perhaps you could mark records as deleted rather than delete them. Then there are truly no gaps. Say you are producing numbered invoices: you would have a zero value cancelled invoice with that number rather than delete it.
There is a way to manually insert the id even in an autoinc table. All you would have to do is identify the missing id.
However, don't do this. It can be very dangerous if your database is relational. It is possible that the deleted id was used elsewhere. When removed, it would not present much of an issue, perhaps it would orphan a record. If replaced, it would present a huge issue because the wrong relation would be present.
Consider that I have a table of cars and a table of people
car
carid
ownerid
name
person
personid
name
And that there is some simple data
car
1 1 Van
2 1 Truck
3 2 Car
4 3 Ferrari
5 4 Pinto
person
1 Mike
2 Joe
3 John
4 Steve
and now I delete person John.
person
1 Mike
2 Joe
4 Steve
If I added a new person, Jim, into the table, and he got an id which filled the gap, then he would end up getting id 3
1 Mike
2 Joe
3 Jim
4 Steve
and by relation, would be the owner of the Ferrari.
I generally agree with the wise people on this page (and duplicate questions) advising against reusing auto-incremented id's. It is good advice, but I don't think it's up to us to decide the rights or wrongs of asking the question, let's assume the developer knows what they want to do and why.
The answer is, as mentioned by Travis J, you can reuse an auto-increment id by including the id column in an insert statement and assigning the specific value you want.
Here is a point to put a spanner in the works: MySQL itself (at least 5.6 InnoDB) will reuse an auto-increment ID in the following circumstance:
delete any number rows with the highest auto-increment id
Stop and start MySQL
insert a new row
The inserted row will have an id calculated as max(id)+1, it does not continue from the id that was deleted.
As djna said in her/his answer, it's not a good practice to alter database tables in such a way, also there is no need to that if you have been choosing the right scheme and data types. By the way according to part od your question:
I have a table with an auto_increment field and sometimes rows get deleted so auto_increment leaves gaps. Is there any way to avoid this?
If your table has too many gaps in its auto-increment column, probably as a result of so many test INSERT queries
And if you want to prevent overwhelming id values by removing the gaps
And also if the id column is just a counter and has no relation to any other column in your database
, this may be the thing you ( or any other person looking for such a thing ) are looking for:
SOLUTION
remove the original id column
add it again using auto_increment on
But if you just want to reset the auto_increment to the first available value:
ALTER TABLE `table_name` AUTO_INCREMENT=1
not sure if this will help, but in sql server you can reseed the identity fields. It seems there's an ALTER TABLE statement in mySql to acheive this. Eg to set the id to continue at 59446.
ALTER TABLE table_name AUTO_INCREMENT = 59446;
I'm thinking you should be able to combine a query to get the largest value of auto_increment field, and then use the alter table to update as needed.