MySQL Command Line Tips


Working with MySQL’s command line shell is sometimes an exercise in patience. Here are some tips to make your life easier:

Display results vertically to avoid line-wrapping

If you do a select * from a table with a lot of columns, you will often end up with the results being wrapped across your terminal, making them just about impossible to read. To display the results vertically (i.e. <column>: <value>), end your query with \G instead of ;

select * from users \G

Clear the screen

If you type help it says that \c is the clear command - this is actually something else (I will cover it later). To clear your screen of output and put a fresh mysql> at the top, type: \! clear (or on Windows, \! cls).

Cancel the current command and clear the input

How often do you type select * fom uesrs or similar? If you are used to Linux, your instinctive response is to press Ctrl-C - unfortunately in older versions of MySQL this will quit the shell and dump you back to bash (or equivalent). Try this instead:

slect * fom typo\c

The input will be cleared and MySQL won’t display any errors or warnings.

Edit your last query

Often you will select * when you don’t need to, and if it’s a multi-line query that you have pasted into the shell, it is laborious to fix. Try \e;.

This will open your last run query in whatever text editor you have defined in $EDITOR (vim/nano, etc.). You can then edit your query and then save and exit, and it will execute with the changes.

Share this post
  • Digg
  • StumbleUpon
  • Reddit
  • del.icio.us
  • Facebook
  • muti
  • Mixx
  • Google
  • laaik.it

Tags:

This entry was posted on Wednesday, May 13th, 2009 at 10:58 am and is filed under MySQL. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “MySQL Command Line Tips”

  1. Ross Ylitalo said this on

    Thank you kindly for your command line tips.

    When I enter \! cls at mysql prompt (I’m using windows), I get:

    Uknown Command ‘\!’

    Is there another way to access system commands when using windows?

    Thank you kindly,

    Ross Ylitalo

  2. Melanie said this on

    Thanks for the vertical display of results. I didn’t know that. It definitely makes my results look prettier :-)

Leave a Reply