views:

21

answers:

1

Hi

I want to upload data through CSV files in my contact management application. The CSV file structure is like this:

Name, Phone, City
John Doe, 555-555-5555, New York

While the table structure in the db is like this:

name, phone, city_id

In the database, the city name is stored in another table and the foreign key is referenced in the contacts table.

My question is how can I replace the city names in the CSV file with city id for insertion into db.

Background info: Language is PHP and database is MySQL

Thanks

+2  A: 

Load firstly into temporary table with structure (Name, Phone, City), and after that make proper insert:

Select name, phone, city_id
from temptable t, cities c
WHERE t.city = c.CityName
Michael Pakhantsov
+1 For using a temporary table to do the import. You might have to do some basic consistency checks on it though before inserting the data into the actual table (ie. check if every city in the CSV already exists in your city table).
wimvds
I think this method would be too expensive (in terms of efficiency) if I have too many records in the csv file along with more than 2 foreign key columns.
Gaurav Sharma