Executing SQL Statements from a Text File
The mysql client typically is used interactively, like this:
shell> mysql db_name
However, it is also possible to put your SQL statements in a file and then tell mysql to read its input from that file.
To do so, create a text file text_file that contains the statements you wish to execute. Then invoke mysql as shown here:
shell> mysql db_name < text_file
If you place a USE db_name statement as the first statement in the file, it is unnecessary to specify the database name on the command line:
shell> mysql < text_file
If you are already running mysql, you can execute an SQL script file using the source command or \. command:
mysql> source file_name
mysql> \. file_name
Sometimes you may want your script to display progress information to the user. For this you can insert statements like this:
SELECT ‘<info_to_display>’ AS ‘ ‘;
The statement shown outputs <info_to_display>.
You can also invoke mysql with the –verbose option, which causes each statement to be displayed before the result that it produces.
you can also run this as follow with user name, password, database name like this:
mysql --user="username" --database="databasename" --password="yourpassword" < "filepath"
You can also simply login to mysql shell and the sql file as follows:
- Access mysql using:
mysql -u <your_user> - p
- At the mysql prompt, enter:
For any query or issue, feel free to discuss on http://discuss.eduguru.in