Unix Technical Forum

How to update existing rows with LOAD DATA

This is a discussion on How to update existing rows with LOAD DATA within the MySQL forums, part of the Database Server Software category; --> Please, I have database with following tables CREATE TABLE `licences` ( `Id` int(11) NOT NULL auto_increment, `Licence` varchar(30) NOT ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:31 AM
Vojta
 
Posts: n/a
Default How to update existing rows with LOAD DATA

Please, I have database with following tables

CREATE TABLE `licences` (
`Id` int(11) NOT NULL auto_increment,
`Licence` varchar(30) NOT NULL,
`ValidFrom` date default NULL,
`ValidTo` date default NULL,
`Name` varchar(50) default NULL,
`State` char(1) default NULL,
`Registered` tinyint(1) default '0',
`Update` tinyint(1) default '0',
`LastConnectionDate` date default NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `Licence` (`Licence`),
KEY `Name` (`Name`)
) ENGINE=MyISAM AUTO_INCREMENT=1919 DEFAULT CHARSET=utf8;

CREATE TABLE `statistics` (
`Id` int(11) NOT NULL,
`Year` smallint(2) unsigned NOT NULL default '0',
`Month` tinyint(1) unsigned NOT NULL default '0',
`Day` tinyint(1) unsigned NOT NULL default '0',
`Flags` tinyint(1) NOT NULL default '0',
UNIQUE KEY `Id` (`Id`,`Year`,`Month`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

What I need is to load data from file into table Licences so that new
records are inserted and existing rows are updated without changing their ID
so that data from table Statistics uing foreign key are not deleted. I do
not know how to do that.

LOAD DATA INFILE ''C:/licences.txt'' [REPLACE | IGNORE] INTO TABLE licences
FIELDS TERMINATED BY ''\t'' LINES TERMINATED BY ''\r\n'' (Licence, Name,
ValidFrom, ValidTo, State, Registered, Update);

If I use REPLACE it inserts new rows but rows with the same key Licence are
replaced and gets new id which results in deleting data in table Statistics.

If I use IGNORE it does not update existing rows.

If I use neither REPLACE nor IGNORE it fails on first row with the same key
Licence.

Please, can you give an advice? Thank you! Vojta


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 09:32 AM
Peter H. Coffin
 
Posts: n/a
Default Re: How to update existing rows with LOAD DATA

On Thu, 9 Nov 2006 13:09:59 +0100, Vojta wrote:
> What I need is to load data from file into table Licences so that new
> records are inserted and existing rows are updated without changing their ID
> so that data from table Statistics uing foreign key are not deleted. I do
> not know how to do that.
>
> LOAD DATA INFILE ''C:/licences.txt'' [REPLACE | IGNORE] INTO TABLE licences
> FIELDS TERMINATED BY ''\t'' LINES TERMINATED BY ''\r\n'' (Licence, Name,
> ValidFrom, ValidTo, State, Registered, Update);
>
> If I use REPLACE it inserts new rows but rows with the same key Licence are
> replaced and gets new id which results in deleting data in table Statistics.
>
> If I use IGNORE it does not update existing rows.
>
> If I use neither REPLACE nor IGNORE it fails on first row with the same key
> Licence.
>
> Please, can you give an advice? Thank you! Vojta


Why do you HAVE TO use LOAD DATA for this, instead of a small loader
application written to handle your structure?

> CREATE TABLE `licences` (
> `Id` int(11) NOT NULL auto_increment,
> `Licence` varchar(30) NOT NULL,
> `ValidFrom` date default NULL,
> `ValidTo` date default NULL,
> `Name` varchar(50) default NULL,
> `State` char(1) default NULL,
> `Registered` tinyint(1) default '0',
> `Update` tinyint(1) default '0',
> `LastConnectionDate` date default NULL,
> PRIMARY KEY (`Id`),
> UNIQUE KEY `Licence` (`Licence`),
> KEY `Name` (`Name`)
> ) ENGINE=MyISAM AUTO_INCREMENT=1919 DEFAULT CHARSET=utf8;
>
> CREATE TABLE `statistics` (
> `Id` int(11) NOT NULL,
> `Year` smallint(2) unsigned NOT NULL default '0',
> `Month` tinyint(1) unsigned NOT NULL default '0',
> `Day` tinyint(1) unsigned NOT NULL default '0',
> `Flags` tinyint(1) NOT NULL default '0',
> UNIQUE KEY `Id` (`Id`,`Year`,`Month`)
> ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
>


You can PROBABLY make LOAD DATA work if you change the statistics table
to tie to the license value instead of ID. That's probably what you
really want to do anyway, from a design perspective.

--
25. No matter how well it would perform, I will never construct any sort of
machinery which is completely indestructible except for one small and
virtually inaccessible vulnerable spot.
--Peter Anspach's list of things to do as an Evil Overlord
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:00 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com