mysql資料庫重命名存儲過程,mysql怎麼重命名資料庫

本文目錄一覽:

怎樣在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的到來吧!

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/237626.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:06
下一篇 2024-12-12 12:06

相關推薦

  • 如何修改mysql的埠號

    本文將介紹如何修改mysql的埠號,方便開發者根據實際需求配置對應埠號。 一、為什麼需要修改mysql埠號 默認情況下,mysql使用的埠號是3306。在某些情況下,我們需…

    編程 2025-04-29
  • Python 常用資料庫有哪些?

    在Python編程中,資料庫是不可或缺的一部分。隨著互聯網應用的不斷擴大,處理海量數據已成為一種趨勢。Python有許多成熟的資料庫管理系統,接下來我們將從多個方面介紹Python…

    編程 2025-04-29
  • openeuler安裝資料庫方案

    本文將介紹在openeuler操作系統中安裝資料庫的方案,並提供代碼示例。 一、安裝MariaDB 下面介紹如何在openeuler中安裝MariaDB。 1、更新軟體源 sudo…

    編程 2025-04-29
  • Python操作MySQL

    本文將從以下幾個方面對Python操作MySQL進行詳細闡述: 一、連接MySQL資料庫 在使用Python操作MySQL之前,我們需要先連接MySQL資料庫。在Python中,我…

    編程 2025-04-29
  • 資料庫第三範式會有刪除插入異常

    如果沒有正確設計資料庫,第三範式可能導致刪除和插入異常。以下是詳細解釋: 一、什麼是第三範式和範式理論? 範式理論是關係資料庫中的一個規範化過程。第三範式是範式理論中的一種常見形式…

    編程 2025-04-29
  • MySQL遞歸函數的用法

    本文將從多個方面對MySQL遞歸函數的用法做詳細的闡述,包括函數的定義、使用方法、示例及注意事項。 一、遞歸函數的定義 遞歸函數是指在函數內部調用自身的函數。MySQL提供了CRE…

    編程 2025-04-29
  • leveldb和unqlite:兩個高性能的資料庫存儲引擎

    本文將介紹兩款高性能的資料庫存儲引擎:leveldb和unqlite,並從多個方面對它們進行詳細的闡述。 一、leveldb:輕量級的鍵值存儲引擎 1、leveldb概述: lev…

    編程 2025-04-28
  • Python怎麼導入資料庫

    Python是一種高級編程語言。它具有簡單、易讀的語法和廣泛的庫,讓它成為一個靈活和強大的工具。Python的資料庫連接類型可以多種多樣,其中包括MySQL、Oracle、Post…

    編程 2025-04-28
  • MySQL bigint與long的區別

    本文將從數據類型定義、存儲空間、數據範圍、計算效率、應用場景五個方面詳細闡述MySQL bigint與long的區別。 一、數據類型定義 bigint在MySQL中是一種有符號的整…

    編程 2025-04-28
  • MySQL左連接索引不生效問題解決

    在MySQL資料庫中,經常會使用左連接查詢操作,但是左連接查詢中索引不生效的情況也比較常見。本文將從多個方面探討MySQL左連接索引不生效問題,並給出相應的解決方法。 一、索引的作…

    編程 2025-04-28

發表回復

登錄後才能評論