This question already has answers here:
How to reset the auto increment number/column in a MySql table
(4 answers)
Closed 8 years ago.
I have a table named users_items. In this table there are 3 columns. 1 of them is called id. There are like 100.000 - 150.000 data in this table. id is set to AUTO_INCREMENT.
I want to reset all id's to 0 and than replace with numbers 1,2,3,4,5,6 continue like that.
You won't be able to make all of them 0 at the same time, as you can't have duplicates for the PK.
Create a copy of the table using phpMyAdmin or any tool you want or using SQL queries.
Then delete all data from the original table using:
DELETE users_items
Or:
TRUNCATE users_items
Then reset auto increment using:
ALTER TABLE users_items AUTO_INCREMENT = 1
If you used TRUNCATE then you won't have to reset the auto increment counter.
After this you can use SELECT and INSERT to get the data from the copied table back to this one:
INSERT INTO users_items (col2, col3...) SELECT col2, col3,... FROM users_items_copy
(Note: the id column was not touched while selecting and inserting rows.)
To start from one simply do the following:
ALTER TABLE tablename AUTO_INCREMENT = 1
IF need reference use following links:
altering table:
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
Auto increament/Reset primary key:
Reorder / reset auto increment primary key
Mysql reset autoincreament:
http://www.mysqltutorial.org/mysql-reset-auto-increment
Related
This question already has answers here:
How to insert new row to database with AUTO_INCREMENT column without specifying column names?
(3 answers)
Closed 2 years ago.
This is a silly little problem. I have a large table with hundreds of columns, so I don't want to write out each column name individually. The problem is that say, table1 is the source table, with 200 columns, and table2 is the destination table with 201 columns, where the last column of table2 is an extra auto-increment (primary key) column. The idea is that I simply can do
insert into table2 select * from table1 where row = ##;
and I would wish that all the data would be copied and the auto-increment column would just do its job. However I get this pesky error message:
Error Code: 1136. Column count doesn't match value count at row 1
Anyone have a simple solution to this?
My recommendation is to generate the column names with a SQL query and just cut-and-paste.
But you can also use the temporary table approach:
create table temp_table1 as
select * from table1 where row = ##;
alter table temp_table1 drop column row;
Then you can use temp_table1 with *. Of course, this assumes that all the other columns line up! I also recommend listing all the columns for the insert . . . and you are back to the recommendation at the beginning of the answer.
The simplest solution can be create a backup of the table2, drop the autoincrement column then insert whatever to you want to insert as the column number would match, and then you can add the autoincrement column at the end again.
This question already has answers here:
Reorder / reset auto increment primary key
(16 answers)
Closed 3 years ago.
I have a table in my MySQL database where the id's of the entries starts from 3000 because the previous rows were deleted. What I would like achieve is to change the value of the id columns. I would need the rows to start from 1. If I have 1900 entries, I would need the id's from 1 to 1900. The id column in my database is set to AUTO_INCREMENT and the id is primary key.
My question is that is it possible to achieve this with an SQL command? Or I should rather create a new database and transfer the objects with new id's?
As mentioned here https://stackoverflow.com/a/43137256/5031885
UPDATE table_name SET id = 0 - id;
set #counter = 0;
UPDATE table_name SET id = (#counter := #counter + 1);
This will first set all id values to negative equivalents and then will loop each row, update and increment the counter.
This solution will not work if id column is unsigned.
Also, keep in mind that this will not change the current AUTO_INCREMENT pointer.
To help with auto-increment you can use:
ALTER TABLE table_name AUTO_INCREMENT = 1;
Even if we assign 1 to AUTO_INCREMENT, it will continue with the next key as it should.
You should better drop the column and again add it as it will surely lead to inconsistancy of data as its a primary key plus if it becomes a foreign key as a reference in other table then it would create more problems even after dropping the column
You need to drop the column and then add it again with autoincrement and if it is also a foreign key then you have to drop the constraints from both the tables using CASCADE.
This question already has answers here:
Fixing gaps in mysql table row id after we delete some of them [duplicate]
(2 answers)
Closed 4 years ago.
I have a table with existing data that has a unique and auto increment key column named "_id" and several other columns and because this was a test database, I deleted some random rows and would like to reset my _id column to 1 and auto fill the existing _id rows automatically by adding +1 (same as auto increment). It will be easier if I explain using images, please see below:
Next inserted row will auto increment _id to "6".
please try following sql:
ALTER TABLE "tablename" AUTO_INCREMENT = 1
This question already has answers here:
Deleting duplicate rows from a table
(3 answers)
Closed 8 years ago.
I have a table that does not has any unique key or primary key. It has 50 columns and any or all of these columns can be duplicates. How do I delete all duplicate rows but keep the first occurrence?
The generic SQL approach is to store the data, truncate the table, and reinsert the data. The syntax varies a bit by database, but here is an example:
create table TempTable as
select distinct * from MyTable;
truncate table MyTable;
insert into MyTable
select * from TempTable;
There are other approaches that don't require a temporary table, but they are even more database-dependent.
If you are using a mysql database use the following command
ALTER IGNORE TABLE tablename ADD UNIQUE INDEX (field1,field2,field3...)
This allows duplicates to be removed through the addition of a unique index even with duplicate entries.(the IGNORE keyword is thus used)
If you are using an Oracle database use the following command
Delete from tablename where rowid not in (select min(rowid) from tablename group by row1,row2,row3.....)
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