How to reinstate auto increment during importation of CSV file into MySql while retaining pervious numbering? -


id,created,filenumber,address,city,state,zip  1008,"02/27/2014, 10:28",142840,124 north st,anycity,ny,91111  1007,"02/12/2014, 21:06",142839,424 maple ave,anycity,ny,91111  1006,"02/12/2014, 21:06",142839,143 great pl,anycity,ny,91111 

i have "like above" records in comma separated value file format. import table mysql , while doing to:

retain existing records same id numbers make id continue increment each record added if 1 of records is deleted don't want records update number (for example, if record id 1007 deleted remaining record numbers 1006 , 1008) , last, not least "filenumber" auto increment on each new record first 2 numbers being year date (e.g. 14 stands 2014) , remaining 4 numbers incrementing each record (e.g 142841, 142842, 142843, etc). example, if record added december 31, 2014, file number should auto increment 142844 , next record addition on january 1, 2015 152845. , want retain each existing records same filenumber numbers , new records continue auto increment.

is there way import mysql , make columns id , filenumber above?

i thinking of this:

create table `records` (   `id` int not null auto_increment primary key,   `created` timestamp default now(),   `filenumber` int(6) zerofill not null auto_increment,    `address` varchar(50) not null,   `city` varchar(12) not null,   `state` varchar(2) not null,   `zip` int(10) not null);     load data local infile 'c:\\records.csv'  table records  fields terminated ',' lines terminated '\n' (id, created, filenumber, address, city, state, zip); 

some corrections:

you can't define multiple fields have auto increment.
if want keep id auto..., filenumber int ... auto... wrong.

possible options (for enabling):

  1. you better not define auto increment filenumber field id field only.
  2. for latest inserted filenumber value hold current year prefix, need trigger set 2 digit year prefix.
  3. you can use primary key value suffix year form filenumber.

and, retain former auto_increment value in new table importing data, need use alter table command.

alter table table_name    auto_increment = new_number_from_where_to_start_with; 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -