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.
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.
First type Ctrl+a, then Ctrl+k.
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.
In mysql 5.7, this has been resolved. You can now do ctrl-c to exit the SQL command line.
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.
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
If you came here hoping to find the same answer in Transact-SQL, type RESET
Related
When I input a code or anything into MySQL and hit "enter" it moves down and "->" appears. It is as if the code is not going through or the code is not being read.
I have attempted to download "add-ons" but I am really not sure what I am doing. This is for school and I am having trouble getting in touch with the professor.
I am new to this and can't figure out what I am doing wrong. Please help!
Please see image of what it looks like to me.
Please add semicolon ; after the mysql code.
Problem 1: Be aware of the prompt. Either of these
MariaDB >
mysql >
means that you are inside the MySQL commandline tool. You can enter only SQL statements. Most SQL queries need to be terminated by a ; or \G (but not both). To exit that tool:
exit
Or, if you get stuck in certain ways
CTRL-C
exit
Each of these implies a shell script:
$
#
mymachine$
/usr/home/rj $
C:\Users\rj:
and many others
Problem 2: mysqldump is a command, not SQL. So it needs to be executed in a shell script.
Problem 3: There is yet another problem. When it suggested typing 'help;', it did not mean for you to include the quotes. Instead, type just help;.
Rookie question.
I was performing some mysql processes, and then the console displays an indented -> as in the image below, instead of the normal console prompt:
What does that mean? I can't do anything with it there.
Thank you!
Means that the sentence is not completed.
To end a sentence inside a mysql session, you have to type ;⏎.
By the way, you couldn't do a cat inside a mysql session.
Everywhere I look I can't find this "EXACT" situation.
I enter: create database october24
after hitting return I get:
->
So it's not my regular linux prompt and it's not the mysql prompt after logging in, which is mysql>
I don't get/see any errors just that crappy little prompt. I have to "ctrl-c" to get out, otherwise, nothing I enter after that prompt does squat. I haven't found anything on this. Can someone who is more versed in msyql/SQL point me in the right direction?
Thanks
You need to end each statement with a semi-colon (;).
create database october24;
That prompt is because MySQL doesn't know you were done with your query and is expecting more input.
You're missing a semi-colon. Type it, press enter and then the prompt will be back :)
EG:
mysql> select id, max(name)
-> from table
-> group by id;
What's the SQL command to undo:
USE db;
The syntax I see everywhere is:
USE [db] ;
implying that I can leave out the db part. Not so - this is a syntax error however (maybe just syntax errors in the SQL syntax syntax?).
edit
The programming problem this is causing is that I can't reset the environment in which subsequent commands run. I could reset my DB connection, but this seems efficient.
cmdX; // Works
vs.
cmdX;
cmdY; // May fail because command X upset some state.
cmdX should clean up after itself and put things back where it found them.
Analogously:
cd ./a
doX()
cd ../
doY() // Y expects to not be in a?
I don't think you can. The documentation doesn't say the parameter is optional. It says:
The database remains the default until the end of the session or another USE statement is issued:
So if you want to drop the default, end your session and start a new one without selecting a DB.
What programming problem is this causing for you?
The database argument is not optional.
mysql> use
ERROR:
USE must be followed by a database name
I'm not sure where you saw this command with square brackets around the argument. That is not shown at the documentation page: http://dev.mysql.com/doc/refman/5.6/en/use.html
Microsoft SQL Server uses square brackets around identifiers (as opposed to a style to indicate an optional argument), but the MS SQL documentation for USE also doesn't show it: http://msdn.microsoft.com/en-us/library/ms188366.aspx
What would it mean to "undo" a USE command? Would it be like cd - in bash, making the previous default database again the default? There is no such command in MySQL for this. It doesn't remember what was your previous default database. If you want to return, you just have to USE that database and name it explicitly.
I have an application that is defining some SQL code:
mySql = "SELECT
sq.question,
qs.title,
sq.id as question_id,
sq.type,
qs.id as option_id,
sri.title as rankTitle,
sri.id as rankId,
sfi.title as formTitle,
sfi.id as formId,
sq.sub_type,
sq.sort_order
FROM survey_questions as sq
LEFT JOIN question_suboptions as qs
ON sq.id = qs.question_id
LEFT JOIN survey_rankingitems as sri
ON sq.id = sri.question_id
LEFT JOIN survey_formitems as sfi
ON sq.id = sfi.question_id
WHERE sq.survey_id = #{#surveyId}
ORDER BY sq.sort_order"
I would like to paste this code (everything between the double quotes) in the MySQL command line, change the one parameter and execute it, but I have run into an issue where for every line above MySQL will display:
Display all 1450 possibilities? (y or n)
And then 1450 different available commands. If I remove all linebreaks and tabs then I can paste in, but that is time consuming and a pain. Is there a way that I can simply paste in the above code, edit it and then execute it as a single unit?
This is the default mysql (CLI) behavior each time the user presses the Tab key (mysql uses the underlying readline or EditLine libraries (not on Windows)).
By default, when the user requests to use a database, mysql reads tables and fields definitions. Then, pressing the Tab key makes mysql conveniently offers completion of the current input with the known tables and fields.
However, pasting some text into mysql that contains TAB characters (\t or 0x09) triggers the same behavior - even though no Tab key was actually pressed from the keyboard. And this can be annoying.
Two options given to mysql can prevent that behavior, though. My favorite is --disable-auto-rehash. The other one is --quiet or -q.
--disable-auto-rehash to prevent database, table, and column name completion (which are not read from the database, use the rehash command if later on you need completion). Commands history is kept, though (retrieved via the ↑ and ↓ keys for instance). Which is convenient.
--quick or -q which makes mysql not using the history file and no completion (does not read the database definitions).
On Linux one may add an alias in .bashrc to use --disable-auto-rehash automatically
alias mysql2='mysql --disable-auto-rehash'
Perhaps you could save the statement to a text file myTest.sql, then use the MySQL command source myTest.sql to run it? You could then tweak the SQL in the file, save the changes, and run it again.
You need to remove the line breaks and tabs. The double tab is causing it to display the Display all 1450 possibilities? (y or n) and the line breaks are causing it to execute early.
If it's PHP, write a little script to strip it for you:
echo (preg_replace("/\s+/", " ", $string));
Or something similar for other languages.
Breaking not so bad's answer explained the cause of this problem really well.
From the question:
If I remove all linebreaks and tabs then I can paste in, but that is time consuming and a pain.
In my case, I just replaced the tabs with spaces and I was able to paste the query just fine. The MySQL console doesn't seem to care about the newlines, just the tabs.
As a way to prevent this, most editors have a setting that will insert tabs instead of spaces when you press the Tab key. I normally have my IDEs configured this way, but in this instance it was a query I'd copied from MySQL workbench. Conveniently, it also has a setting to use spaces instead of tabs:
Edit > Preferences > General Editors > check Tab key inserts spaces instead of tabs > OK