Home > Database > Mysql Tutorial > Execute SQL statements from text file on MySQL client

Execute SQL statements from text file on MySQL client

WBOY
Release: 2023-09-16 11:25:03
forward
1444 people have browsed it

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
Copy after login

Putting SQL statements into a text file

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
Copy after login

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
Copy after login

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
Copy after login

Sometimes users may want the script to show them progress information. To do this, you can use the insert statement as shown below -

SELECT &#39;<info_to_display>&#39; AS &#39; &#39;;
Copy after login

Output

<info_to_display>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template