This tutorial shows you how to use the LOAD DATA INFILE
statement to import CSV file into MySQL table.
The LOAD DATA INFILE
statement allows you to read data from a text file and import the file's data into a database table very fast.
Before importing the file, you need to prepare the following:
You can use this utility to connect to existing databases and edit table data, run SQL queries, modify scripts and manage database objects. New in MySQL Workbench 8.0.11 Build 12872684 RC. The MySQL Workbench is a useful tool with a GUI (Graphical User Interface) that is easy to use. We can recreate the tables in an entire database using the Forward Engineer option in the MySQL Workbench. In this article, we will create a table in the MySQL Workbench with step-by-step examples and screenshots. An Introduction to the MySQL Workbench.
- A database table to which the data from the file will be imported.
- A CSV file with data that matches with the number of columns of the table and the type of data in each column.
- The account, which connects to the MySQL database server, has FILE and INSERT privileges.
Suppose we have a table named discounts
with the following structure:
We use CREATE TABLE statement to create the discounts
table as follows:
The following discounts.csv
file contains the first line as column headings and other three lines of data.
The following statement imports data from the c:tmpdiscounts.csv
file into the discounts
table.
The field of the file is terminated by a comma indicated by FIELD TERMINATED BY ','
and enclosed by double quotation marks specified by ENCLOSED BY '
‘.
Each line of the CSV file is terminated by a newline character indicated by LINES TERMINATED BY 'n'
.
Because the file has the first line that contains the column headings, which should not be imported into the table, therefore we ignore it by specifying IGNORE 1 ROWS
option.
Now, we can check the discounts
table to see whether the data is imported. Activision game support phone number.
Transforming data while importing
Sometimes the format of the data does not match the target columns in the table. In simple cases, you can transform it by using the SET
clause in the LOAD DATA INFILE
statement.
Suppose the expired date column in the discount_2.csv
file is in mm/dd/yyyy
format.
When importing data into the discounts
table, we have to transform it into MySQL date format by using str_to_date() function as follows:
Importing file from client to a remote MySQL database server
It is possible to import data from client (local computer) to a remote MySQL database server using the LOAD DATA INFILE
statement.
When you use the LOCAL
option in the LOAD DATA INFILE
Boom 3d ios. , the client program reads the file on the client and sends it to the MySQL server. The file will be uploaded into the database server operating system's temporary folder e.g., C:windowstemp
on Windows or /tmp
on Linux. This folder is not configurable or determined by MySQL.
Let's take a look at the following example:
The only difference is the LOCAL
option in the statement. If you load a big CSV file, you will see that with the LOCAL
option, it will be a little bit slower to load the file because it takes time to transfer the file to the database server.
The account that connects to MySQL server doesn't need to have the FILE privilege to import the file when you use the LOCAL
option.
Importing the file from client to a remote database server using LOAD DATA LOCAL
has some security issues that you should be aware of to avoid potential security risks.
Importing CSV file using MySQL Workbench
MySQL workbench provides a tool to import data into a table. It allows you to edit data before making changes.
The following are steps that you want to import data into a table:
Open table to which the data is loaded. Bonus codes for online casinos.
Mysql Workbench View Tables
Click Import button, choose a CSV file and click Open button
Review the data, click Apply button.
MySQL workbench will display a dialog 'Apply SQL Script to Database', click Apply button to insert data into the table.
We have shown you how to import CSV into MySQL table using LOAD DATA LOCAL
and using MySQL Workbench. With these techniques, you can load data from other text file formats such as tab-delimited.
Mysql Workbench Edit Table Data
In the previous section you created a model, schema, and table. You also forward engineered your model to the live server. In this section you will see how you can use MySQL Workbench to add data into your database on the live server.
On the Home screen click the link Edit Table Data in the SQL Development area of the Workspace. This launches Edit Table Data wizard.
Figure 4.7. Getting Started Tutorial - Edit Table Data
Easy new file 2 1. In the wizard select the 'Big Iron Server' connection from the stored connection drop down listbox. Click .
Select the schema,
dvd_collection
. Select the table to edit,movies
. Click .You will see a data grid. This is where you can enter the data for your database. Remember that the
movie_id
was set to be autoincrement, so you do not need to enter values directly for this column. In the data grid enter the following movie information:title release_date Gone with the Wind 1939-04-17 The Hound of the Baskervilles 1939-03-31 The Matrix 1999-06-11 Above the Law 1988-04-08 Note: do not modify any values in the
movie_id
column.Now click the button in the toolbar located in the bottom right corner. A list of SQL statements will be displayed. Confirm that you understand the operations to be carried out. Click to apply these changes to the live server. Omnigraffle pro 7 10 upgrade.
Confirm that the script was executed correctly and then click .
View the data grid again and observe that the autoincrement values have been generated.
Figure 4.8. Getting Started Tutorial - Edit Data
Now you will check that the data really has been applied to the live server. Launch the MySQL Command Line Client. Enter
SELECT * FROM movies;
to see the data just entered.You can also carry out a similar check from within MySQL Workbench. Click on the Home screen tab.
Click the link Open Connection to start Querying in the SQL Development section of the Workspace. This will launch the Connect to Database dialog. Select 'Big Iron Server' from the drop down listbox. Click .
A new SQL Editor tab will be displayed. In the SQL Statements area enter the following code:
Now click the toolbar button - this resembles a small lightning bolt. The SQL Editor will display a new Result tab contain the result of executing the SQL statements.
Figure 4.9. Getting Started Tutorial - Results
Mysql Workbench Edit Table Data
In this section of the tutorial you have learnt how to add data to your database, and also how to execute SQL statements using MySQL Workbench.