Importing CSV Files to SQL Databases

CSV is a data exchange format that most software can export to, since it is a simple list of values separated by commas (hence the name). Spreadsheet programs like OpenOffice Calc, Microsoft Excel, Google Docs Spreadsheet and others commonly have an ‘Export to CSV’ option, often found under the File menu.

There are scripts that convert CSV to SQL, but most modern database systems support reading CSV data such as:

As they all use different syntax to import CSV data, examples are provided.

Laying Out the Data

This reference relies on a few assumptions. Firstly, it assumes you have obtained your data in CSV format. Secondly, good sense assumes you would not be doing this if you had a better way. Important metadata is lost by using CSV as an intermediate format. Details like Foreign Keys, column constraints, and so on.

For the purposes of this guide, we will be using the following dataset.

"id","firstName","lastName"
12345,"Ingrid","Insert"
23456,"Eric","Example"
12543,"Dave","Dataset"

All examples that follow use this CSV set.

Creating the Table

A simple example of a table we are importing the CSV file to.

CREATE TABLE employees (
id INT NOT NULL PRIMARY KEY,
firstName VARCHAR(32) NOT NULL,
lastName VARCHAR(48) NOT NULL
);

Importing the Data

This process isn’t terribly complicated, but it does vary according to the database system you are importing to.

MySQL

LOAD DATA INFILE '/path/to/employees.csv' INTO TABLE employees
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Microsoft SQL Server and Sybase ASE

BULK INSERT employees FROM ‘c:\path\to\employees.csv’ WITH (
FIELDTERMINATOR = ‘,’,
ROWTERMINATOR = ‘\n’
);
GO

PostgreSQL

COPY employees FROM '/path/to/employees.csv' DELIMITERS ',' CSV;

Oracle

Offhand I don’t know a way to import a CSV file into Oracle using only SQL or PL/SQL, but there’s a great article on Dev Shed that covers how to do it with the Oracle 10G XE administration interface. Here is a convenient link to the good part, since if you’re reading this, you have likely already exported the CSV data, and want to know how to import that CSV to SQL.

Alternatively, you could convert your CSV file to SQL INSERT statements, and then execute those INSERTS. A simple CSV to SQL Conversion Tool can do this effectively.

If anyone knows how to import CSV files into Oracle without using the admin GUI or an external tool, please share!

References

Finished reading? Try one of the following:

  • Tell me what you think in the comments!
  • Get the feed, and keep tabs on new content.
  • Write a response on your blog, and post a link in the comments.
Readability - (FRE: 59.6 | FKI: 8.6 | GFI: 10.5)
3 Responses to Importing CSV Files to SQL Databases
  1. Matt Maclellan
    January 9, 2012 | 12:24

    There’s no “easy” way to insert from CSV to Oracle. The cleanest way that I’ve come across to insert data into Oracle from a CSV is with the use of python. The benefit is that it can be subsequently scheduled and if done correctly is reusable.

  2. Estevan Rau
    January 29, 2012 | 13:26

    Muchos Gracias for your blog.Really thank you! Great.

  3. Jed Sharp
    February 9, 2012 | 03:02

    how to do it in python? can you show a link or example? Thank you very much…. I use Oracle 10g… I’m a beginner in python.

Leave a Reply

Wanting to leave an <em>phasis on your comment?

Trackback URL http://chris.olstrom.com/howto/import-csv-sql/trackback/
More in Guides (2 of 10 articles)