SQL編程
1).if-elseif-else陳述句
語法:
if 條件 then
//條件1
elseif 條件 then
//條件2
else
//條件3
end if;
示例演示:
create procedure pro_9(in grade int) -- 輸入等級 begin if grade=1 then select '最強王者' as '排位等級'; elseif grade=2 then select '超凡大師' as '排位等級'; elseif grade=3 then select '璀璨鉆石' as '排位等級'; else select '恥辱廢鐵' as '排位等級'; end if; end// ? call pro_9(3)//
(2).case-when-then
語法:
case 變數
when 1 then 條件1
when 2 then 條件2
when 3 then 條件3
else 默認條件
end case;
示例演示:
create procedure pro_10(in grade int) begin case grade when 1 then select '最強王者' as '排位等級'; when 2 then select '超凡大師' as '排位等級'; when 3 then select '璀璨鉆石' as '排位等級'; else select '恥辱廢鐵' as '排位等級'; end case; end// ? call pro_10(2)//
case-when-then陳述句寫在sql陳述句當中:
select sid,sname,sex,age,city,ch,ma,case when ch>=90 then '等級A' when ch>=80 then '等級B' when ch>=70 then '等級C' when ch>=60 then '等級D' else '退學吧' end as '等級' from stuinfo left join stumarks using(sid)//
(3).loop回圈
在python中回圈遇到break會退出,MySQL中leave=break,
?create procedure pro_11(in num int) begin declare total int default 0; declare i int default 0; -- 方法體可以起別名 xiaofang:loop set total = total+i; set i=i+1; if i>num then leave xiaofang; end if; end loop; select total from dual; end// ? call pro_11(100)//
(4).while回圈
語法:
while 條件 do
//代碼
end while
示例演示:
create procedure pro_12(in num int) begin declare total int default 0; declare i int default 0; while num>=i do set total = total+i; set i=i+1; end while; select total from dual; end// ? call pro_12(100)//
(5).repeat回圈
語法:
repeat
代碼
until 條件 --碰到條件為true就結束回圈
end repeat;
示例演示:
create procedure pro_13(in num int) begin declare total int default 0; declare i int default 0; repeat set total = total+i; set i=i+1; until i>num end repeat; select total from dual; end// ? call pro_13(100)//
(6).leave和iterate
leave相當于break;iterate相當于continue,
示例演示:
create procedure pro_14() begin declare i int default 0; sign:while i<5 do set i=i+1; if(i=3) then iterate sign; end if; select i from dual; end while; end// ? call pro_14()//
函式
(1)內置函式
a.數字類
| 陳述句 | 含義 |
|---|---|
| select rand() from dual// | 隨即小數 |
| select * from stuinfo order by rand()// | 隨即排序 |
| select round(5.6) from dual// | 四舍五入 |
| select ceil(5.6)// | 向上取整 |
| select floor(5.6)// | 向下取整 |
b.大小寫轉換
| 陳述句 | 含義 |
|---|---|
| select ucase('hello')// | 大寫 |
| select lcase('Hello')// | 小寫 |
c.截取字串
| 陳述句 | 含義 |
|---|---|
| select left('abcdef',3)// | 從左邊截取3個字符 |
| select right('abcdef',3)// | 從右邊截取3個字符 |
| select substring('abcdef',2,3)// | 從第二個位置向后取3個字符 |
d.字符拼接
關鍵字:concat()
select concat(sid,sname,age,sex,city) from stuinfo//
e.獲取字符的長度
length() #位元組長度
char_length() #字符長度
trim() #去除前后空格
replace() #替換
select length('資料庫')// --cmd中輸出的是gbk編碼,6 ? select char_length('資料庫')// -- 3 ? select char_length(trim(' 資料庫 '))// --3 ? select replace('MySQL','SQL','sql')// --將MYSQL中的SQL替換成sql
f.獲取Unix時間戳
select unix_timestamp()// ? -- 把時間戳轉變為當前的時間格式 select from_unixtime(unix_timestamp())//
g.時間函式
select now(),year(now()),month(now()),day(now()),hour(now()),minute(now()),second(now())\G
h.西式時間函式
select dayname(now()) as `星期`,monthname(now()) as `月份`,dayofyear(now()) as '本年第幾天'//
(2)自定義函式
語法:
create function `函式名`(引數....) returns 回傳的資料型別
begin
函式體;
end//
示例演示:
create function func_1() returns varchar(64) begin return '2020年東京奧運會'; end// ? select func_1()// create function func_2(num int) returns int(64) begin return num*num; end// ? select func_2(10)//
觸發器
1.觸發器是一個特殊的存盤程序
2.不需要呼叫,MySQL自動呼叫
3.是一個事務,可以回滾
4.跟著表后面的
(1).觸發器的種類
觸發事件,產生行為(增刪改)
1.insert觸發器
2.update觸發器
3.delete觸發器
(2).創建觸發器
語法:
create trigger `觸發器名` 觸發時間[before|after] 觸發事件 on `表名` for each row
begin
代碼+sql
end//
(3)new表和old表
1.這兩張表都是臨時表 ;注意和視圖的區別,視圖是merge合并演算法,是永久表
2.當觸發器觸發的時候,記憶體中自動創建,執行完畢以后自動銷毀
3.它們的表結構和關聯的表是一致的
4.只讀,不能修改
(4)insert觸發器
create trigger `trig1` after insert on `stuinfo`for each row begin declare sid_ int default 0; declare ch_ float default 0; declare ma_ float default 0; set sid_ = new.sid; insert into stumarks values(sid_,ch_,ma_); end// ? insert into stuinfo values(null,'ABC',2,18,'Q',90);
(5)update觸發器
create trigger `trig2` after update on `stuinfo`for each row begin declare sid_ int default 0; declare old_sid int default 0; declare ch_ float default 0; declare ma_ float default 0; set sid_ = new.sid; set old_sid = old.sid; update stumarks set sid=sid_ where sid=old_sid; end// ? update stuinfo set sid=17 where sid=16//
(6)delete觸發器
create trigger `trig3` after delete on `stuinfo`for each row begin declare sid_ int default 0; declare ch_ float default 0; declare ma_ float default 0; set sid_ = old.sid; delete from stumarks where sid=sid_; end// ? delete from stuinfo where sid=17//
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/64444.html
標籤:MySQL
上一篇:mysql入門(二)
下一篇:MySQL資料庫02
