Scripting in the MySQL Command line environment

In Oracle, I’m used to the ed command to create scripts and the @ operator to run them. You can work in a similar way with MySQL:

To create a script from within the MySQL environment use:

\! vi test.sql

.. there’s a space after the exclamation mark and this assumes that you like the vi editor. Essentially it opens up vi so you can create the test.sql file. If you store all of your scripts in a particular directory (e.g. /home/dave/sql) then it may help if you start mysql in that location.

\! Essentially lets you enter shell commands so if you use HOST from within oracle SQL Plus, you can get the same effect in MySQL by keying:

\! sh

Once you’ve keyed in your script, you can run it using the following syntax:

\. test.sql

..again, there’s a space after the full stop and you need to remember the .sql suffix when running and editing the script.

The script can contain multiple statements, for example the following works fine as the contents of the test.sql file.

use mydb ;
select * from cust ;
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.