I have a database in phpmyadmin and there is one thing I don't understand with
the auto_increment. I have several tables with each id auto_increment. If I remove a data with the id-number of 3 for example and then add a new data why does it
print out id-number 4. I just deleted the id 3 shouldn't it print out id 3 again?
Preview
After each insert to the table, the autoincrement value is incremented by 1. So when you add row with ID 3, then autoincrement will be 4. Its not changed, when you delete some row(s).
To change autoincrement value (ID of the next inserted row), use this query:
ALTER TABLE table_name AUTO_INCREMENT = 3
Here is the documentation:
http://www.w3schools.com/sql/sql_autoincrement.asp
https://dev.mysql.com/doc/refman/5.6/en/example-auto-increment.html
Related
I have an id i.e primary key and auto increment. Is there any query to update my existing id and make my id start from 1 and next id 2 and so on..
For example
id name
3 ABC
5 XYZ
9 PQR
NOTE: id is already primary and auto increment and I don't want truncate my id.
if possible i want to get
id name
1 ABC
2 XYZ
3 PQR
ALTER TABLE table AUTO_INCREMENT = 1; is not my solution.
Thanks
Of course there is a way:
set #counter = 0;
update table_name
set id = (#counter := #counter + 1);
EDIT
To avoid problem with duplicate keys you can run something like this before to temporary change current ids to negative equivalents:
update table_name
set id = 0 - id;
Is there any query to update my existing id and make my id start from 1 and next id 2 and so on
What you can do is transfer the content of your table to another table. Reset the auto increment counter, insert your data back into the original table but let MySQL assign the primary key.
Assuming your table name is mytable You do it like this:
CREATE TABLE mytable_tmp select * from mytable;
TRUNCATE TABLE mytable;
ALTER TABLE mytable AUTO_INCREMENT = 1;
INSERT INTO mytable(name) SELECT name FROM mytable_tmp ORDER BY id;
DROP TABLE mytable_tmp;
In my opinion you shouldn't mess with auto_increment columns at all. Let them be as they are. Their only job is to identify a row uniquely. If you want a nice serial number use another column (make it unique if you wish)!
You will always run into trouble and there will always happen things, that mess with your nice 1, 2, 3, ... sequence. A transaction gets rolled back? Boom, your sequence is 1, 2, 3, 5, ... instead of your intended 1, 2, 3, 4, ...
This can also be a very heavy operation. An auto_increment column is always also a primary key. Every other index on this table includes the primary key. Every time you reset your auto_increments, every index on this table is rewritten.
So my advice is, don't mess with auto_increments.
This query will work for your scenario:
ALTER TABLE tablename DROP id
ALTER TABLE tablename ADD id INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (id), 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?
I have a column in a MySQL table which enters an auto-increment ID as new rows are added. Data from the MySQL table is pulled into an HTML table and the rows of the HTML table are sorted by the ID column. Normally this works fine for my purposes, but I now need to enter a row with a lower ID than the current auto-increment value, so that it appears further down the table.
So, I am wondering if there is a way, using a MySQL statement to add 1 to the ID values for a specific range of rows, e.g. add 1 to rows 800-850, so that they would now be 801-851, and then I could manually insert a new row with an ID value of 800.
You need to let the auto-incrementation working alone and create a new field table_id in your table to set the id number you want.
Reset table id is never a good idea : for example, don't forget all foreign keys linked on your table id.
To increment 1 to your specific id number, it would be :
UPDATE your_table SET table_id = table_id + 1 WHERE table_id BETWEEN 800 AND 850
update [yourtablename]
set [youridfieldoftable]=[youridfieldoftable] + 1
where [youridfieldoftable] >= 800
and [youridfieldoftable] <= 850
Be sure to substitute the table and field names with the correct ones. The field is the same four times.
In order to avoid the error
Duplicate entry x for key 'PRIMARY'
add the ORDER BY clause in the suggested query
UPDATE
your_table
SET
table_id = table_id + 1
WHERE
table_id BETWEEN 800 AND 850
ORDER BY
table_id DESC
I have a table which has a structure like as below.
create table test_table (id INT NOT NUll AUTO_INCREMENT
, name varchar(100),
primary key (id))ENGINE=INNODB
Select * from test_table;
id name
1 a
2 b
3 c
Now I want to increment the id by a number lets say 2
So the final results should be
Select * from test_table;
id name
3 a
4 b
5 c
The way I can do it is, first remove the PK and auto increment and then
update the table:
update test_table set id=id+2;
The other way is to make a temp table with out PK and auto increment and then
extract the result to the main table.
Is there any other way to do this without destroying the table structure ?
I am using MYSQL.
In your example, you need to remove the PK first to allow (temporary) duplicate id's during the course of the update.
To avoid duplicates, you must perform an ordered update:
UPDATE test_table SET id = id + 2 ORDER BY id DESC;
This will update records with largest value of id first, hence avoiding collision.
Obviously, if you want to decrement the values of id, then use "ORDER BY id ASC".
Here is the query to update the tables in SQL :- Its generic
UPDATE table_name SET column1=value, column2=value2,WHERE some_column=some_value;
Please follow the link for more information
Update Query
Thanks,
Pavan
I need to create an append query, that appends many records to a table. this table has a primary key, that is a sequential number. How do I make my append query, append records to the table and automatically assign the next sequential number for the primary key? I woudl need to run this query on a live multi-user MYSQL server throughout the day
thanks!
If the PK is a true auto-incremental field, you should be able to leave the PK out of your 'append' query. The table will automatically assign the next value in sequence to your data row(s) that you are inserting.
ex: If you have this data in table names
id name
1 Ken
2 Jon
3 Steve
And you run this query
INSERT INTO names (name) VALUES ('Peter')
Your table should automatically assign id # 4 to Peter
If the sequential PK is maintained manually, I would suggest you alter that field to be a true auto-incremental field if at all possible, or create a new auto-increment field and drop the old one. Just make sure you update any other related tables before you drop the field.