Canceling a Partially Entered Statement

Problem

You start to enter a statement, and then decide not to issue it after all.

Solution

Cancel the statement using your line-kill character or the \c sequence.

Discussion

If you change your mind about issuing a statement that you’re entering, cancel it. If the statement is on a single line, backspace over it or use your line-kill character to erase the entire line. (The particular line-kill character to use depends on your terminal setup; for me, and most Unix/Linux users, the character is Ctrl-U; Windows users should press Esc.)

If you’ve entered a statement over multiple lines, the line-kill character erases only the last line. To cancel the statement completely, enter \c, and press Enter. This returns you to the mysql> prompt:

mysql>SELECT *
    -> FROM limbs
    -> ORDER BY\c
mysql>

Sometimes \c appears to do nothing (that is, the mysql> prompt does not reappear), which leads to the sense that you’re trapped in a statement and can’t escape. If \c is ineffective, the cause usually is that you began typing a quoted string and haven’t yet entered the matching end quote that terminates the string. Let mysql’s prompt help you figure out what to do here:

  • If the prompt has changed from mysql> to ">, That means mysql is looking for a terminating double quote. If the prompt is '> or `> instead, mysql is looking for a terminating single quote or backtick. Type the appropriate matching quote to end the string, and then type \c followed by Enter.

  • If the prompt is /*>, you’re in the middle of typing a C-style /* ... */ comment. End the comment by typing */, and then type \c followed by Enter.

Get MySQL Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.