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.
Related
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?
To make this quick : Primary key ID column has auto increment but I also have another column called transaction ID, which is not unique, and can be 1 for 3 rows, then 2 for 4 rows, 3 for another say 2 rows and so on.
Is it ok to get MAX(transaction_id) and ++ ? Somewhere here it was said that there might be a lock problem when 2 or 3 users initiate a new transaction, so they can use same transaction_id. I cannot use AUTO_INCREMENT on this one. Am I right or wrong ?
Thanks,
Adrian
i have another field that describes the owner of the transaction_id...so even if there are transaction_id's with same value, the owner will be different...just worrying too much about the AUTO_INCREMENT feature :)))
cheers
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?
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.