MySQL CSV Storage Engine (original) (raw)

Summary: in this tutorial, you will learn about MySQL CSV storage engine and how to create tables that use the CSV storage engine to store data in CSV format.

Introduction to MySQL CSV storage engine

The CSV storage engine stores table data as a plain text file in comma-separated values (CSV) format.

The CSV storage engine is useful when you need to directly share the table data with other applications that use the CSV format.

It’s crucial to be aware of the limitations of the CSV storage engine, including:

To check if your MySQL Server supports CSV storage engine, you use the following query:

SELECT engine, support FROM information_schema.engines ORDER BY engine;Code language: SQL (Structured Query Language) (sql)

Output:

+--------------------+---------+ | engine | support | +--------------------+---------+ | ARCHIVE | YES | | BLACKHOLE | YES | | CSV | YES | | FEDERATED | NO | | InnoDB | DEFAULT | | MEMORY | YES | | MRG_MYISAM | YES | | MyISAM | YES | | ndbcluster | NO | | ndbinfo | NO | | PERFORMANCE_SCHEMA | YES | +--------------------+---------+ 11 rows in set (0.01 sec)Code language: JavaScript (javascript)

In the output, the engine column stores storage engine types and the support column indicates whether a storage engine is supported.

Let’s take an example of using the CSV storage engine.

1) Creating a CSV table

To create a table that uses the CSV storage engine, you set the ENGINE clause to "CSV" in the CREATE TABLE statement.

For example, the following creates a table called contacts that uses the CSV storage engine:

CREATE TABLE contacts( name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL ) ENGINE = CSV;Code language: SQL (Structured Query Language) (sql)

Note that the CSV table doesn’t support the column with the AUTO_INCREMENT attribute.

When you create a table that uses the CSV storage engine, MySQL creates two files in the database directory:

For example, when you create the contacts CSV table, MySQL creates two files:

2) Inserting data into a CSV table

The following statement inserts some rows into the contacts table:

INSERT INTO contacts (name, email) VALUES ('john', '[[email protected]](/cdn-cgi/l/email-protection)'), ('jane', '[[email protected]](/cdn-cgi/l/email-protection)');Code language: SQL (Structured Query Language) (sql)

When you insert rows into a CSV table, MySQL adds them to the corresponding .CSV file, such as contacts.CSV. In addition, it updates the metafile (contacts.CSM) with the new information.

3) Querying data from a CSV table

The following statement retrieves data from the contacts table:

SELECT name, email FROM contacts;Code language: SQL (Structured Query Language) (sql)

Output:

+------+---------------+ | name | email | +------+---------------+ | john | [[email protected]](/cdn-cgi/l/email-protection) | | jane | [[email protected]](/cdn-cgi/l/email-protection) | +------+---------------+ 2 rows in set (0.00 sec)Code language: JavaScript (javascript)

If you examine the contacts.csv file from the data directory, you’ll see the same records:

"john","[[email protected]](/cdn-cgi/l/email-protection)" "jane","[[email protected]](/cdn-cgi/l/email-protection)"Code language: JavaScript (javascript)

Summary

Was this tutorial helpful?