HeidiSQL是一款数据库管理工具,它是德国程序员Ansgar Becker和几个Delphi程序员开发的一个开源工具。支持通过多种方式连接管理 MySQL(MariaDB)、MSSQL Server、PostgreSQL数据库,功能强大,完全免费,推荐大家使用。HeidiSQL比起其他客户端工具而言,支持显示库表的索引大小和数据大小,并且支持改库名(是通过移动表来完成)。
官网:https://www.heidisql.com
HeidiSQL特点:
• 连接到多个服务器窗口。
• 可以使用命令行连接到服务器。
• 创建和编辑表格,视图,存储程序,触发器和安排日程。
• 从一台服务器/数据库直接导入数据到另一台服务器/数据库
• 管理用户权限。
• 导入文本文件。
• 为CSV,HTML,XML和SQL导出表行。
• 批量修改表(移动到新DB,改变,整理等)
• 批量插入到表中ASCII或二进制文件。
• 写自定义语法高亮和代码自动完成查询。
• 漂亮的SQL格式化。
• 监视和杀灭客户进程。
• 找到一个所有数据库的所有表的具体文本服务器。
• 在批处理方式优化和修复表。
界面就是这样,具体使用需要大家多多尝试。今天主要说一下使用HeidiSQL来更改库名,其是通过移动表来完成的。MySQL原生是不支持更改库名的,只支持更改表名。但是我们可以借助rename这个更改表的命令来完成快速更改库名,这个方法比把全部数据导出再导入来得更快。
核心操作方法:
1. create database newdb
2. rename olddb.tables to newdb.tables
但是这个命令碰到视图会出错,也不能把旧库中的函数,存储过程,事件移过来,对于这一些,我们可以从旧库中导出这些定义,再导入到新库中。我们不可能一个个去改,得借助脚本。
脚本实现的逻辑思路:
1. 查看要改的新旧库是否存在,来判断是否需要进一步处理。
2. 先用rename把所有的表移到新命名的库中。
3. 导出旧库中的视图,函数,存储过程,事件。
4. 导入新库中。
5. 删除旧库。
而在HeidiSQL中更改库名就是这么实现的,也就避免了我们自己写脚本了。在HeidiSQL界面选择一个库名,右击编辑库,就可以更改库名了。然后就可以看到如下操作过去:
SELECT CONNECTION_ID();
/* 已连接: 线程ID:11 */
/* 字符集: utf8mb4 */
SHOW STATUS;
SHOW VARIABLES;
SHOW DATABASES;
/* 进入会话 "test" */
SELECT `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`='information_schema';
SHOW TABLE STATUS FROM `information_schema`;
SHOW FUNCTION STATUS WHERE `Db`='information_schema';
SHOW PROCEDURE STATUS WHERE `Db`='information_schema';
SHOW TRIGGERS FROM `information_schema`;
SELECT `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`='information_schema';
SHOW TABLE STATUS FROM `information_schema`;
SHOW FUNCTION STATUS WHERE `Db`='information_schema';
SHOW PROCEDURE STATUS WHERE `Db`='information_schema';
SHOW TRIGGERS FROM `information_schema`;
SHOW EVENTS FROM `information_schema`;
SELECT *, EVENT_SCHEMA AS `Db`, EVENT_NAME AS `Name` FROM information_schema.`EVENTS` WHERE `EVENT_SCHEMA`='information_schema';
SELECT `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`='mysql';
SHOW TABLE STATUS FROM `mysql`;
SHOW FUNCTION STATUS WHERE `Db`='mysql';
SHOW PROCEDURE STATUS WHERE `Db`='mysql';
SHOW TRIGGERS FROM `mysql`;
SELECT *, EVENT_SCHEMA AS `Db`, EVENT_NAME AS `Name` FROM information_schema.`EVENTS` WHERE `EVENT_SCHEMA`='mysql';
USE `mysql`;
SHOW CREATE TABLE `mysql`.`db`;
SHOW COLLATION;
SHOW ENGINES;
USE `test`;
SELECT `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`='test';
SHOW TABLE STATUS FROM `test`;
SHOW FUNCTION STATUS WHERE `Db`='test';
SHOW PROCEDURE STATUS WHERE `Db`='test';
SHOW TRIGGERS FROM `test`;
SELECT *, EVENT_SCHEMA AS `Db`, EVENT_NAME AS `Name` FROM information_schema.`EVENTS` WHERE `EVENT_SCHEMA`='test';
SHOW VARIABLES LIKE 'collation_server';
SHOW CREATE DATABASE `test`;
SELECT `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`='test';
SHOW TABLE STATUS FROM `test`;
SHOW FUNCTION STATUS WHERE `Db`='test';
SHOW PROCEDURE STATUS WHERE `Db`='test';
SHOW TRIGGERS FROM `test`;
SELECT *, EVENT_SCHEMA AS `Db`, EVENT_NAME AS `Name` FROM information_schema.`EVENTS` WHERE `EVENT_SCHEMA`='test';
SHOW DATABASES;
CREATE DATABASE `test1` /*!40100 COLLATE 'utf8_general_ci' */;
RENAME TABLE `test`.`user1` TO `test1`.`user1`;
RENAME TABLE `test`.`user2` TO `test1`.`user2`;
RENAME TABLE `test`.`user3` TO `test1`.`user3`;
RENAME TABLE `test`.`user4` TO `test1`.`user4`;
SELECT `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`='test';
SHOW TABLE STATUS FROM `test`;
SHOW FUNCTION STATUS WHERE `Db`='test';
SHOW PROCEDURE STATUS WHERE `Db`='test';
SHOW TRIGGERS FROM `test`;
SELECT *, EVENT_SCHEMA AS `Db`, EVENT_NAME AS `Name` FROM information_schema.`EVENTS` WHERE `EVENT_SCHEMA`='test';
DROP DATABASE `test`;
SHOW DATABASES;
/* 进入会话 "test" */
SHOW DATABASES;
/* 进入会话 "test" */
SELECT `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`='';
SHOW TABLE STATUS FROM ``;
/* SQL错误(1102):Incorrect database name '' */
SHOW FUNCTION STATUS WHERE `Db`='';
SHOW PROCEDURE STATUS WHERE `Db`='';
SHOW TRIGGERS FROM ``;
/* SQL错误(1102):Incorrect database name '' */
SELECT *, EVENT_SCHEMA AS `Db`, EVENT_NAME AS `Name` FROM information_schema.`EVENTS` WHERE `EVENT_SCHEMA`='';
SELECTCONNECTION_ID();
/*已连接:线程ID:11*/
/*字符集:utf8mb4*/
SHOWSTATUS;
SHOWVARIABLES;
SHOWDATABASES;
/*进入会话"test"*/
SELECT`DEFAULT_COLLATION_NAME`FROM`information_schema`.`SCHEMATA`WHERE`SCHEMA_NAME`='information_schema';
SHOWTABLESTATUSFROM`information_schema`;
SHOWFUNCTIONSTATUSWHERE`Db`='information_schema';
SHOWPROCEDURESTATUSWHERE`Db`='information_schema';
SHOWTRIGGERSFROM`information_schema`;
SELECT`DEFAULT_COLLATION_NAME`FROM`information_schema`.`SCHEMATA`WHERE`SCHEMA_NAME`='information_schema';
SHOWTABLESTATUSFROM`information_schema`;
SHOWFUNCTIONSTATUSWHERE`Db`='information_schema';
SHOWPROCEDURESTATUSWHERE`Db`='information_schema';
SHOWTRIGGERSFROM`information_schema`;
SHOWEVENTSFROM`information_schema`;
SELECT*,EVENT_SCHEMAAS`Db`,EVENT_NAMEAS`Name`FROMinformation_schema.`EVENTS`WHERE`EVENT_SCHEMA`='information_schema';
SELECT`DEFAULT_COLLATION_NAME`FROM`information_schema`.`SCHEMATA`WHERE`SCHEMA_NAME`='mysql';
SHOWTABLESTATUSFROM`mysql`;
SHOWFUNCTIONSTATUSWHERE`Db`='mysql';
SHOWPROCEDURESTATUSWHERE`Db`='mysql';
SHOWTRIGGERSFROM`mysql`;
SELECT*,EVENT_SCHEMAAS`Db`,EVENT_NAMEAS`Name`FROMinformation_schema.`EVENTS`WHERE`EVENT_SCHEMA`='mysql';
USE`mysql`;
SHOWCREATETABLE`mysql`.`db`;
SHOWCOLLATION;
SHOWENGINES;
USE`test`;
SELECT`DEFAULT_COLLATION_NAME`FROM`information_schema`.`SCHEMATA`WHERE`SCHEMA_NAME`='test';
SHOWTABLESTATUSFROM`test`;
SHOWFUNCTIONSTATUSWHERE`Db`='test';
SHOWPROCEDURESTATUSWHERE`Db`='test';
SHOWTRIGGERSFROM`test`;
SELECT*,EVENT_SCHEMAAS`Db`,EVENT_NAMEAS`Name`FROMinformation_schema.`EVENTS`WHERE`EVENT_SCHEMA`='test';
SHOWVARIABLESLIKE'collation_server';
SHOWCREATEDATABASE`test`;
SELECT`DEFAULT_COLLATION_NAME`FROM`information_schema`.`SCHEMATA`WHERE`SCHEMA_NAME`='test';
SHOWTABLESTATUSFROM`test`;
SHOWFUNCTIONSTATUSWHERE`Db`='test';
SHOWPROCEDURESTATUSWHERE`Db`='test';
SHOWTRIGGERSFROM`test`;
SELECT*,EVENT_SCHEMAAS`Db`,EVENT_NAMEAS`Name`FROMinformation_schema.`EVENTS`WHERE`EVENT_SCHEMA`='test';
SHOWDATABASES;
CREATEDATABASE`test1`/*!40100COLLATE'utf8_general_ci'*/;
RENAMETABLE`test`.`user1`TO`test1`.`user1`;
RENAMETABLE`test`.`user2`TO`test1`.`user2`;
RENAMETABLE`test`.`user3`TO`test1`.`user3`;
RENAMETABLE`test`.`user4`TO`test1`.`user4`;
SELECT`DEFAULT_COLLATION_NAME`FROM`information_schema`.`SCHEMATA`WHERE`SCHEMA_NAME`='test';
SHOWTABLESTATUSFROM`test`;
SHOWFUNCTIONSTATUSWHERE`Db`='test';
SHOWPROCEDURESTATUSWHERE`Db`='test';
SHOWTRIGGERSFROM`test`;
SELECT*,EVENT_SCHEMAAS`Db`,EVENT_NAMEAS`Name`FROMinformation_schema.`EVENTS`WHERE`EVENT_SCHEMA`='test';
DROPDATABASE`test`;
SHOWDATABASES;
/*进入会话"test"*/
SHOWDATABASES;
/*进入会话"test"*/
SELECT`DEFAULT_COLLATION_NAME`FROM`information_schema`.`SCHEMATA`WHERE`SCHEMA_NAME`='';
SHOWTABLESTATUSFROM``;
/*SQL错误(1102):Incorrectdatabasename''*/
SHOWFUNCTIONSTATUSWHERE`Db`='';
SHOWPROCEDURESTATUSWHERE`Db`='';
SHOWTRIGGERSFROM``;
/*SQL错误(1102):Incorrectdatabasename''*/
SELECT*,EVENT_SCHEMAAS`Db`,EVENT_NAMEAS`Name`FROMinformation_schema.`EVENTS`WHERE`EVENT_SCHEMA`='';
其执行过程跟上面给出的步骤差不多,自己可以找一个复杂的库更改一下试试看,玩的愉快。