Export/import CSV files with MySQL

Source: Internet
Author: User
ArticleDirectory
    • Exporting data as CSV file directly using simple MySQL Query
    • Importing data from CSV file directly using simple MySQL Query
    • Similar posts:
Export/import CSV files with MySQL-no external tool required

Loading data from and to CSV or other tab delimited or
Similar file format is essential in day to day operation. With MySQL it
Is easy to load data into table using files and exporting data into CSV
Files is quite easy. No need to use any external tool it can be done
Right there from the query tool/command prompt, no specific GUI tool
Required.

Let's see how?

Exporting data as CSV file directly using simple MySQL Query

Simply add into OUTFILE [file_path] In your query and you are done. Here is a simple example:

Select*From [Table]
Into OUTFILE 'X:/[file]'

Above statement uses default options for file export of result set
A query, but it is simple to override them. By default file will be Tab
Delimited file. We can always override as showed in example below:

 Select * From  [ Table  ]   Into   OUTFILE   'X:/[file]' 
Fields Terminated By ',' Optionally Enclosed By '"'
Lines TerminatedBy 'N'

Or

 Select * Into   OUTFILE   'X:/[file]' 
Fields Terminated By ',' Optionally Enclosed By '"'
Lines Terminated By 'N'
From [ Table ]

The beauty is it don't really matter where you place from table
Clause, any of the style wocould work from abve example. Don't forget
That we can use all our complex join or where conditions which we really
Use with select. 

Full Syntex:

Select 
[ All | Distinct | Distinctrow ]
[ High_priority ]
[ Straight_join ]
[ SQL _small_result ] [ SQL _big_result] [ SQL _buffer_result ]
[ SQL _cache | SQL _no_cache ] [ SQL _calc_found_rows ]
Select_expr [ , Select_expr... ]
[ From Table_references
[ Where Where_condition ]
[ Group By { Col_name | expr | position }
[ ASC | Desc ] ,... [ With Rollup ] ]
[ Having Where_condition ]
[ Order By { Col_name | expr | position }
[ ASC | Desc ] ,...]
[ Limit { [ Offset, ] Row_count | row_count offset } ]
[ Procedure procedure_name ( Argument_list ) ]
[ Into OUTFILE 'File _ name' Export_options
| Into Dumpfile 'File _ name'
| Into Var_name [ , Var_name ] ]
[ For Update | Lock In Share Mode ] ]
Importing data from CSV file directly using simple MySQL Query

Here is the simplest way to do it:

 
Load Data Infile 'X:/[file]'
Into Table [Table]

Similar to export option of MySQL it will keep CT file to be Tab
Delimited if we don't specify any option for it in it's simplest form.
So if you simple export file without any custom specification as a first
Query example it wocould simple load data from that file, but if you have
A file with custom separator than it needs to be specified.

 Load   Data   Infile   'X:/[file]' 
Into Table [ Table ]
Fields Terminated By ',' Optionally Enclosed By '"'
Lines Terminated By 'N'

Full Syntex:

 Load  Data   [  Low_priority | Concurrent ]   [  Local  ]   Infile   'File _ name' 
[ Replace | Ignore ]
Into Table Tbl_name
[ Character Set Charset_name ]
[ { Fields | Columns }
[ Terminated By 'String' ]
[ [ Optionally ] Enclosed By 'Char' ]
[ Escaped By 'Char' ]
]
[ Lines
[ Starting By 'String' ]
[ Terminated By 'String' ]
]
[ Ignore Number Lines ]
[ ( Col_name_or_user_var ,... ) ]
[ Set Col_name = expr ,... ]

So exporting and importing data from MySQL seems quite easy and we do
Not need to write any programming statement to format them just
Simple query can do it.

References: (for more reference please refer)
Http://dev.mysql.com/doc/refman/5.0/en/select.html
Http://dev.mysql.com/doc/refman/5.1/en/load-data.html

Similar posts:
    • Heidisql 5.1-Good has got better
    • Heidisql 4.0 RC1 released
    • Using views to avoid cross database connection-MySQL
    • Alternate to find_in_set for non-MySQL Databases
    • MySQL 5.5.8-problem while creating new user (SQL error (1364): field 'authentication _ string 'doesn' t have a default value)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.