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. schema与数据类型优化-高性能mysql

    总结作为开发人员重点注意的内容!这是一篇有关高性能MYSQL第四章schema相关的笔记. 0.前言 在项目中,数据库表列有两个text字段,用来存储大文本,在数据规模达到40万后,如果查询没命中索引 ...

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

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

  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. EF core (code first) 通过自动迁移实现多租户数据分离 :按Schema分离数据

    前言 本文是多租户系列文章的附加操作文章,如果想查看系列中的其他文章请查看下列文章 主线文章 Asp.net core下利用EF core实现从数据实现多租户(1) Asp.net core下利用EF ...

  9. MySQL Performance Schema详解

    MySQL的performance schema 用于监控MySQL server在一个较低级别的运行过程中的资源消耗.资源等待等情况. 1 performance schema特点 提供了一种在数据 ...

随机推荐

  1. C# Azure 存储-分布式缓存Redis的新建&配置&查看

    1. 介绍 Redis 是一款开源的,基于 BSD 许可的,高级键值 (key-value) 缓存 (cache) 和存储 (store) 系统.由于 Redis 的键包括 string,hash,l ...

  2. netstat 1

    lsof -i   :8086 第二个参数需要:开头 netstat linux  -a (all)显示所有选项,默认不显示LISTEN相关 理解这个很关键, 这是为什么我们netstat -a 不显 ...

  3. Java中常量小知识

    常量分类:常量分为静态常量,非静态常量(全局常量),局部常量 静态常量:要么定义的时候赋初值,要么在静态代码块中赋值 非静态常量:要么在定义的时候赋初值,要么在代码块中赋值 局部常量:可以在定义时赋初 ...

  4. 第十六回 IoC组件Unity续~批量动态为Unity添加类型和行为

    回到目录 之前的一篇Unity的文章主要是基本的实现,并没有什么特别的地方,使用Unity可以方便的实现应用程序的IoC控制反转,这给我们的应用程序在耦合度上变得高了,同时可测试性加强了,当然,这些的 ...

  5. Java学习之路:不走弯路,就是捷径

    1.如何学习程序设计? JAVA是一种平台,也是一种程序设计语言,如何学好程序设计不仅仅适用于JAVA,对C++等其他程序设计语言也一样管用.有编程高手认为,JAVA也好C也好没什么分别,拿来就用.为 ...

  6. Atitit intellij idea的使用总结attilax

    Atitit intellij idea的使用总结attilax 1. ideaIC-2016.2.4.exe1 1.1. Ij vs eclipse市场份额1 1.2. Ij的优点(方便的支持gro ...

  7. 大数据时代的IT架构设计

    大数据时代的IT架构设计(来自互联网.银行等领域的一线架构师先进经验分享) IT架构设计研究组 编著   ISBN 978-7-121-22605-2 2014年4月出版 定价:49.00元 208页 ...

  8. javascript_core_07之错误处理、函数作用域

    1.错误处理:保证程序发生错误时,不会被强制退出: ①处理方式:try{可能出错的正常语句:}catch(err){只有出现错误时才执行的错误处理代码:}finally{无论是否出错都必须执行的代码: ...

  9. Java 7 中的Switch 谈 Java版本更新和反编译知识

    Java 7 中的Switch 谈 Java版本更新和反编译知识          学习编程,享受生活,大家好,我是追寻梦的飞飞.今天主要讲述的是Java7中的更新Switch实现内部原理和JAD反编 ...

  10. javaweb+SSH实现简单的权限管理系统

    权限管理,平时里很多地方我们都可以看到,比如聊QQ时群里的群主.管理员以及成员之间的功能是不一样的--大家一定会遇到的一个问题,所以整理 一下自己写权限系统的一些经验给大家,只起参考作用,也望大家笑纳 ...