I create a new db (usuarios) and i want to insert old values into a new table but my SQL backup also saved the auto_increment value and it is the primary key so when i import the file an error message show and it is due to the primary key value is already used in the new table, for instance:
INSERT INTO `usuarios` VALUES
(5, 'USERBETA', 'USERINFO')
It says that the primary key 5 is duplicated. How can I fix this to ignore the old auto_increment value and use the a new one instead?
UPDATE: This query will change your existing data to use much larger ID numbers, at which point you could then import the old data. There should be no ambiguity as long as you move existing table data IDs to a large enough number, and then import the old data.
UPDATE usuarios SET id = (id+10000)
(10000 is arbitrary, it should be some number larger than the largest existing ID in old data)
Then re-set your ID number at 1:
ALTER TABLE `usuarios` AUTO_INCREMENT = 1;
Then import your data.
ORIGINAL ANSWER
If you don't have relational tables (nothing depending on the fixed ID's already set in table usuarios) then you can just import without the id column. But, if you need to keep those old id already assigned, you could turn off auto_increment before import and then re-enable once you've inserted everything.
Presuming id is the column of your id:
Remove any existing (old) data:
TRUNCATE TABLE usuarios
Remove the auto_increment
ALTER TABLE usuarios CHANGE `id` INT(11) NOT NULL
Import your data like you have been.
Then, re-add the auto_increment:
ALTER TABLE usuarios MODIFY `id` INTEGER NOT NULL AUTO_INCREMENT;
ALTER TABLE `usuarios` AUTO_INCREMENT = 1234;
Related
I have a table with a unique auto-incremental primary key. Some entries have been deleted from the table, so there are gaps in the ids, it is not a sequence.
I need a query that will make it a sequence. the table is not connected with any other table, so I can temporarily remove the pk and auto-increment from the id column (until the ids will be a sequence).
I use SQL server
If possible, I want to run the query starting from specific id
You cannot update identity column values, nor you can remove the identity from the column, update the values and set it back. You must create a new table with the same schema, copy the data from the old table, but for ID generate a new value, drop the old table and rename the new one to keep the same name.
use [tempdb]
go
if OBJECT_ID('TestTable') is not null
drop table TestTable
go
create table TestTable (
ID int not null primary key clustered identity(1,1)
, Name varchar(50)
)
insert into TestTable(Name) values
('Row #1'),('Row #2'),('Row #3'),
('Row #4'),('Row #5'),('Row #6'),
('Row #7'),('Row #8'),('Row #9')
delete from TestTable where ID in (3, 4, 8) -- Make some gaps
create table TestTable_NEW (
ID int not null primary key clustered identity(1,1)
, Name varchar(50)
)
insert into TestTable_NEW(Name)
select Name
from TestTable
order by ID -- Preserve the rows order
drop table TestTable
exec sp_rename N'TestTable_NEW', N'TestTable'
However, I will not recommend doing this at all. Identity values are supposed to be immutable. They should never change. If you have problems with gaps, then do not allow deletion of existing records. Also, identity do not guarantee that there will be no gaps in the sequence. Only that the values will be unique and increasing. So you should definitely reconsider your database and/or application design, because it is flawed.
I have a table in which I took two field one is id that is primary key with Auto Increment attribute (tinyint) and another one is name (varchar(20)).
id tinyint(4) NOT NULL AUTO_INCREMENT, name varchar(20) NOT NULL,
PRIMARY KEY (id)
after that I have insert the values in table from 1 to 127 (max limit of tinyint). Now If I try to insert the record in table it gives error because I have reached at max limit of tinyint. I am fine with it. But If I delete all the records and table is empty then also I can't insert any record. I know I can use truncate here that will reset the primary key. But My question is here that why mysql doesn't insert the any available value(from 1 to 127) for primary key and If I manually insert the value for id from 1 to 127 it works.
insert into `new table` (id,name) values(1,'blahblahblah') Working
insert into `new table` (name) values('blahblahblah') Not working
If I have a application with large database and I come this situation and any record insertion can fail in future then how can I know before occurred this.
Is there any way by which I can insert the record(in empty table by delete all records) without truncating the table.
Sorry for my poor English.
Thanks
Mysql saves your AUTO_INCREMENT counter in its INFORMATION_SCHEMA table.
I don't know which version you're using but you should read the docs about it.
As you can read here, you can set the number you want using
ALTER TABLE tablename AUTO_INCREMENT = 1;
Thank you in advance for your help on this one.
Here's my issue:
Table A had spam accounts that I deleted. The rows were like:
1, 2556, 2559, 2565, 2595, etc.
Am trying to import Table A into the empty Table B in the same database.
tables have different fields.
Table B declines the import because the insert is set to auto increment.
Mysql table B does not seem to want to skip rows.
I went to Table A and updated the rows to:
1, 2, 3,4,5..., 18
Now when I try to create a user in Table A using PyphAdmin with user_id value of NULL,
the id i get is 2596. Mysql still remembers the old incrementing sequence.
How can I get Mysql table to auto-increment based on the previous row so the new
id will be 19 ?
The mysql import file is encrypted in phpshield so I dont see what's
going on and has no way to edit it. But I assume this is what is happening after I ruled out all other possibilities.
The field must be primary key - PRIMARY KEY (id)
The field must be AUTO_INCREMENT
Reset table's autoincrement value.
For example -
CREATE TABLE table1(
id INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
Set new autoincrement value -
ALTER TABLE table1 AUTO_INCREMENT = 19;
Try this
ALTER TABLE A AUTO_INCREMENT=19;
ALTER TABLE TableA
AUTO_INCREMENT=19;
I need to import data from one MySQL table into another. The old table has a different outdated structure (which isn't terribly relevant). That said, I'm appending a field to the new table called "imported_id" which saves the original id from the old table in order to prevent duplicate imports of the old records.
My question now is, how do I actually prevent duplicates? Due to the parallel rollout of the new system with the old, the import will unfortunately need to be run more than once. I can't make the "import_id" field PK/UNIQUE because it will have null values for fields that do not come from the old table, thereby throwing an error when adding new fields. Is there a way to use some type of INSERT IGNORE on the fly for an arbitrary column that doesn't natively have constraints?
The more I think about this problem, the more I think I should handle it in the initial SELECT. However, I'd be interested in quality mechanisms by which to handle this in general.
Best.
You should be able to create a unique key on the import_id column and still specify that column as nullable. It is only primary key columns that must be specified as NOT NULL.
That said, on the new table you could specify a unique key on the nullable import_id column and then handle any duplicate key errors when inserting from the old table into the new table using ON DUPLICATE KEY
Here's a basic worked example of what I'm driving at:
create table your_table
(id int unsigned primary key auto_increment,
someColumn varchar(50) not null,
import_id int null,
UNIQUE KEY `importIdUidx1` (import_id)
);
insert into your_table (someColumn,import_id) values ('someValue1',1) on duplicate key update someColumn = 'someValue1';
insert into your_table (someColumn) values ('someValue2');
insert into your_table (someColumn) values ('someValue3');;
insert into your_table (someColumn,import_id) values ('someValue4',1) on duplicate key update someColumn = 'someValue4';
where the first and last inserts represent inserts from the old table and the 2nd and 3rd represent inserts from elsewhere.
Hope this helps and good luck!
I have a table that has its primary key set to autoincrement. This table needs to be brought up to date via an import of data, but I want to assign the primary key based on the imported data not on the next incremented value. Also the data to import may not be in sequence - some ids might be missing (101,105,122 etc).
In my head I see it as
Table is at id 50 with auto increment on
Alter existing table modify primary key to straight int (no increment)
Process data.. (adding records according to imported ids 101,105,122)
Alter table when done to enable auto increment again new record should be 123
Possible?
You can insert into an AUTO_INCREMENT column just as you would insert into any other column.
INSERT INTO yourtable (id, name) VALUES (101, 'Foo')
You can set the auto-increment to a specific value manually using an ALTER statement:
ALTER TABLE yourtable AUTO_INCREMENT = 123;
There is more information about this in the manual:
To change the value of the AUTO_INCREMENT counter to be used for new rows, do this:
ALTER TABLE t2 AUTO_INCREMENT = value;
You cannot reset the counter to a value less than or equal to any that have already been used. For 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 plus one. For InnoDB, if the value is less than the current maximum value in the column, no error occurs and the current sequence value is not changed.