MySQL: ceasing to use a Scheme (Database)? - mysql

How do I stop using a database?
To start mysql, you can use:
mysql -u root -pXXXX<ENTER>
At this time, no database is selected. We'll call this
state 1
To select (or use) a database:
use "MyDB";
.....My operations or queries
Now, I want to return to state 1 (without any database selected). How I can do that? I can select another database, but I don't want to do that.

What you are asking for is not possible. The only way to return to that state is to disconnect and then reconnect.
If you are just looking to switch away from your current db, you can switch to a system database, such as the internal "mysql" database:
use mysql
Or you could create an empty database and use that:
create database empty;
use empty

Try prompt.
From the MySQL manual:
Reconfigure the mysql prompt to the given string. The special
character sequences that can be used in the prompt are described later
in this section.
If you specify the prompt command with no argument, mysql resets the prompt to the default of mysql>.

Ike Walker's answer is on the right track me thinks.
Create a swap space, or you could just halt the server (stop the process) and restart it I suppose. That defeats the purpose of the server—but it would for sure wind up where you want it with no database in 'use'.
I'm certain you know this, but I mention here just in case. You never know. Someone might not know it is possible to do this.

Related

How do you deselect MySQL database?

USE MyDatabase;
How do I UNUSE MyDatabase or DESELECT MyDatabase so I can use SHOW DATABASES again? What is the opposite of USE?
There is no UNUSE. You just select another database and USE it.
Bruce Daniels presented an answer at https://web.archive.org/web/20160130220207/https://dev.mysql.com/doc/refman/5.5/en/use.html.
It goes like:
CREATE DATABASE foofoofooweird;
USE foofoofooweird;
DROP DATABASE foofoofooweird;
Et voila, no database is in use.
To be clear, the hack is to CREATE, USE, then DROP (delete) a temporary placeholder database.
It is NOT advocating dropping (deleting) any database you actually use!
All credits to Bruce Daniels!
MySQL cmd can't make "unuse" function, if you want to change between databases, just use the USE DATABASENAME; command.
As said in other answers, you simply can switch to another database, but if you just want to use none of the databases you can quit from mysql session and start it again.
\q
mysql -u username -p password
I don't see clear reason to do this, but it answers the question.
There is no command UNUSE as USE db_name,
but instead you can use the same USE statement but with the replacing db name.
Suppose i am now working in world db and want to chanage to employee
intially i have entered USE world now i will simply enter use employees

How can I edit a view in MySQL Workbench without it auto prefixing a database name to the tables/views used

