MySQL一次性創(chuàng)建表格存儲過程實戰(zhàn)
一、創(chuàng)建表格
創(chuàng)建下個月的每天對應(yīng)的表user_2022_01_01、...
需求描述:
我們需要用某個表記錄很多數(shù)據(jù),比如記錄某某用戶的搜索、購買行為(注意,此處是假設(shè)用數(shù)據(jù)庫保存),當(dāng)每天記錄較多時,如果把所有數(shù)據(jù)都記錄到一張表中太龐大,需要分表,我們的要求是,每天一張表,存當(dāng)天的統(tǒng)計數(shù)據(jù),就要求提前生產(chǎn)這些表——每月月底創(chuàng)建下一個月每天的表!
PREPARE stmt_name FROM preparable_stmt EXECUTE stmt_name [USING @var_name [, @var_name] ...] {DEALLOCATE | DROP} PREPARE stmt_name -- 知識點 時間的處理 -- EXTRACT(unit FROM date)截取時間的指定位置值 -- DATE_ADD(date,INTERVAL expr unit) 日期運算 -- LAST_DAY(date) 獲取日期的最后一天 -- YEAR(date) 返回日期中的年 -- MONTH(date) 返回日期的月 -- DAYOFMONTH(date) 返回日
思路:構(gòu)建循環(huán)語句,創(chuàng)建單個表格比較的簡單,但是對于很多種表格,而且是下個月的表格,對于表命名有一定的要求,所以就需要用到我們之前的日期函數(shù),和字符串函數(shù)的一些知識。
-- 思路:循環(huán)構(gòu)建表名 user_2021_11_01 到 user_2020_11_30;并執(zhí)行create語句。 use mysql7_procedure; drop procedure if exists proc22_demo; delimiter $$ create procedure proc22_demo() begin declare next_year int; declare next_month int; declare next_month_day int; declare next_month_str char(2); declare next_month_day_str char(2); -- 處理每天的表名 declare table_name_str char(10); declare t_index int default 1; -- declare create_table_sql varchar(200);
首先利用declare 定義需要的一些變量,next_year(下一年),next_month(下一個月),next_month_day(天數(shù)),這里為什么要這樣去定義,特別是年,月,不應(yīng)該是提前知道的嗎?答案是有時候比如是12月呢,那么下一個月的年份就不一樣了,所以需要利用日期函數(shù)的一些運算去解決這些問題。
-- 獲取下個月的年份 set next_year = year(date_add(now(),INTERVAL 1 month)); -- 獲取下個月是幾月 set next_month = month(date_add(now(),INTERVAL 1 month)); -- 下個月最后一天是幾號 set next_month_day = dayofmonth(LAST_DAY(date_add(now(),INTERVAL 1 month))); if next_month < 10 then set next_month_str = concat('0',next_month); else set next_month_str = concat('',next_month); end if; while t_index <= next_month_day do if (t_index < 10) then set next_month_day_str = concat('0',t_index); else set next_month_day_str = concat('',t_index); end if;
上面都是對表的名字的一些字段和別名進行獲取和拼接
set table_name_str = concat(next_year,'_',next_month_str,'_',next_month_day_str); -- 拼接create sql語句 set @create_table_sql = concat( 'create table user_', table_name_str, '(`uid` INT ,`ename` varchar(50) ,`information` varchar(50)) COLLATE=\'utf8_general_ci\' ENGINE=InnoDB'); -- FROM后面不能使用局部變量! prepare create_table_stmt FROM @create_table_sql; execute create_table_stmt; DEALLOCATE prepare create_table_stmt; set t_index = t_index + 1; end while; end $$ delimiter ; call proc22_demo();
這樣就實現(xiàn)了效果
二、補充:MySQL的存儲函數(shù)與存儲過程的區(qū)別
MySQL存儲函數(shù)(自定義函數(shù)),函數(shù)一般用于計算和返回一個值,可以將經(jīng)常需要使用的計算或功能寫成一個函數(shù)。
存儲函數(shù)和存儲過程一樣,都是在數(shù)據(jù)庫中定義一些 SQL 語句的集合。
存儲函數(shù)與存儲過程的區(qū)別;
- 1.存儲函數(shù)有且只有一個返回值,而存儲過程可以有多個返回值,也可以沒有返回值。
- 2.存儲函數(shù)只能有輸入?yún)?shù),而且不能帶in, 而存儲過程可以有多個in,out,inout參數(shù)。
- 3.存儲過程中的語句功能更強大,存儲過程可以實現(xiàn)很復(fù)雜的業(yè)務(wù)邏輯,而函數(shù)有很多限制,如不能在函數(shù)中使用insert,update,delete,create等語句;
- 4.存儲函數(shù)只完成查詢的工作,可接受輸入?yún)?shù)并返回一個結(jié)果,也就是函數(shù)實現(xiàn)的功能針對性比較強。
- 5.存儲過程可以調(diào)用存儲函數(shù)、但函數(shù)不能調(diào)用存儲過程。
- 6.存儲過程一般是作為一個獨立的部分來執(zhí)行(call調(diào)用)。而函數(shù)可以作為查詢語句的一個部分來調(diào)用.
create function func_name ([param_name type[,...]]) returns type [characteristic ...]? begin ? ? routine_body end;
參數(shù)說明:
- (1)func_name :存儲函數(shù)的名稱。
- (2)param_name type:可選項,指定存儲函數(shù)的參數(shù)。type參數(shù)用于指定存儲函數(shù)的參數(shù)類型,該類型可以是MySQL數(shù)據(jù)庫中所有支持的類型。
- (3)RETURNS type:指定返回值的類型。
- (4)characteristic:可選項,指定存儲函數(shù)的特性。
- (5)routine_body:SQL代碼內(nèi)容。
create database mydb9_function; -- 導(dǎo)入測試數(shù)據(jù) use mydb9_function; set global log_bin_trust_function_creators=TRUE; -- 信任子程序的創(chuàng)建者 -- 創(chuàng)建存儲函數(shù)-沒有輸輸入?yún)?shù) drop function if exists myfunc1_emp; delimiter $$ create function myfunc1_emp() returns int begin ? declare cnt int default 0; ? ? select count(*) into ?cnt from emp; ? return cnt; end $$ delimiter ; -- 調(diào)用存儲函數(shù) select myfunc1_emp();
create database mydb9_function; -- 導(dǎo)入測試數(shù)據(jù) use mydb9_function; set global log_bin_trust_function_creators=TRUE; -- 信任子程序的創(chuàng)建者 -- 創(chuàng)建存儲函數(shù)-沒有輸輸入?yún)?shù) drop function if exists myfunc1_emp; delimiter $$ create function myfunc1_emp() returns int begin ? declare cnt int default 0; ? ? select count(*) into ?cnt from emp; ? return cnt; end $$ delimiter ; -- 調(diào)用存儲函數(shù) select myfunc1_emp();
到此這篇關(guān)于MySQL一次性創(chuàng)建表格存儲過程實戰(zhàn)的文章就介紹到這了,更多相關(guān)MySQL創(chuàng)建表格內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。