http://stackoverflow.com/questions/12426320/how-do-i-set-the-default-schema-for-a-user-in-mysql

 

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.

asked Sep 14 '12 at 14:13
 
1  
Are user x and z MySQL users, or users of some other system (e.g. your OS)? Do you only need to specify a default database when using one type of client (e.g. mysql CLI, or PHP Data Objects), or do you need it to be for all clients? Do you need/want to override any default schema specified by the client on connecting? Do you want to disable changing of the default schema after one has been selected? – eggyalSep 14 '12 at 14:40 
    
MySQL does not support schemas. Do you mean "database" instead? – a_horse_with_no_name Sep 14 '12 at 15:22
1  
Oracle MySQL refers to databases as schemas in their tools. So where Microsoft has schemas and databases, MySQL just has schemas, but we call them databases. – Robert Louis Murphy Sep 14 '12 at 15:40

2 Answers

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

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.tblusers because 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!

asked Nov 30 '09 at 16:49
afilbert

45115
 

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 do I set the default schema for a user in MySQL的更多相关文章

  1. MySQL新特性文档型数据库

    mongodb在文档型数据库这方面一直做的很好,也发展了很多年,MySQL作为一个比较大众的数据库也慢慢支持了该特性,下面介绍一下MySQL支持文档型数据库的简单操作. 环境: 主机名 IP 系统 软 ...

  2. Security3: Schema 和 Permission

    Schema是Object的容器,授予对Schema访问的权限,就是授予对Schema下所有object的访问权限. 一,Schema 是object的container The new schema ...

  3. 数据库中Schema和Database有什么区别

    在MySQL中创建一个Schema好像就跟创建一个Database是一样的效果,在SQL Server和Orcal数据库中好像又不一样. 目前我只能理解,在mysql中 schema<==> ...

  4. 数据库中User和Schema的关系

    如果我们想了解数据库中的User和Schema到底什么关系,那么让我们首先来了解一下数据库中User和Schema到底是什么概念.        在SQL Server2000中,由于架构的原因,Us ...

  5. Online Schema Upgrade in MySQL Galera Cluster using TOI Method

    http://severalnines.com/blog/online-schema-upgrade-mysql-galera-cluster-using-toi-method     As a fo ...

  6. [转]Sql Server 2005中的架构(Schema)、用户(User)、登录(Login)和角色(Role)

    每一个概念的产生必然是因为碰到了无法解决的问题.换句话说,如果没有它,必然会导致某些问题难以解决.所以我想从这个角度切入,希望能把这几个复杂而暧昧的多角关系从最实用的角度来阐述清楚. 在问题的最初,我 ...

  7. SQL Server中临时表是在什么schema下的(转载)

    Specifying schema for temporary tables 问: I'm used to seeing temporary tables created with just the ...

  8. 数据库六大约束用法:主键(primary key)、外键(foreign key)、非空(not null)、默认(default)、检查(check)、唯一(unique)

    1. 数据库有六大约束 主键(primary key) 外键(foreign key):被参照的键必须有唯一约束或是主键 非空(not null) 默认(default) 检查(check):orac ...

  9. mysql中的schema 等价于database,相当于一个数据库

    MySQL 中 Schema 等价于 数据库. mysql> SELECT -> SCHEMA_NAME, -> DEFAULT_CHARACTER_SET_NAME, -> ...

随机推荐

  1. MySQL 系列(四)主从复制、备份恢复方案生产环境实战

    第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 第三篇:MySQL 系列(三)你不知道的 视图.触发器.存储过程.函数 ...

  2. &lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;ff()&quot; &gt;&lt;/a&gt; 用法解析

    javascript:void(0) 仅仅表示一个死链接 如果是个# javascript:void(#),就会出现跳到顶部的情况,搜集了一下解决方法 1:<a href="####& ...

  3. C#多线程问题整合

    一.跨进程访问组件 错误:线程间操作无效: 从不是创建控件“XXX”的线程访问它 解决方法: 1:把CheckForIllegalCrossThreadCalls设置为false 这个方法只是不去捕获 ...

  4. extjs 6.2 helloworld

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 那些年我遇到的ERP顾问

    当我写下这篇随笔的时候,算起来在我从业9年的时间里,也差不多遇到了4-5拨的ERP咨询顾问,严格来说是4家ERP顾问公司.分别是:IBM.汉得.鼎捷以及盈通金服.从实施水准.技术力量.沟通技巧.做事态 ...

  6. Extjs整合CKEditor富文本编辑器插件

    CKEditor插件官方下载地址: http://ckeditor.com/download/releases 我使用的版本是 ExtJS5.1.0  CKEditor4.4.8 参考文章: http ...

  7. SpringMVC上传图片并压缩及剪切demo

    /** * */ package com.up.controller; import java.awt.Image; import java.awt.image.BufferedImage; impo ...

  8. java 判断语句和循环语句

    一.判断语句:if和switch if(关系表达式1) { 语句体1; }else if (关系表达式2) { 语句体2; }…else { 语句体n+; } switch(表达式) { case 值 ...

  9. React 中无用但可以装逼的知识

    最近看了Dan Abramov的一些博客,学到了一些React的一些有趣的知识.决定结合自己的理解总结下.这些内容可能对你实际开发并没有什么帮助,不过这可以让你了解到更多React底层实现的内容以及为 ...

  10. (4)Jquery1.8.3快速入门_基本选择器

    一.Jquery选择器: 基本选择器: 1.id                           #id      根据元素的id获取的唯一元素. 2.class                  ...