How to change the default database in MySQL
Dear Lazyweb, quick question: once I’ve USE-ed a database in MySQL, how can I un-USE it so I have no default database? Is it even possible?
Stay curious!
Dear Lazyweb, quick question: once I’ve USE-ed a database in MySQL, how can I un-USE it so I have no default database? Is it even possible?
The only way I know of without reconnecting is to create a new database, USE it, then drop it. Now you have no default database.
Brian Papantonio
11 Oct 08 at 11:12 pm
I’ve just looked around digitally and in DuBois and I don’t think you can.
Don McArthur
11 Oct 08 at 11:28 pm
[...] http://www.xaprb.com/blog/2008/10/11/how-to-change-the-default-database-in-mysql/ asks Hoosgot, [...]
How to change the default database in MySQL …
11 Oct 08 at 11:37 pm
Ya creating, selecting and then dropping a db is one way, and reconnecting (or change user through the client API) is another.
That might be it.
So, what do you want to do that requires this?
Arjen Lentz
11 Oct 08 at 11:49 pm
Ah, it’s just a thing — sometimes I want to test things out without a default DB. For example, to make sure all table names in the query are fully qualified so there’s no ambiguity. Or if I’m working on some bug or other, or reproducing behavior on a client’s server. And I’m lazy, and I don’t want to disconnect and reconnect.
Xaprb
12 Oct 08 at 12:34 am
MySQL test suite sometimes uses approach described by Brian. In other cases it uses special option for mysqltest’s “connect” command (indeed, it is probably not an approach that can be used in your case).
Dmitry Lenev
12 Oct 08 at 2:10 am
I just tested select_db() with an empty DB name, which issues a COM_DB_INIT at the protocol layer, and it doesn’t work. It sends the “correct” packet, with a 0 length DB, but you get back an error: “No database selected”. I suppose a bug/feature request could be filed to change the behavior of this request. :)
Eric Day
12 Oct 08 at 2:54 am
\r
Mikael Fridh
12 Oct 08 at 4:42 am
>> all table names in the query are fully qualified so there’s no ambiguity
If that is all you are looking for, then “explain extended” and then show warnings will let you know the ‘database.table’ used by MySQL
Shantanu Oak
12 Oct 08 at 6:43 am
Shantanu, that’s a trick I also use sometimes to help me figure out which table a column comes from. Developers think it’s obvious, but when you’re a consultant it’s tough to optimize queries when you don’t know the schema by heart!
Xaprb
12 Oct 08 at 8:43 am