In this article, we will see how to run sql script in MySQL with different methods. We run multiple sql statements which are stored in text file using following methods in MySQL.
1. From MySQL command prompt(MySQL shell).
2. From Windows command prompt.
3. From workbench
4. Linux command prompt.
5. From MySQL shell on Linux.
Syntax and Examples to run sql script in MySQL
Simple Syntax:
mysql --user="username" --database="databasename" --password="yourpassword" < "filepath"
Create a sql script file with following syntax.
use r2schools; create table bill(name varchar(30), amt int);
Save it as windowscmd.sql. But its name and extension can be anything.
1. From MySQL command prompt(MySQL shell) on Windows.
Open MySQL and connect to MySQL then run the sql script file windowscmd.sql file.
source C:\Users\karunakar\Desktop\windowscmd.sql
Now switch to the respective database and verify. use r2schools; show tables;
2.Run sql script in MySQL from Windows command prompt.
Open command prompt as administrator. By right click on Windows command prompt and select Run as Administrator.
mysql -u root -p < "C:\Users\karunakar\Desktop\windowscmd.sql"
3. Run sql script in MySQL from Workbench.
a.Open MySQL Workbench and connect to MySQL server.
b. Then, click on file and select Run SQL Scrip as show below.
How to run sql script in MySQL on Linux:
Lets create sql script and save it as create_table.sql. But its name and extension can be anything.
cat > create_table.sql
use r2schools; create table bill(name varchar(30), amt int);
1. Run sql script file in MySQL from Linux command prompt.
mysql -u root -p < create_table.sql
Now connect to the database and verify the script executed successfully or not.
2. Run sql script file from MySQL shell on Linux:
source create_table.sql
So, we have seen how to run sql script in MySQL on Windows and Linux operating systems.