How to Insert Data into MySQL Database from a Text File

MySQL is a popular open-source relational database management system widely used for web applications. One of the fundamental tasks in database management is inserting data into a MySQL database. However, inserting data manually into a database is cumbersome and prone to human errors, especially when dealing with large datasets. In this tutorial, we will explore how to insert data into a MySQL database from a text file.

Step 1: Create a MySQL Database

Before you can insert data into a MySQL database, you need to create a database. You can do this using any MySQL client of your choice, such as MySQL Workbench or PhpMyAdmin. In this tutorial, we will use the command-line tool to create our database.

First, log in to the MySQL server using the command:

$ mysql -u root -p

You will be prompted to enter your MySQL root user password.

Next, create a new database using the command:

Mysql> CREATE DATABASE mydatabase;

Replace "mydatabase" with your preferred database name. You can verify that the database has been created by running the following command:

Mysql> SHOW DATABASES;

Step 2: Create a Table

Once you have created a database, the next step is to create a table to hold the data that you want to insert. In this tutorial, we will create a simple table with three columns: id, name, and age.

To create the table, run the following command:

Mysql> CREATE TABLE mytable (

Id INT NOT NULL AUTO_INCREMENT,

Name VARCHAR(30) NOT NULL,

Age INT,

PRIMARY KEY (id)

);

This command creates a table named "mytable" with three columns: id, name, and age. The id column is an auto-incrementing integer, the name column is a variable-length string, and the age column is an integer. The PRIMARY KEY constraint is applied to the id column, ensuring that each row in the table has a unique id.

Step 3: Prepare the Data File

Now that you have created a database and a table, the next step is to prepare the data file that you want to insert into the database. In this tutorial, we will use a simple text file with comma-separated values (CSV) as our data source. The file should contain one row for each record to be inserted into the database, with each field separated by a comma.

The following example shows the contents of a sample data file named "data.txt":

1,John,25

2,Jane,30

3,Bob,40

Step 4: Load Data into the Table

You can now load the data from the text file into the table using the LOAD DATA INFILE statement in MySQL. This statement allows you to import data from a text file into a MySQL table.

The syntax for the LOAD DATA INFILE statement is as follows:

LOAD DATA INFILE 'filename'

INTO TABLE tablename

FIELDS TERMINATED BY ','

LINES TERMINATED BY '\n';

Replace "filename" with the path and name of your data file, "tablename" with the name of your MySQL table, and '\n' with the appropriate end-of-line character for your operating system.

To load the data from our sample data file into the "mytable" table, run the following command:

Mysql> LOAD DATA INFILE '/path/to/data.txt'

INTO TABLE mytable

FIELDS TERMINATED BY ','

LINES TERMINATED BY '\n';

If the command executed successfully, you should see a message indicating the number of rows affected.

Step 5: Verify the Data

You can now verify that the data has been inserted into the MySQL table by running a SELECT statement. The following command selects all rows from the "mytable" table:

Mysql> SELECT * FROM mytable;

If the command executed successfully, you should see the following output:

+—-+——+—–+

| id | name | age |

+—-+——+—–+

| 1 | John | 25 |

| 2 | Jane | 30 |

| 3 | Bob | 40 |

+—-+——+—–+

In this tutorial, we have demonstrated how to insert data into a MySQL database from a text file. We first created a database and a table, then prepared a sample data file in CSV format. We then used the LOAD DATA INFILE statement to load the data from the file into the table. we verified that the data had been inserted into the table using a SELECT statement. This method is efficient and less error-prone, especially when dealing with large datasets.