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?
Related
Currently the table structure is like this:
user_preference
---------------
id
user_id
pref_id
this table store all the user options, and id is auto -inc
the problems are:
1) is it necessary to keep an ID for every table ? It seems the common practice to keep a system generated id for every table
2) whenever the user update their perference, I will clear all related record for him and insert the update one, the auto-inc number will become very large later. How can I prevent that?
Thanks for helping.
You can periodically reset the auto increment counter back to 1, to ensure that the id does not become arbitrarily large (and sparse) over the course of frequent deletion of records.
In MySQL:
ALTER TABLE table_name AUTO_INCREMENT = 1
In SQL Server:
DBCC CHECKIDENT (table_name, RESEED, 0)
Each of these commands will reset the auto increment counter to 1, or to the value closest to 1 if 1 be already in use by another record.
You do not need to have an AUTO_INCREMENT PRIMARY KEY for every table. Sometimes there is a 'natural' key that works quite well for the PK.
Do not manipulate AUTO_INCREMENT values. Do not depend on any property other than uniqueness.
Your user_preference table smells like a many-to-many mapping? If so, this is optimal:
CREATE TABLE user_preference (
user_id ...,
pref_id ...,
PRIMARY KEY(user_id, pref_id),
INDEX (pref_id, user_id)
) ENGINE=InnoDB;
For discussion of "why", see http://mysql.rjweb.org/doc.php/index_cookbook_mysql#many_to_many_mapping_table
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
I have table that have 4 fields.
i.e.
1. id : Primary key, Auto Increment ,
2. name,
3. salary,
4. designation.
when I add data id increments automatically.
Suppose when I remove or delete row that have id = 15 i.e. last record and after that if I add new record then id starts from 16.
So that is my problem, when I remove record It should be permanently removed.
and id starts from 15 again.
Any suggestion for it ?
The purpose of the Primary Key on a table on RDBMS is to uniquely identify a record FOREVER. Even if you delete this record, its id "remains there" forever. The world needs to know that the record with id e.g. 15, existed once, but does not exist anymore. You should not be reusing primary keys that have been deleted. It is a wrong business and technical approach. You will get into much trouble.
If you have used sequence for auto increment for the 'id' and you delete a record with id = 15 then the sequence will get the next value i.e 16 as it has already picked the value 15 as it is independent of whether the id exists in the table or not. Hope it helps you.
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.
How would I ensure that when something is deleted in a database the id of the records are changed so I get a continuous, consecutive collection of ids, e.g. I have a record with ID 1 and another record with ID 2 and another with ID 3, when 2 is deleted, how can I ensure that the ID of the record '3' will now be '2'.
You normally don't, in particular if this is a primary key and even more so if it is used as a foreign key (then you will need to change it everywhere it is referenced).
Having "holes" in your table is not an operational issue at all.
Let $deleted_id be the ID of the record you deleted.
UPDATE yourtable SET id = id - 1 WHERE id > $deleted_id
But please be aware that you also have to update all references to your IDs, if any exist.
Your not supposed to do this with Primary Keys as it would effect other records in tables that are joined.
Maybe if you have a specific reason for doing this then we can help?