When I create a view, I create it in the context of the default database. So none of my references to table have a prefix which explicitly specify a database. However, when I edit a view in Workbench it automatically adds the database prefix!
I don't want the database prefix because when I restore a database under a different name it causes the restore to fail.
Is this possible to stop the prefixing in a view edit or there another way to get round the restore issue?
see https://bugs.mysql.com/bug.php?id=85176
The mysql 8.0.3 or above has been fixed
That's not possible. Views are stored in specific databases, not in some space "above" all databases. Consider following...
use playground_a; /*or whatever database*/
create view view_whatever as
select * from table_whatever;
use playground_b;
select * from view_whatever; /*here you will get an error that view_whatever does not exist*/
select * from playground_a.view_whatever; /*this works*/
That's why there will always be database prefixes in the view definition.
The only possibility I see, would be to use a stored procedure with a database name as parameter. In the procedure you'd use a prepared statement to execute a concated string of your query and the database name parameter. Of course this comes with downsides, like i.e. you can't add a where clause easily.
Creating the view without explicitely specifying a schema is a convenience feature. Behind the scenes the view is still saved in a specific schema (the default one in this case). When editing the source code is retrieved from the server which returns the real code (including the schema qualification). Hence already when you send the view code the association happens and cannot be removed again later.
Here is the command I use to create the backup:
mysqldump -u xxxxxx -pxxxxxx --routines database_a | gzip -9 > $FULLGZIPPATH
If you aren't easily able to update to MySQL 8.X then a workaround I've implemented was a post-processing step performed on the dump file itself prior to importing. I just remove the explicit prefixed db name, since the import process / view creation doesn't need it.
PowerShell -Command ^
"filter replace-dbname { $_ -replace '`<DB_NAME>`.`', '`' }"^
"Get-Content dump.sql -ReadCount 10 | replace-dbname | Add-Content replaced_dump.sql"
I've used PowerShell since I'm on Windows, but any scripting language will do. The only notes are that:
You'll need to do the replacement a-few-lines-at-a-time if you can't afford to read the entire dump into memory. Our dumps are about 11GB, which'd be a strain on our testing server's resources.
In my script I'm not doing an in-place string replacement, so it'll create a new dump file replaced_dump.sql alongside the original dump.sql. For me this was useful for diagnostics, because it meant if there was an issue, I didn't have to dump it again. Again, depending on your dump/disk size this might be an issue.
If your database happens to have `<DB_NAME>`.` as content in something like a text-field, this basic approach will also remove the string there as well.

mysql --ignore-table - concise multiple

Is there a concise way to ignore multiple tables using mysql --ignore-table?
The documentation says that --ignore-table must be used for each table.
I have to ignore around 20 tables so the command is going to be huge
This must be run via the command line
Go the whitelist route instead of the blacklist route. From the man page:
mysqldump [options] [db_name [tbl_name ...]]
You still have to type everything out, but at least you don't have to continuously type the --ignore-table flag.
Try using MySQL Workbench to build the command. It'll give the option to specify which tables you want via checkboxes.
If you need to, you can copy and paste the command from the Export Progress tab.

MySQL - Aliasing the database name?

I have a systematic problem that I don't have the time or budget to fix and I was hoping that there was a mysql hack I could deploy which would save my little world from impending doom.
The creators of the system I now administrate didn't realise that a database name could contain characters other than letters, numbers and underscores. As a result queries look like this.
SELECT * FROM {$db}.settings
If the database name contains something non-standard... like a dash. The world ends and all queries break.
The db prefix is there because this system used-to-but-doesn't-now run a multi-site feature.
You would think that I could fix this with..
$db = "`{$db}`";
If only.. you see it turns out a load of javascript functionality sends around this database name to keep track of which site to update. By adding these quotes, you break all of that as instead of getting databasename, it is getting
`databasename`
So... why don't I just set the database name to something that is compatible.. this system ships to external servers where I have no control over the database name...
The pain...
Is there a way to alias the database name in a preliminary query which will then affect all subsequent queries. Basically set a constant?
Something like that would then work on all subsequent queries. Something like:
set `$db` AS database
I can just set the $db variable to database and it should run.. if this is possible?
If you operate always on one database ($db doesn't change after you connect to the database), then just after connecting issue
USE `$db`;
and remove {$db}. prefix from all queries. USE query sets the active (default) database for future queries in current connection.
You didn't tell what programming language you are using, so I will assume PHP. Usually you can set default database while establishing connection (mysqli_connect(), $dbname parameter). Also, there may be a function like mysqli_select_db() that you can call after you connect.
I would fix the queries using something like:
sed -e "s/{\$db}/\`{\$db}\`/g" -i * */* */*/* */*/*/*
with a big warning to test this first, because this may mess up a lot. Especially check if your sed's -i option needs a suffix or not.

Default database for MySQL

Is there a way to allocate a default database to a specific user in MySQL so they don't need to specify the database name while making a query?
I think you need to revisit some concepts - as Lmwangi points out if you are connecting with mysql client then my.cnf can set it.
However, your use of the word query suggests that you are talking about connecting from some programming environment - in this case you will always need a connection object. To create connection object and in this case having default database to connect to will lead to no improvement (in terms of speed or simplicity). Efficiently managing your connection(s) might be interesting for you - but for this you should let us know exactly what is your environment.
If you use a database schema you don't need to specify the database name every time, but you need to select the database name.
The best thing to do would be to use a MySQL trigger on the connection. However, MySQL only accepts triggers for updates, deletes and inserts. A quick Google search yielded an interesting stored procedure alternative. Please
see MySQL Logon trigger.
When you assign the permissions to every user group, you can also specify, at the same file, several things for that group, for example the database that users group need to use.
You can do this with a specification file, depending on the language you are working with, as a simple variable. Later, you only have to look for that variable to know which database you need to work with. But, I repeat, it depends on the language. The specification file can be an XML, phpspecs file, or anything like this.