Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo sql USE?

Tags:

sql

mysql

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?
like image 519
user48956 Avatar asked May 15 '26 05:05

user48956


2 Answers

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?

like image 196
Barmar Avatar answered May 18 '26 05:05

Barmar


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.

like image 28
Bill Karwin Avatar answered May 18 '26 03:05

Bill Karwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!