How to reinstall a & ldquo; Auto incremento & rdquo; Column for the InnoDB engine database?

advertisements

I am using an artificial primary key for a table. The table had two columns, one is the primary key and the other one is a Dates (datatype: Date) column. When I tried to load bulk data from a file (which contained values for the second column only), the YYYY part of the dates were added to the primary key column (which was the first column in the table) and the rest of the date was truncated.

So I needed to reset the table. I tried it using the Truncate table statement, but it failed with an error because this table was referenced in the foreign key constraint of another table. So I had to do it using the delete * from table; statement. I did delete all the records, but then when I inserted the records again (using the insert into statement this time), it started incrementing the ID starting from the year after the last year in the year I had previously inserted (i.e. it did not refresh it).

NOTE:- I am using MySQL 5.5 and InnoDB engine.

MY EFFORT SO FAR:-

  • I tried ALTER TABLE table1 AUTO_INCREMENT=0; (Reference Second Answer) ---> IT DID NOT HELP.
  • I tried ALTER TABLE table1 DROP column; (Reference- answer 1) ---> Error on rename of table1

  • Deleted the table again and tried to do:

    DBCC CHECKIDENT('table1', RESEED, 0);
    
    

    (Reference) ---> Syntax error at "DDBC" - Unexpected INDENT_QUOTED (This statement is right after the delete table statement, if that matters)

  • In this article, under the section named "Auto Increment Columns for INNODB Tables" and the heading "Update 17 Feb 2009:", it says that in InnoDB truncate does reset the AUTO_INCREMENT index in versions higher than MySQL 4.1... So I want some way to truncate my table, or do something else to reset the AUTO_INCREMENT index.

QUESTION:-

Is there a way to somehow reset the auto_increment when I delete the data in my table? I need a way to fix the aforementioned DDBC CHECKINDENT error, or somehow truncate the table which has been referenced in a foreign key constraint of another table.


Follow below steps:

Step1: Truncate table after disabling foreign key constraint and then again enable-

set foreign_key_checks=0;
truncate table mytable;
set foreign_key_checks=1;

Step2: Now at the time of bulk uploading select columns in table only those are in your csv file means un-check rest one (auto id also) and make sure that colums in csv should be in same order as in your table. Also autoid columns should not in your csv file.


You can use below command to upload data.

LOAD DATA LOCAL INFILE '/root/myfile.csv' INTO TABLE mytable fields terminated by ',' enclosed by '"' lines terminated by '\n' (field2,field3,field5);

Note: If you are working in windows environment then change accordinglyl.