How to run or import sql file in MySQL

In this article, we will learn how to run or import sql file in MySQL on Linux Windows.

We can import sql file in MySQL by two methods:

On Linux:

1. From Linux shell
2. By connecting to MySQL

On Windows:

1. From Command Prompt
2. By connecting to MySQL.

Import sql file in MySQL on Linux:

From Linux shell(terminal):

1. Create a sql file

cat > createdatabase.sql create database test; use test; create table emp(empno int, empname varchar(40));

press ctrl+d to save and quit this sql file.

2. Now import(run) sql file by using below command.

mysql -u root -p < createdatabase.sql How to run or import sql file in MySQL

3. Verify whether database ‘test’ and table ’emp’ created or not by connecting to MySQL server.

By connecting to MySQL

1. Create anothr sql file.

cat > createtable.sql CREATE TABLE students(SNO int PRIMARY KEY, SNAME varchar(50),DOB date, class i nt, gender varchar(5));

press ctrl+d to save and quit this sql file.

2. Now connect to MySQL from Linux.

mysql -u root -p

3. Switch to the database in which database to be imported sql file.

use test;

4. Now, import the sql file in MySQL using below command.

source /home/r2schools/createtable.sql

5. Now verify sql file imported successfully or not.

So, we have successfully imported sql file in MySQL on Linux with different methods.

Import sql file in MySQL on Windows:

1. Create sql file and paste below commands and save the file.

create database test; use test; create table emp(empno int, empname varchar(40));

2. Open command command prompt.

3. Now, import sql file in MySQL on Windows

mysql -u root -p < createdatabase.sql

By connecting to MySQL

1. Create anothr sql file with name createtable.sql

CREATE TABLE students(SNO int PRIMARY KEY, SNAME varchar(50),DOB date, class i nt, gender varchar(5));

2. Now connect to MySQL on Windows.

mysql -u root -p

3. Switch to the database in which database to be imported sql file.

use test;

4. Now, import the sql file in MySQL using below command.

source C:\Test\createtable.sql

5. Lets verify by running show tables; comamnd.

So, we have successfully imported sql file in MySQL on Windows with different methods.

Finally we have successfully imported sql file in both Linux and Windows environments.