Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 September MySQL: Import and Export CSV data with Headers

MySQL: Import and Export CSV data with Headers

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a demonstration on how to export or import CSV data using MySQL INFILE and OUTFILE.

The CSV files are most famous for the basic data migration activities.
Database developer does not need to write or create any particular database link between two servers.
They can easily import data from CSV and export that CSV data into another server and also CSV file does not have any data length limitation like XLS file.
User can store millions of records into CSV file.

This technique is preferred only for temporary purposes, and if it continues the process, the user has to write some automatic process like database server replication.

Import or Export a CSV File using PostgreSQL COPY Command

MySQL provides a different way to manage, data import and export in the CSV format.

You can also perform this exercise using MySQL workbench Import/Export wizard.

Very soon, I will prepare and share a video help for this workbench related solution.

Let me demonstrate it:

First, I created a table and import data from CSV file.
I already created a CSV file with sample data. Make sure that, your table column and CSV column would be same.
You can create this CSV file using a comma delimited save as option of Microsoft excel.

MySQL CSV

Create a sample table:

1
2
3
4
5
CREATE TABLE TestCSV
(
Rno INTEGER
,Name VARCHAR(50)
);

Now COPY data from the CSV file into this table:

1
2
3
4
5
6
LOAD DATA INFILE 'E:/dbrnd.com/TestCSV.csv'
INTO TABLE TestCSV
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

You should ignore the first row because of CSV headers.

Check your sample table:

1
SELECT *from TestCSV

MySQL CSV Table Data

You can also import particular column from CSV file to MySQL Table:

1
2
3
4
LOAD DATA LOCAL INFILE 'E:/dbrnd.com/TestCSV.csv' INTO TABLE TestCSV
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(@col1,@col2) set name=@col2;

As you can see in the above script, I used query variable and @col1 stores all Rno data, and this is working as a dummy import for column Rno.
I set values for Name column by assigning @col2, so this query only import second column data, and the first column would be NULL.

Now, script for export MySQL table data into CSV File:

1
2
3
4
5
6
7
8
SELECT
IFNULL(Rno,'N/A')
,Name
FROM TestCSV
INTO OUTFILE 'E:/dbrnd.com/TestCSV2.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

After execution of the above query, please check your newly created CSV File.
Note: This query will create a new CSV file at the specified path.

Sep 29, 2015Anvesh Patel
PostgreSQL: Script to find the unused and duplicate indexMySQL: Frequently asked Interview Questions and Answers Part 1
Anvesh Patel
Anvesh Patel

Database Engineer

September 29, 2015 MySQL, MySQL DBA ScriptAnvesh Patel, CSV File, database, database research and development, dbrnd, export, import, MySQL, MySQL Command, MySQL Database Administrator, MySQL Database Designing, MySQL Database Programming, MySQL Error, MySQL Performance Tunning, MySQL Query, MySQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....