How do you clear an unexecuted MySQL query from the command line?

MysqlCommand Line

Mysql Problem Overview


Suppose you're typing a command line query into a MySQL database and you need to cancel out and start over. From a bash shell you could just type ctrl-c and get a new prompt. In MySQL, ctrl-c would exit the client and return you to the shell.

For example, I have a long, complex SELECT statement typed in but I haven't yet hit return. I realize that I don't want to send the command but I want to have the command on-screen so I can use it as a reference. I'd like to bail out without quitting MySQL. Any ideas?

Key point: the command hasn't yet been executed.

Mysql Solutions


Solution 1 - Mysql

Type \c.

When you start up MySQL, you'll likely see this message:

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

The "buffer" that it's referring to is the command/query buffer.

Solution 2 - Mysql

First type Ctrl+a, then Ctrl+k.

Solution 3 - Mysql

Use one of the following shortcuts to delete the current line:

  • Type Ctrl + u to delete everything from the cursor back to the line start
  • Type Ctrl + k to delete everything from the cursor to the end of the line

Otherwise as already indicated type \c at the end of the current line (a shortcut for the clear command) and then press Enter.

Solution 4 - Mysql

If it's a long command I usually arrow to the beginning of the command and add gibberish, so it won't execute, but I can up-arrow and gain access without having to re-type it. Otherwise if you want to not execute it, just hit the up-arrow and it should scroll you through your command history. Find a short command, backspace, then type whatever you want.

Solution 5 - Mysql

Depends on your shell's key bindings. You could press home or what ever key sequence you use to get back to the start, prefix your query with X or what ever to make it syntactically invalid, hit enter and you're good

Solution 6 - Mysql

If you came here hoping to find the same answer in Transact-SQL, type RESET

Solution 7 - Mysql

In mysql 5.7, this has been resolved. You can now do ctrl-c to exit the SQL command line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLoganView Question on Stackoverflow
Solution 1 - MysqlruakhView Answer on Stackoverflow
Solution 2 - MysqluntillView Answer on Stackoverflow
Solution 3 - MysqlbladerzView Answer on Stackoverflow
Solution 4 - MysqlMike PurcellView Answer on Stackoverflow
Solution 5 - MysqlatxdbaView Answer on Stackoverflow
Solution 6 - MysqlPost ImpaticaView Answer on Stackoverflow
Solution 7 - MysqldkniffinView Answer on Stackoverflow