第四部分:觸發器的操作
什麼是觸發器?
觸發器是監聽某種情況,觸發某種操作。
創建觸發器有四元素:一個監聽地點(table)、一個監聽事件(insert、update、delete)、一個觸發時間(before、after)、一個觸發事件(insert、delete、update)。
觸發器是數據庫對象之一,與函數非常類似,需要聲明、執行。但是觸發器的執行是由事件來觸發的。
當表發生更改時,需要自動進行一些處理,比如每增加一條學生的記錄,學生的總數就需要同時改變,這時候就要執行一次計算學生總數的操作。
觸發器的操作包括創建觸發器、查看觸發器及刪除觸發器。
創建有一條執行語句的觸發器:
create trigger trigger_dairytime before insert on product for each row insert into dairy values(NULL,'product',now());
這句話的意思是 對錶product做任何操作都會觸發觸發器trigger_dairytime,使得在操作之前會先對錶dairy插入一條當前時間的語句。for each row是指任何一個記錄操作都會觸發觸發器。
創建完之後驗證觸發器是否起作用:
insert into product values(1,'apple',1.5);//向product插入數據
再查看dairy是否有插入一行時間信息:select * from dairy;
創建包含多條執行語句的觸發器:
將$$設置爲語句結束符:delimiter $$;
創建:create trigger trigger_dairytime2 after insert on product for each row
-> begin
-> insert into dairy values(NULL,'product',now());
-> insert into dairy values(NULL,'product',now());
-> end
-> $$
然後再輸入:delimiter;//將結束符號還原爲默認結束符號“;”
然後再向product中插入數據來檢驗觸發器是否設置成功。
查看觸發器:show triggers \G;
在系統數據庫information_schema中存在一個存儲所有觸發器信息的系統表triggers,可以通過這個查詢,
select * from triggers where trigger_name='trigger_dairytime' \G;
刪除觸發器:drop trigger trigger_dairytime;
完了再用show triggers \G;來查看
第五部分:數據的操作
插入完整數據記錄:
insert into product(id,name,price) values(9,'pear2',5.0);
//字符串加單引號
或者是:
insert into product values(7,'apple2',9.0);
插入部分數據記錄:
insert into product(name,price) values('peach2',4.5);
插入多條數據記錄:
insert into product(id,name,price) values(...),(...),(...);
還可以插入另一個表的查詢結果,來實現表數據值的複製功能,這兩個表的個數和參數類型需一樣:
insert into fruits(id,name,price) select product_id,pro_name,pro_price from product;
//向fruits表中插入product表中的信息
更新特定數據記錄:
update product set price=6.5 where name='pear2';
//將product表中name爲pear2的price更新爲6.5
update product set price=5.5 where id<5;
刪除特定數據記錄:
delete from product where name='pea2';
delete from product;//把這裏面的數據全刪了
第六部分:單表數據記錄查詢
查詢字段數據:
select id,name,price from product;//字段之間也可以交換順序
查詢所有字段數據:
select * from product;
加distinct 避免查詢到重複的數據:
select distinct price from product;//加關鍵字distinct,去掉重複數據
實現四則運算數據查詢:
select id,price*100 from product;
給price*100換一個可讀性字段名:
select id,price*100 salary from product;
設置顯示格式數據查詢:
select CONCAT(id,'水果的總價:',price*100) salary from product;??
條件數據查詢:
select name from product where price=5.5;
多條件的話可以加and或&&,或者是or等邏輯運算符。
帶between和and的查詢:
select name from product where price between 5 and 9;
查詢不符合範圍的數據記錄:
select name from product where price not between 5 and 9;
空或非空查詢:
select price from product where id is NULL;
select price from product where id is not NULL;
select price from product where not id is NULL;
帶關鍵字in的集合查詢:
select name from product where price in (5.5,4.5);
select name from product where price not in (5.5,4.5);
select name from product where not price in (5.5,4.5);
注意:對於關鍵字not in,如果集合中有NULL,則不會有任何查詢結果。
Lile查詢,like用來判斷字段的值是否與指定的值相匹配。Like後面可以跟完整的字符,也可以支持“_”和“%”通配符,“_”匹配單個字符,“%”可以匹配任意長度的字符。
比如:
select name from product where name like 'p%';//查詢name字段中以p開頭的name
select name from product where not name like 'p%';//查詢不以p開頭的name
查詢第二個字母爲e的name:
select name from product where name like '_e%';
也可以用not like來查詢不匹配的數據。
select name from product where name not like '%h%';
//查詢不含字母h的name
select price from product where price like '%5%';
//查詢price中包含數字5的
升序排序查詢:
select * from product order by price asc;//price字段升序查詢
不加asc也是默認的升序。
降序排序查詢:
select * from product order by price desc;//price字段降序查詢
多字段排序查詢:
將price降序排列,如果price相等再按id降序排列:
select * from product order by price desc,id desc;
Limit來限制查詢結果的數量:
- 不指定初始位置:
select * from product where price=5.5 limit 3;//限制三條記錄
- 指定初始位置:
限制從滿足條件的第二個記錄開始,顯示三條記錄:
select * from product where price=5.5 order by price limit 1,3;
limit n 等價於 limit 0,n。
統計函數查詢:
支持count()、avg()、sum()、max()、min()。統計函數都會忽略值爲NULL的數據記錄,但是不會忽略值爲0的數據記錄。
使用count():
查詢表中的數據個數:
select count(*) number from product;
查詢某個字段的個數:
select count(name) number from product;
還可以再加條件:
select count(price) number from product where price=5.5;
使用avg():
select avg(price) average from product;
還可以再加條件:
select avg(price) average from product where not price=0;//忽略掉0的情況
使用sum():
select sum(price) sumvalue from product;
使用max() min():
select max(price) maxval,min(price) minval from product;
注意:使用統計函數時,如果表中無數據,count會返回0,別的函數會返回NULL。
分組查詢:
當字段中有重複值時,可將該字段分組查詢:
select * from product group by type;
select group_concat(type) from product group by type;
//group_concat,是獲取每組中指定參數的記錄元素
顯示出每個分組中的個數和名字:
select type,group_concat(name) names,count(name) number from product group by type;
實現多個字段分組查詢:
select type,name from product group by type,name;
//首先按type分爲6組,再對每組type針對name再分組
select type,name group_concat(name) names,count(name) from product group by type,name;
//獲取每組中的記錄元素,並統計記錄數目
由type來分組,統計每組的平均price:
select type,avg(price) average from product group by type;
Having來實現條件分組查詢,跟在group by的後面。having後面跟上條件:
由type分組顯示出平均price大於5的名稱和個數:
select type,avg(price) average,group_concat(name) names,count(name) number from product group by type having avg(price)>5;