The Wayback Machine - https://web.archive.org/web/20240615142958/https://www.geeksforgeeks.org/create-table-from-csv-in-mysql/
Open In App

Create Table From CSV in MySQL

Last Updated : 29 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

CSV (Comma Separated Value)  files are a type of file containing data frames that are separated by a comma (generally). These files are textual in format and aren’t confined to a specific program or standard, due to which they are widely used. It is quite common for data frames to be stored in form of CSV files, which could later be used to retrieve data easily. 

In this article, we will learn how to create a table from a CSV file in MySQL. 

A distribution of MySQL is required to be installed in the Operating System. For the purpose of Demonstration we would be using the following CSV file to create a table:

CSV file viewed in Microsoft Excel

 

This CSV file contains 4 columns:

  1. DATE
  2. TOTAL_TIME
  3. HOURS
  4. MINUTES

Creation of Skeleton Table:

STEP 1: We would be creating a table in MySQL, which contains the info about each column of the CSV file. Then we would copy the data from the CSV file to each of the columns.  

Query:

CREATE TABLE table_name ( csv_column_1 
DATATYPE, csv_column_2 DATATYPE,
 csv_column_3 DATATYPE ....);

Where the last 2 entries could be extended to the number of columns in the CSV file. i.e. csv_column_2/3/4… goes on to the number of columns in the CSV file.  In our case, there are 4 columns each of which has the following data types:

DATE            => Date
TOTAL_TIME      => INT
HOURS           => INT
MINUTES         => INT

So to create a table for the above file, the query would be:

Query:

CREATE TABLE TRANSFER ( DATE DATE, 
TOTAL_TIME INT, HOURS INT, MINUTES INT);
Structure of the table

 

This would create a table. To view the table enter the query:

Query:

SELECT * FROM TRANSFER;

 

Which displays an empty table. Since we haven’t added any data to it. 

STEP 2: 

Now to add the data from the CSV file to it, run the following:

Query:

LOAD DATA INFILE 'file.csv' 
INTO TABLE TRANSFER
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

The  LOAD DATA INFILE statement allows you to read data from a text file and import the file’s data into a database. Upon execution of the above statement we display the table again using:

Query:

SELECT * FROM TRANSFER;

To which the resulting table is:

A Table containing all the values of the CSV files

 

Therefore in this way, you could import data from a CSV file into a MySQL Database. 

Explanation:

The first line tells the compiler to load data from a file named ‘file.csv‘ into the table named TRANSFER. The second line states that the fields (different cells) are terminated by a comma, cell data could be enclosed between double quotes (to allow the usage of commas within the cell), and lines are terminated by a new line character. The third line tells the compiler to ignore the first line, as it contains the names of the columns of the table, which we already mentioned while creating the table.

Therefore the blueprint for loading the data to a table would be:

Query:

LOAD DATA INFILE '_path_to_csv_' 
INTO TABLE TABLE_NAME
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

where,

_path_to_csv_  => path to the csv file
TABLE_NAME     => Name of the table (skel table) 
in which the data is to be copied

If the excel file has different delimiters, that could also be mentioned in the second line. 


Previous Article
Next Article

Similar Reads

How To Import Data From a CSV File in MySQL?
A CSV (comma-separated values) file is a text file with commas separating information. They are most commonly found in spreadsheets and databases and are now primarily used for datasets in data science and machine learning. They assist businesses in exporting large amounts of data. However, we cannot run SQL queries on such CSV data, so we must con
9 min read
Load .CSV Data into MySQL and Combine Date and Time
Prerequisite - How To Import Timestamp From a CSV File in MySQL? A CSV (comma-separated values) file is a text file with commas separating information. They are most commonly found in spreadsheets and databases and are now primarily used for datasets in data science and machine learning. They assist businesses in exporting large amounts of data to
4 min read
Export SQL Server Data From Table to CSV File
SQL Server is a very popular relational database because of its versatility in exporting data in Excel, CSV, and JSON formats. This feature helps with the portability of data across multiple databases. Here, we will learn how to export SQL Server Data from a table to a CSV file. Tools like Azure Data Studio are very useful for exporting tables in S
3 min read
MySQL | Common MySQL Queries
MySQL server is a open-source relational database management system which is a major support for web based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. Even all social networking websites mainly Facebook, Twitter, and Google depends on MySQL dat
9 min read
PHP | MySQL ( Creating Table )
What is a table? In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect. A table has a specified number of columns, but can have any number of rows. Creating a MySQL Table Using MySQLi and PDO We have already
3 min read
MySQL | Recursive CTE (Common Table Expressions)
What is a CTE? In MySQL every query generates a temporary result or relation. In order to give a name to those temporary result set, CTE is used. A CTE is defined using WITH clause. Using WITH clause we can define more than one CTEs in a single statement. A CTE can be referenced in the other CTEs that are part of same WITH clause but those CTEs sho
5 min read
Cloning Table in MySQL
There may be a number of occasions where you need to create an exact copy of an already defined (or created) table. MySQL enables you to perform this operation. Because we may need such duplicate tables for testing over the data without having any impact over the original table and the data stored in it. Original_table - ID F_name L_name Project_id
2 min read
How to Print Out All Rows of a MySQL Table in Python?
MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a web server(here XAMPP) we use various modules in P
2 min read
How to Compute the Sum of All Rows of a Column of a MySQL Table Using Python?
MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a web server, we use various modules in Python such a
2 min read
How to Convert MySQL Table Field Type from BLOB to JSON?
In this article, we would be learning a MySQL query to convert a field of BLOB Data Type to JSON Data Type in a table. To execute this query, we would need to alter the table and subsequently the field's definition. We would first need to use the ALTER TABLE command to start making changes to the table. ALTER TABLE: ALTER TABLE is a command used to
2 min read
Article Tags :