How do I set the default schema for a user in MySQL
2016-08-05 14:22
549 查看
http://stackoverflow.com/questions/12426320/how-do-i-set-the-default-schema-for-a-user-in-mysql
up vote16down votefavorite 2 | Is there a way to set a default schema for each user in MySQL and if so how? Where user x would default to schema y and user z would default to schema a. mysql user default database-schema
| ||||||||||||
|
2 Answers
activeoldestvotesup vote27down voteaccepted | There is no default database for user. There is default database for current session. You can get it using DATABASE() function - SELECT DATABASE(); And you can set it using USE statement - USE database1; You should set it manually - USE db_name, or in the connection string. |
http://stackoverflow.com/questions/1820971/set-current-database-in-mysql-script
6down votefavorite 1 | I have a script file for MySQL that needs to be run on about 30 databases. Here's a snippet:ALTER TABLE <whatever_database>.`tblusers` ADD COLUMN `availability` ... ALTER TABLE <whatever_database>.`tblusers` ADD COLUMN `reliability` INTEGER ... There are many more lines in this script and I'd like to automate it in a loop of current databases to be updated. I've got the list and the loop down using cursors, but here's where I'm running into trouble: When I'm on a particular database in the cursor, that database name is in a variable. I can't just say ALTER TABLE curschema. tblusersbecause the script complains that there is no database named curschema (the name of the variable containing the database name that I'd like to run operations on). I've been able to work around this by creating and executing a statement using parameters: SET @curschema = curschema; SET @query = NULL; SET @email = emailAddress; SET @pass = pass; SET @statement = CONCAT('SELECT userid INTO @query FROM ', @curschema, '.tbluser PREPARE stmt FROM @statement; EXECUTE stmt; The problem is, setting up executable strings (like above) will become an extremely tedious task with the dozens of lines of code that I have to run. I was hoping that there was a way that I could set the current database for operations and then just reset that database each loop pass so my generic statements might be run: (start of my loop) SET DATABASE database0 -- (through to database29) -- run statements with database0 .... 29 being implied with the above command ALTER TABLE `tblusers` ADD COLUMN `availability` TINYINT(1) UNSIGNED ... ALTER TABLE `tblusers` ADD COLUMN `reliability` INTEGER UNSIGNED NOT ... (end of my loop) Does such a command exist to set the current database on which operations may be performed? If no such command exists, is there an easier way to accomplish my task? I figure that at this juncture it's just easier to do a find/replace on all the database names 30 times than it is to rewrite my entire script file to be executable/parameter-ized strings. Thanks! mysql database
| |||
add a comment |
1 Answer
activeoldestvotesup vote16down voteaccepted | Did you try USE? USE db_name The USE db_name statement tells MySQL to use the db_name database as the default (current) database for subsequent statements. The database remains the default until the end of the session or another USE statement is issued. Example: USE db1; SELECT COUNT(*) FROM mytable; # selects from db1.mytable USE db2; SELECT COUNT(*) FROM mytable; # selects from db2.mytable |
相关文章推荐
- How to set bmp for your UserControl in the toolbox
- java.lang.RuntimeException: Date pattern must be set for column update_time in the schema of component
- How do I set the "default button"
- How do I set the timeout for a JAX-WS webservice client? if you want to set timeouts for specific se
- How to fix the issue that GEM_HOME and/or GEM_PATH not set issue for rvm in mac version 10.12
- 设置页面的默认焦点控件和默认接受事件控件How Do I Set the DefaultFocus or DefaultButton in a Page Based on a Master Page in ASP.NET 2.0
- How to add a user and set environments for the us
- How do I change the default runlevel in Feodra 15?
- How to set the DefaultButton in a Page Based on ASP.NET Master Page
- How to set the current user on the model instance in the admin:
- How to set up OpenERP for various timezone kindly follow the following steps to select timezone in OpenERP
- How do I see what character set a database / table / column is in MySQL?
- How to set default values for IBInspectable in Objective-C?
- How to set a MySQL root user password in MAC OSX
- How to add a user and set environments for the user?
- How to set the default input focus on a field in an HTML web form
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.
- How do I add capabilities to my executable in qt for symbian?
- In Java, what is the default location for newly created files?
- How to change the default browser to debug with in Visual Studio 2008?