Loading SQL Commands from a File

shell> mysql -u username -p db_name < file_name.sql mysql> \. file_name.sql

Most of the phrases in this book assume that you are interactively entering commands into mysql, the MySQL command-line client. You can, however, just as easily store the commands in plain text files and then use mysql (or some other client) to run the commands.

In the first example, you are running mysql in batch mode on the command line. In batch mode, mysql runs one or more commands, displays output (if any), and then exits.

More specifically, in the first example, mysql

  1. Connects to the MySQL server using the specified username

  2. Prompts for a password to attempt to authenticate with

  3. Sets the default database to be db_name

  4. Sends the contents of file_name.sql to the server

  5. Displays the results (if any) of the command

  6. Exits

In the second example, the contents of file file_name.sql are read in by the client and sent to the server. The important things to note are that you are already using the mysql command-line client in interactive mode.


The \. command is (hopefully) unrelated to Slashdot, the infamous technology news site. Instead, its name derives from the UNIX shell source command, which is used to process the text of a file as a set of shell commands. In most Unix shells, the command is invoked as shell> . some_file_name.

MySQL Phrasebook. Essential Code and Commands
MySQL Phrasebook
ISBN: 0672328399
EAN: 2147483647
Year: 2003
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net