Home >Database >Mysql Tutorial >Execute SQL statements from text file on MySQL client
Let us understand how to execute SQL statements from text files on MySQL client. The mysql client is generally used interactively. Let’s see an example of the same -
shell> mysql db_name
You can also put SQL statements into a file and then tell mysql to read the input from that particular file . To do this, first create a text file text_file.
This text file will contain the statements that need to be executed. Let us take an example to understand this process −
shell> mysql db_name < text_file
If you place the USE db_name statement as the first statement in the text file, there is no need to specify the database name on the command line. Let us take an example to understand this -
shell> mysql < text_file
If mysql is already running, you can use the source command or \ to execute the SQL script file. Order. Let us take an example to understand this -
mysql> source file_name mysql> \. file_name
Sometimes users may want the script to show them progress information. To do this, you can use the insert statement as shown below -
SELECT '<info_to_display>' AS ' ';
<info_to_display>
You can also call mysql with the help of the --verbose option. This causes each statement to be displayed before the result it generates.
mysql ignores Unicode Byte Order Mark (BOM) characters at the beginning of input files.
Before this function, they are read and sent to the server, causing syntax errors.
Now the BOM will not cause mysql to change its default character set.
To do this, you can call mysql with options such as --default-character-set=utf8.
The above is the detailed content of Execute SQL statements from text file on MySQL client. For more information, please follow other related articles on the PHP Chinese website!