首页 > 编程知识 正文

mysql数据库重命名存储过程,mysql怎么重命名数据库

时间:2023-12-27 22:27:13 阅读:325699 作者:REYH

本文目录一览:

怎样在mysql里面修改数据库名称?

被取消的命令MySQL 之前提供了一个 rename database db_old to db_new 的命令来直接对数据库改名,可能由于实现的功能不完备(比如,这条命令可能是一个超大的事务,或者是由于之前的表很多还是 MyISAM 等),后来的版本直接取消了这条命令。更改数据库名大致上有以下几种方案:

一、mysqldump 导入导出要说最简单的方法,就是直接用 mysqldump 工具,在旧库导出再往新库导入(最原始、最慢、最容易想到)的方法:旧库 yttdb_old 导出(包含的对象:表、视图、触发器、事件、存储过程、存储函数)

二、改整库的表名利用 MySQL 更改表名的方法来批量把旧库的所有表依次遍历,改名为新库的表。这种方法比第一种要快很多倍,但是没有第一步操作起来那么顺滑,不能一步到位。比如,要把数据库 yttdb_old 改名为 yttdb_new,如果数据库 yttdb_old 里只有磁盘表,那很简单,直接改名即可。或者写个脚本来批量改,非常简单。但是一般旧库里不只有磁盘表,还包含其他各种对象。这时候可以先考虑把旧库的各种对象导出来,完了在逐一改完表名后导进去。

三、历史方案其实在 MySQL 早期还有一种方法。假设 MySQL 部署好了后,所有的 binlog 都有备份,并且二进制日志格式还是 statement 的话,那就可以简单搭建一台从机,让它慢慢追主机到新的库名,等确切要更改旧库的时候,再直接晋升从机为主机即可。这里只需要从机配置一个参数来把旧库指向为新库:replicate-rewrite-db=yttdb_old-yttdb_new不过这种局限性很大,不具备标准化,不推荐。

总结其实针对 MySQL 本身改库名,大致就这么几种方法:

如果数据量小,推荐第一种;

数据量大,则推荐第二种;

数据量巨大,那就非 MySQL 本身能解决的了。

可通过部署第三方 ETL 工具,通过解析 MySQL 二进制日志或其他的方式来把旧库数据直接读取到新库达到改名的目的等等。

mysql如何变更数据库实例名

常见的主要有三种方法:

如果所有表都是MyISAM类型的话,可以直接修改文件夹的名字。关闭mysql→把data目录中的db_name目录重命名为new_db_name→开启mysql

新建数据库,在新的数据库里重命名所有旧数据库中的表,再删除旧的数据库。具体操作命令如下:创建新的数据库→重命名数据表名称→删除旧的数据库。

CREATE DATABASE new_db_name;RENAME TABLE db_name.table1 TO new_db_name.table1,db_name.table2 TO new_db_name.table2;DROP DATABASE db_name;

利用mysqldump命令从旧的数据导出数据,再导入新数据库。具体操作命令如下:导出数据→创建新的数据库→导入数据→删除旧的数据库。

mysqldump -u root -p -h ip db_name db_name_dump.SQLmysql -u root -p -h ip -e “CREATE DATABASE new_db_name”mysql -u root -p -h ip new_db_name db_name_dump.SQLmysql -u root -p -h ip -e “DROP DATABASE db_name”

mysql存储过程及流程控制?

存储过程(Stored

Procedure)是一组为了完成特定功能的SQL语句集功能是将常用或复杂的工作,预先用SQL语句写好并用一个指定名称存储起来,

以后需要数据库提供与已定义好的存储过程的功能相同的服务时,只需调用

call

存储过程名字,

即可自动完成命令。存储过程是由流控制和SQL语句书写的过程,这个过程经编译和优化后存储在数据库服务器中,可由应用程序通过一个调用来执行,而且允许用户声明变量

。同时,存储过程可以接收和输出参数、返回执行存储过程的状态值,也可以嵌套调用。

mysql 存储过程

你应该在做统计吧,估计你不会的就是mysql存储过程的语法 我之前也写过 很是郁闷 我给你一段代码 是我用mysql写过的一个存储过程 你看看 主要是了解里面的语法 看懂了 你所说的需求并不难

有看不懂的地方一起讨论 :

begin

declare tikk datetime ;

declare done int default 0;

declare userid int default 0;

declare moduleid int default 0;

declare couid int default 0;

declare mduname varchar(255) ;

declare opsid int default 0;

declare c1 cursor for Select I_userID,I_operationID from space_operation_record where status0 group by I_userID,I_operationID order by createtime desc;

declare continue handler for sqlstate '02000' set done =1;

set tikk = now();

open c1;

repeat

fetch c1 into userid, opsid;

if not done then

select I_moduleID from space_operation where status0 and ID=opsid into moduleid;

