I need to insert old data to a new database which I want to keep the old database ID value to the new database ID (the new database ID field is the auto increment ID). So how can I do this for the auto increment ID in MySQL (no way to export / import as I do not have the right for doing this, I can only insert data from the old database and the structure of new database and old database are different), so how can I do this?
## how to disable the auto increment id in new_table?
insert into new_table(id, new_field1) select id, old_field1 from old_table;
## how to re-enable the auto increment id and set the initial value to the max(id)?
Just building upon the comment from P.Salmon, it seems that it should just happen without any problem when you insert values to both the id and other fields at the same time.
However if you for some reason need to change the id given to the next inserted row in the table, you have use an alter table statement. Just like the one in the example on the page P.Salmon linked to https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html
ALTER TABLE new_table AUTO_INCREMENT = 100;
Related
i'm deleting previous data and trying to insert new list of data,id values are keep on increment because of auto-increment. is it possible to have new auto increment id with 1 ?
and i tried with ALTER TABLE table AUTO_INCREMENT = 1; its not working for me.
Use this query while deleting your old 20 Records.
truncate table YourTableName;
It will reset the database structure and if you insert new record it will start from 1(one) id again.
You cannot reset the counter to a value less than or equal to the
value that is currently in use. For both InnoDB and MyISAM, if the
value is less than or equal to the maximum value currently in the
AUTO_INCREMENT column, the value is reset to the current maximum
AUTO_INCREMENT column value plus one.
And also read this article Link
Just visit this question
I accidentally deleted some rows in my database, but I have a copy of the data in a backup.
Is it possible to insert the rows back in with the same id (auto-increment field). Or do I have to use the auto-increment generated id then update all the relationships manually?
You can, just by setting the field when INSERTing. Autoincrement only works if no value is specified for the field.
I am facing one problem with one of my project.I have a inventory upload which import data from csv to a innodb table. What happened here is with a multiple load data command (i suppose) the auto increment value get sets to 18446744073709551615 and not letting other insert to work. It was working fine before. I am not sure if the large amount of data in the table creates this issue or not.
Details of the table is as follows
Software version: 5.5.31-0ubuntu0.12.04.1-log - (Ubuntu)
largest insert id used : 17455787099
number of rows in the table : 23887371
some variables realted to this are as follows
auto increment increment 1
auto increment offset 1
autocommit ON
automatic sp privileges ON
innodb autoextend increment 8
innodb autoinc lock mode 1
sql auto is null OFF Documentation
i have removed the delete queries from the table. But still the autoincriment is out of sync
any help is much appreciated
thanks
Nithin
the auto increment value get sets to 18446744073709551615-largest
insert id used : 17455787099
This is a bit confusing.
Try resetting the auto increment value before loading new files:
ALTER TABLE tableName AUTO_INCREMENT = 1
EDIT:
Create a new identical table :
CREATE TABLE tableName LIKE oldtableName;
Copy all rows to the new table, remember to not select the auto_incrementing id.
INSERT INTO tableName (field2, field3, field4)
SELECT field2, field3, field4 FROM oldtableName ORDER BY oldtableName.id;
DROP oldtableName;
RENAME tableName oldtableName;
This will take a while(hours..or more).
EDIT2
If your id column is not referenced by anything
ALTER TABLE tableName DROP id
ALTER TABLE tableName ADD id INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (id), AUTO_INCREMENT=1
The case:
Have huge table that every 24 hours need to delete all data and get new data instead.
How can I truncate this table and insert new data without block users to read data from this table?
What I thinking to do: (please write which option better or suggest more proper option or standard solution)
Option1:
1. insert new data to temp table
2. drop old table
3. rename temp table to the table name
Option2(problem: user cant access the table data in middle):
1. truncate the table
2. insert new data to table
1.insert new data to temp table
2.CREATE TABLE newtable select * from oldtable where 1=2
3.drop old table
4.INSERT INTO newtable select * from temp
If you have any old data to keep:
Add a id column to your table. Fill it with a hash value computed by the value of all columns in a row. Load the new data into a temporary table. Update the old data with the changed and new rows.
Otherwise your first solution is the way to go, since the renaming of a table does not take mch time.
Will the ID auto-increment value be reset if I drop (wipe) a MySQL table? And, if I delete (for example) the entry N° 535, will this entry number be filled again later?
I don't want that ID to be filled with other new entries if I wiped old data. If this is not the behavior, then what's the solution to avoid this?
Which DBMS are you using? MySQL does reset the auto-increment value when you TRUNCATE a table. You can use the (much slower) DELETE FROM tablename to avoid this.
The auto_increment value doesn't change if you DELETE a line, but it is reseted if you do a TRUNCATE TABLE. And the next ID is always the current auto_increment value ("gaps" aren't filled again).
You can change the auto_increment value with ALTER TABLE table AUTO_INCREMENT = num
Yes. The solution would be to not DROP your table. Instead use DELETE FROM ...
If you drop a table, it will be gone along with any identity seed values.