MySQL-10-mysql編程,linuxmysql編程
MySQL編程
1.語句結束符
預設有兩個: ; 和 \g(只能在命令列中使用)
可以使用delimiter 命令來修改語句結束符,例如: delimiter $$(注意,一般手工修改結束符後再改回預設值 ;)
2.注釋
a) 行注釋: # 和 --[空格]
b) 塊注釋: /* */
3.變數
a) 系統變數: show variables [like pattern]; 查看
b) 使用者自訂變數:
i. set 變數名=變數值; 注意:為了區分使用者自訂變數和系統變數,需要在使用者自訂變數名稱前加@符號。例如 set @name=’John’; 使用select @變數名; 來擷取自訂變數。
Set賦值用法的變數值也可是是標量查詢的結果,例如:
Set @total=(select count(*) from student);
ii. 也可以使用select 欄位 from 表名 into @變數名; 例如:select name from student into @stuName;
iii. Select @who=’小李’ ; //關係判斷,注意不是賦值操作,判斷變數 @who中德值是不是等於“小李”,是的話返回1,否的話返回0.
定義變數的第三種方法: 使用 select @變數名 := 變數值 的方式
例如 select @who:=’小李’; //給變數賦值,並將其檢索出來
c) 變數的有效期間
會話結束後,變數就失效(即中斷連線後,變數失效)
d) 變數的範圍
使用者定義的變數是全域的。但在函數內定義的變數則是局部的。
4.資料類型
變數的資料類型與欄位的資料類型一致!
5.運算子
同一般程式設計語言中的運算子基本一致。算數運算子、邏輯運算子。
6.函數
1.內建函數
a) 數值處理函數
abs(x):返回絕對值
ceil(x):返回大於x的最小整數
floor(x):返回小於x的最大整數
mod(x,y):返回x與y的模
rand():返回0-1之間的隨機數 select round(rand()*100);
round(x,y):返回參數x的y位小數的四捨五入結果
truncate(x,y):返回數字x截斷為y位小數的結果字串處理函數
B)字串處理函數
1.concat(s1,s2....sn):把傳入的參數串連成一個字串
2.insert(str,x,y,insert):從str的x位置開始,替換y長度的字串為insert
select insert('abcdefg',2,3,'hello');
3.lower(str),upper(str):將字串轉換為大寫,小寫
4.left(str,x) right(str,x) 返回str左邊(右邊)x個字元,x為null則返回null
5.lpad(str,n,pad) rpad(str,n,pad) 用pad對字串str從最左邊(右邊)進行填充,直到總長度達到n
select name,lpad(name,10,'#'),rpad(name,10,'(') from cats;
6.trim(),ltrim(),rtrim()去掉兩邊,左邊,右邊空格
select concat('1',trim(' abc '),'2'),concat('1',ltrim(' abc '),'2'),concat('1',rtrim(' abc '),'2')\G;
7.replace(str,a,b) 在字串str中用字串b替換所有的字串a
8.strcmp(s1,s2):如果S1比S2小,返回-1;如果S1比S2大則返回1;如果相等則返回0(比較的是ASC2碼)
9.substring(str,x,y) 返回字串str中從位置x起,長度為y的子字串
C)日期時間
curdate()
curtime()
now(); select curdate();
select unix_timestamp(now());
select from_unixtime(1331110656);
select week(now()),year(now());
select hour(curtime()),minute(curtime());
select monthname(now());
select date_format(now(),"%Y-%m-%d %H:%i:%s");
D)流程式控制制函數
create table salary(id int,salary decimal(9,2));
if(value,true,false); select id,salary,if(salary>300,'high','low') from salary;
ifnull(t,false) select id,salary,ifnull(salary,0) from salary;
case when [value1] then [result1]...else[default]end;
Select
case when salary<=300 then 'low' else 'high' end
from salary;
E)其他
database() select database(); //返回當前使用的資料庫
version() //返回當前資料庫的版本
user() //返回當前串連上資料庫的使用者
inet_aton(ip) 將字串地址轉換為網路地址
password() 對mysql使用者加密
md5() 對使用者密碼加密
select * from mysql.user \G;
2.使用者自訂函數(注意:函數是綁定資料庫的,在某個資料庫中建立的函數只能在該資料庫中使用,資料庫被刪除,函數也相應被刪除)
文法:
create function 函數名(參數列表) returns 返回值類型
函數體
例子:
drop function if exists sayHello;
delimiter $$ //先將結束符改為$$,因為函數體中有分號,如果不將結束符修改一下,mysql會認為中途某個語句就結束,會報錯
create function sayHello() returns varchar(20)
begin
return 'hello world!';
end
$$ //函數也是語句,也需要結束符來結束
delimiter ; //將結束符改為預設!!!
使用 select sayHello(); 執行
7.流程式控制制
a) 分支語句
if 條件1 then
語句1
elseif 條件2 then
語句2
else
上述都不滿足時執行的語句
end if;
例子:
drop function if exists func1;
delimiter $$
create function func1() returns varchar(10)
begin
if hour(now()) < 12 then
return 'morning';
else
return 'afternoon';
end if;
end
$$
delimiter ;
b) 迴圈語句
while 條件 do
迴圈體
end while;
迴圈的提前終止:
leave:終止整個迴圈(相當於c語言中的break)
iterate:終止本次迴圈,進入下一次迴圈(作用相當於c語言中的continue)
注意:使用leave和iterate時,需要給迴圈加一個標籤,利用標籤來終止(相當於c語言中的goto語句需要標號)
標籤:while
end while 標籤;
例子1:
drop function if exists func2;
delimiter $$
create function func2() returns int
begin
set @i = 1;
set @sum = 0;
while @i <= 10 do
set @sum = @sum + @i;
set @i = @i + 1;
end while;
return @sum;
end
$$
delimiter ;
例子2:
drop function if exists func;
delimiter $$
create function func() returns int
begin
set @i = 1;
set @sum = 0;
w:while @i <= 10 do
if @i = 5 then
leave w;
end if;
set @sum = @sum + @i;
set @i = @i + 1;
end while w;
return @sum;
end
$$
delimiter ;
例子3:
drop function if exists func;
delimiter $$
create function func() returns int
begin
set @i = 1;
set @sum = 0;
w:while @i <= 10 do
if @i = 5 then
set @i = @i + 1;
iterate w;
end if;
set @sum = @sum + @i;
set @i = @i + 1;
end while w;
return @sum;
end
$$
delimiter ;
說明:即使在函數內部聲明的變數,如果使用@變數 形式,也是全域變數,在函數外部也可以訪問。要想使用局部變數,有以下兩種方法:
1.函數參數: 格式: 參數名 類型
2.函式宣告局部變數: 使用declare聲明局部變數
declare i int default 0; (局部變數沒有@符號)
例1(使用參數):
drop function if exists sayHello2;
delimiter $$
create function sayHello2(user_name varchar(10)) returns varchar(20)
begin
return concat('hello ',user_name);
end
$$
delimiter ;
還可以像下面一樣使用函數:
select id,sayHello2(userName) from student;
例2(使用declare聲明局部變數):
drop function if exists mySum;
delimiter $$
create function mySum() returns int
begin
declare i int default 1;
declare sum int default 0;
while i<=100 do
set sum=sum+i;
set i=i+1;
end while;
return sum;
end
$$
delimiter ;
應用:
1.預存程序
2.儲存函數
3.觸發器