if moduleid '' then

select Nvc_identification from space_operation where status0 and ID=opsid into @identiftion;

if moduleid 0 then

Select Nvc_ename from space_module where status0 and ID=moduleid into mduname;

else

set mduname = 'space';

end if;

create temporary table if not exists sp_tab1(id bigint(20),Nvc_content MEDIUMTEXT,I_obyuID bigint(20),I_tID bigint(20),createtime datetime);

INSERT INTO sp_tab1 Select ID,Nvc_content,I_objectID,I_tmID,createtime from space_operation_record where status0 and I_operationID=opsid and I_userID=userid ;

select count(*) from sp_tab1 into couid;

set @ihod = 0;

set @listp = '';

set @listpp = '';

set @content0p = '';

set @content0 = '';

while couid 0 do

select ID,Nvc_content,I_obyuID,createtime,I_tID into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where ID @ihod order by ID asc limit 0,1;

if @iok '' then

if mduname = 'blog' then

INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,tikk);

elseif mduname = 'team' then

if(@identiftion = 'addblog' || @identiftion = 'mdyblog') then

INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);

else

set @listpp = CONCAT(@listpp,CONCAT(@objiplk,','));

set @operarry1p = substring_index(@conuiy,'|',1);

set @operarry2p = substring_index(@conuiy,'|',-1);

set @content0p = CONCAT(@content0p,CONCAT(@operarry2p,SPACE(1)));

set @objlistp = substring(@listpp,1,length(@listpp)-1);

end if;

elseif mduname = 'space' then

if(@identiftion = 'headphoto' || @identiftion = 'status') then

INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);

else

set @listppr = CONCAT(@listppr,CONCAT(@objiplk,','));

set @operarry1pr = substring_index(@conuiy,'|',1);

set @operarry2pr = substring_index(@conuiy,'|',-1);

set @content0pr = CONCAT(@content0pr,CONCAT(@operarry2pr,SPACE(1)));

set @objlistpr = substring(@listppr,1,length(@listppr)-1);

end if;

else

set @listp = CONCAT(@listp,CONCAT(@objiplk,','));

set @operarry1 = substring_index(@conuiy,'|',1);

set @operarry2 = substring_index(@conuiy,'|',-1);

set @content0 = CONCAT(@content0,CONCAT(@operarry2,SPACE(1)));

set @objlist = substring(@listp,1,length(@listp)-1);

end if;

set @ihod = @iok;

end if;

set couid = couid -1;

end while;

if @content0 '' then

set @contentp = CONCAT(@operarry1,concat('|',@content0));

Select createtime,ID into @uitimej,@IDjok from space_operation_record where status0 and I_operationID=opsid order by createtime desc limit 0,1;

if @uitimej '' then

INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlist);

end if;

end if;

if @content0p '' then

if @identiftion = 'addphoto' then

set @contentp = CONCAT(@operarry1p,CONCAT('|',@content0p));

else

set @contentp = CONCAT(@operarry1p,CONCAT(@content0p,'|'));

end if;

Select createtime,ID into @uitimej,@IDjok from space_operation_record where status0 and I_operationID=opsid order by createtime desc limit 0,1;

if @uitimej '' then

INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);

end if;

end if;

if @content0pr '' then

set @contentp = CONCAT(@operarry1p,concat('|',@content0pr));

Select createtime,ID into @uitimej,@IDjok from space_operation_record where status0 and I_operationID=opsid order by createtime desc limit 0,1;

if @uitimej '' then

INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);

end if;

end if;

delete from sp_tab1;

end if;

end if;

until done end repeat;

close c1;

drop temporary table if exists sp_tab1 ;

UPDATE space_operation_play SET status=0;

UPDATE space_operation_display SET status=0;

Select createtime into @ptimes from space_operation_stat where status0 order by createtime desc limit 0,1;

if @ptimes '' then

create temporary table if not exists sp_tab2(id bigint(20),Nvc_content MEDIUMTEXT,I_userID bigint(20),I_lyuID bigint(20),D_stattime datetime);

INSERT INTO sp_tab2 Select ID,Nvc_content,I_userID,I_tmID,D_stattime from space_operation_stat where status0 and createtime=@ptimes order by D_stattime desc limit 0,30;

select count(*) from sp_tab2 into @cou1id;

set @uoj = 0;

while @cou1id 0 do

select ID,Nvc_content,I_userID,D_stattime,I_lyuID into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where ID @uoj order by ID asc limit 0,1;

if @io1k '' then

INSERT INTO space_operation_play(I_statID,Nvc_content,D_stattime,I_userID,Createtime,I_tmID) VALUES (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa);

set @uoj = @io1k;

end if;

set @cou1id = @cou1id -1;

end while;

drop temporary table if exists sp_tab2 ;

