3

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.

Community
  • 1
  • 1
Solace
  • 8,612
  • 22
  • 95
  • 183
  • 1
    Not your question but you can insert the `(id, dates)` pairs instead of the `(dates)`. This way, you can assign id values manually. – Micha Wiedenmann Apr 23 '14 at 09:56
  • 1
    Does disabling the foreign keys allow you to run a `truncate`? (and then of course re-enable them afterwards) –  Apr 23 '14 at 10:00
  • @MichaWiedenmann Thank you for saying that, because I have a question: I have an autoincrement index on ID, and I wonder when I am inserting all the values for the ID myself, what is the point of having an auto_increment? Does Load DATA INFILE not work when it comes to auto-incrementing the column which has been indexed so? I'll be grateful if you can clarify me on that. – Solace Apr 23 '14 at 10:00
  • 1
    @Zarah `auto_increment` is mere convenience. Think of a column with the `default` option. You can leave out the value for this column and the default value is used instead. `auto_increment` columns act similar except that that an increasing value is used. (`auto_increment` is not "smart" as in searches the first free value but rather uses 1 + $theLastValueUsedByAutoIncrement (if I am not mistaken)). – Micha Wiedenmann Apr 23 '14 at 10:03
  • @MichaWiedenmann What's "$" in "1 + $theLastValueUsedByAutoIncrement"? – Solace Apr 23 '14 at 10:07
  • @Zarah the `$` was pseudo code. I wanted to say that the next auto increment value will be one plus the last used auto increment value. – Micha Wiedenmann Apr 23 '14 at 11:17

2 Answers2

4

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.

Zafar Malik
  • 6,734
  • 2
  • 19
  • 30
  • Have you checked that this does in fact reset the auto increment counter? – Micha Wiedenmann Apr 23 '14 at 11:18
  • Truncate always reset the counter..your problem is how can truncate the table..so you can do it by this. – Zafar Malik Apr 23 '14 at 11:23
  • @ZafarMalik Thank you very much! I did this and this solved the problem. I will need a bit of more information about checking and unchecking the columns for bulk load. How do you check (or select) and uncheck the columns at the time of bulk loading? Is there a command? – Solace Apr 23 '14 at 12:11
  • @MichaWiedenmann Yes, exactly this one had solved my problem. It was first suggested by a_horse_with_no_name in the comments of the question. – Solace Apr 23 '14 at 12:13
  • @Zarah if you are using any GUI tool like sqlyog then you can do it simply checking and unchecking box during uploading data..and if you want to do it through command then use command in my answer.. – Zafar Malik Apr 23 '14 at 12:25
4

You can only reset the auto increment value to 1 (not 0). Therefore, unless I am mistaken you are looking for

alter table a auto_increment = 1;

You can query the next used auto increment value using

select auto_increment from information_schema.tables where 
  table_name='a' and table_schema=schema();

(Do not forget to replace 'a' with the actual name of your table).

You can play around with a test database (it is likely that your MySQL installation already has a database called test, otherwise create it using create database test;)

use test;
create table a (id int primary key auto_increment, x int); -- auto_increment = 1
insert into a (x) values (1), (42), (43), (12);            -- auto_increment = 5
delete from a where id > 1;                                -- auto_increment = 5
alter table a auto_increment = 2;                          -- auto_increment = 2  
delete from a;
alter table a auto_increment = 1;                          -- auto_increment = 1  
Community
  • 1
  • 1
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137