Fergal
Fergal

Reputation: 5613

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?

Upvotes: 2

Views: 8741

Answers (4)

elvenbyte
elvenbyte

Reputation: 776

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.

Upvotes: 1

Lmwangi
Lmwangi

Reputation: 2574

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.

Upvotes: 1

Mahender Reddy Yasa
Mahender Reddy Yasa

Reputation: 171

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.

Upvotes: 1

Unreason
Unreason

Reputation: 12704

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.

Upvotes: 2

Related Questions