end if;

end

mysql 存储过程 DDL 参数

MySQL8.0 开始支持原⼦ DDL(atomic DDL),数据字典的更新,存储引擎操作,写⼆进制日志结合成了一个事务。在没有原⼦DDL之前,DROP TABLE test1,test2;如遇到server crash,可能会有test1被drop了,test2没有被drop掉。下面来看下在MySQL8.0之前和MySQL8.0 数据字典的区别

在MySQL8.0 之前,Data Dictionary除了存在与.FRM, .TRG, .OPT ⽂件外,还存在于系统表中(MyISAM ⾮事务引擎表中),在MySQL8.0 ,Data Dictionary 全部存在于Data Dictionary Storage Engine(即 InnoDB表中),这使crash recovery 维持原⼦性成为了可能

存储引擎⽀持

目前,只有InnoDB存储引擎⽀持原子DDL,为了实现原子DDL,Innodb要写DDL logs 到 mysql.innodb_ddl_log 表,这是⼀个隐藏在mysql.ibd 数据字典表空间⾥的数据字典表。要看mysql.innodb_ddl_log 中的内容,需要

SET GLOBAL LOG_ERROR_VERBOSITY=3;(MySQL 8.0 默认为2,error log 记录Errors and

warnings,不不记录notes)

SET GLOBAL innodb_print_ddl_logs=1;

CREATE TABLE t1 (c1 INT) ENGINE = InnoDB;

查看error log

[Note] [MY-011066] InnoDB: DDL log insert : [DDL record: DELETE SPACE, id=30,

thread_id=25, space_id=9, old_file_path=./test/t1.ibd]

[Note] [MY-011066] InnoDB: DDL log delete : by id 30

[Note] [MY-011066] InnoDB: DDL log insert : [DDL record: REMOVE CACHE, id=31,

thread_id=25, table_id=1066, new_file_path=test/t1]

[Note] [MY-011066] InnoDB: DDL log delete : by id 31

[Note] [MY-011066] InnoDB: DDL log insert : [DDL record: FREE, id=32, thread_

id=25, space_id=9, index_id=143, page_no=4]

[Note] [MY-011066] InnoDB: DDL log delete : by id 32

[Note] [MY-011066] InnoDB: DDL log post ddl : begin for thread id : 25

[Note] [MY-011066] InnoDB: DDL log post ddl : end for thread id : 25

原子DDL 操作步骤

准备:创建所需的对象并将DDL⽇志写入 mysql.innodb_ddl_log表中。DDL日志定义了如何前滚和回滚DDL操作。

执行:执⾏DDL操作。例如,为CREATE TABLE操作执⾏创建。

提交:更新数据字典并提交数据字典事务。

Post-DDL:重播并从mysql.innodb_ddl_log表格中删除DDL⽇志。为确保回滚可以安全执⾏⽽不引⼊不⼀致性,在此最后阶段执⾏⽂件操作(如重命名或删除数据文件)。这一阶段还从 mysql.innodb_dynamic_metadata的数据字典表删除的动态元数据为了DROP TABLE,TRUNCATE和其它重建表的DDL操作。

⽆论事务是提交还是回滚,DDL日志都会mysql.innodb_ddl_log在Post-DDL阶段重播并从表中删除 。mysql.innodb_ddl_log如果服务器在DDL操作期间暂停,DDL⽇志应该只保留在表中。在这种情况下,DDL⽇志会在恢复后重播并删除。

在恢复情况下,当服务器重新启动时,可能会提交或回退DDL事务。如果在重做⽇志和⼆进制日志中存在DDL操作的提交阶段期间执⾏的数据字典事务,则该操作被认为是成功的并且被前滚。否则,在InnoDB重放数据字典重做日志时回滚不完整的数据字典事务 ,并且回滚DDL事务。

原⼦DDL ⽀持类型

• DROP TABLES , all tables dropped or none

• DROP SCHEMA, all entities in the schema are dropped, or none

• Note that atomic DDL statements will be rolled back or committed even in case of crash, e.g. RENAME TABLES

• CREATE TABLE would be successfully committed or rolled back (no orphan ibd left)

• TRUNCATE TABLE (including InnoDB tables with FTS AUX tables) would be successfully committed or rolled back

• RENAME TABLES, all or none

• ALTER TABLE successful or not done

示例

请点击输入图片描述

请点击输入图片描述

结论

在MySQL8.0之前,alter table 操作在server crash的情况下,会遗留.frm,.ibd文件。MySQL8.0 能实现原⼦DDL(包括 DROP TABLE, DROP SCHEMA, CREATE TABLE, TRUNCATE TABLE, ALTER TABLE),alter table 操作,在server crash的情况下,不会遗留.frm,.ibd临时文件。让我们⼀起期待MySQL8.0 GA的到来吧!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。