run sql script in mysql

How to Run SQL Script in MySQL

SQL files typically contain one or more SQL queries that can be executed to create, modify or delete data on a MySQL server. Even when you use MySQLdump to backup your database it results in a .sql file. Often database developers may need to SQL script in MySQL. In this article, we will learn how to run SQL script in MySQL.

How to Run SQL Script in MySQL

There are several ways to run SQL script in MySQL.

1. Using MySQL Console

First method is via MySQL console. Open terminal

$ mysql -u <username> -p <database_name>

Once you have logged into MySQL, run the source command followed by full file path to your .sql file.

mysql> source /home/ubuntu/data.sql

For windows systems, use backslash ‘\’ instead of front slash in your SQL file’s path.

Another shortcut is to combine both the above commands in the terminal or command prompt itself.

$ mysql -u <username> -p database < /home/ubuntu/data.sql

Please note, if the database has not been created, you need to first create an empty database using CREATE DATABASE statement and then run the above commands.

2. Using MySQL tools

If you have installed PHPMyAdmin or any other MySQL GUI tool and it has access to your database, you can open this file in that application and run it to execute its queries.

In this article, we have learnt a couple of simple ways to run SQL script in MySQL.

Also read:

How to Find Number of Columns in Table in MySQL
How to Search For Text in Every Field of Database in MySQL
How to Remove Leading & Trailing Whitespace in MySQL
How to Remove Primary Key in MySQL
How to Find Most Frequent Column Value in MySQL

Leave a Reply

Your email address will not be published. Required fields are marked *