自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實(shí)現(xiàn)MySQL觸發(fā)器的實(shí)際操作步驟

數(shù)據(jù)庫(kù) MySQL
我們今天主要向大家描述的是MySQL觸發(fā)器的實(shí)際操作步驟、以及存儲(chǔ)過(guò)程等相關(guān)實(shí)際操作步驟,以下就是文章的具體內(nèi)容描述。

以下的文章主要講述的是實(shí)現(xiàn)MySQL觸發(fā)器的實(shí)際操作步驟、以及存儲(chǔ)過(guò)程、自定義函數(shù)與視圖的簡(jiǎn)單示例介紹,如果你對(duì)MySQL觸發(fā)器的實(shí)際操作步驟以及存儲(chǔ)過(guò)程的實(shí)際操作感興趣的話,你就可以瀏覽以下的文章了,示例實(shí)現(xiàn)如下效果:

test數(shù)據(jù)庫(kù)有userinfo用戶信息表 和userinfolog用戶信息日志表

1.建立一個(gè)userinfo表新增記錄時(shí)的觸發(fā)器 將新增日志加入到userinfolog

2.建立一個(gè)向userinfo表新增記錄的存儲(chǔ)過(guò)程

3.根據(jù)userinfo表的出生日期字段 我們將建立一個(gè)簡(jiǎn)單算得年齡的自定義函數(shù)

4.創(chuàng)建一個(gè)userinfo的視圖 調(diào)用年齡函數(shù)

準(zhǔn)備相關(guān)表

  1. mysql> use test;  
  2. mysql> create table userinfo(userid int,username varchar(10),userbirthday date);  
  3. mysql> create table userinfolog(logtime datetime,loginfo varchar(100));  
  4. mysql> describe userinfo; 

1.MySQL觸發(fā)器的實(shí)現(xiàn):

  1. mysql> delimiter |  
  2. mysql> create trigger beforeinsertuserinfo  
  3. -> before insert on userinfo  
  4. -> for each row begin  
  5. -> insert into userinfolog values(now(),CONCAT(new.userid,new.username));  
  6. -> end;  
  7. -> |  
  8. mysql> delimiter ;  
  9. mysql> show triggers; 

2.存儲(chǔ)過(guò)程

  1. mysql> delimiter //  
  2. mysql> create procedure spinsertuserinfo(  
  3. -> puserid int,pusername varchar(10)  
  4. -> ,puserbirthday date  
  5. -> )  
  6. -> begin  
  7. -> insert into userinfo values(puserid,pusername,puserbirthday);  
  8. -> end;  
  9. -> //  
  10. mysql> show procedure status like 'spinsertuserinfo';  
  11. mysql> call spinsertuserinfo(1,'zhangsan',current_date);  
  12. mysql> select * from userinfo; 

3.自定義函數(shù)

  1. mysql> update userinfo  
  2. -> set userbirthday='2000.01.01' 
  3. -> where userid='1';  
  4. mysql> drop function if exists fngetage;  
  5. mysql> delimiter //  
  6. mysql> create function fngetage(pbirthday date)  
  7. -> returns integer  
  8. -> begin  
  9. -> return year(now()) - year(pbirthday);  
  10. -> end  
  11. -> // 

4.視圖

  1. mysql> create view viewuserinfo  
  2. -> as select * ,fngetage(userbirthday) as userage from userinfo;  
  3. mysql> select * from viewuserinfo; 

清除日志記錄

  1. mysql> truncate table userinfolog;  
  2. mysql> delete from userinfolog; 

以上的相關(guān)內(nèi)容就是對(duì)MySQL觸發(fā)器的介紹,望你能有所收獲。

責(zé)任編輯:佚名 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-05-19 11:25:46

MySQL觸發(fā)器

2010-06-13 09:46:44

MySQL5觸發(fā)器

2010-05-26 14:06:44

MySQL查詢

2010-05-12 17:15:57

2010-05-12 13:45:25

Mysql 復(fù)制設(shè)置

2010-03-12 15:29:19

Pythonexe

2010-05-13 16:21:58

2010-05-28 14:42:00

MySQL使用備忘

2010-06-09 11:40:32

MySQL SQL 語(yǔ)

2010-05-27 10:35:09

查詢MySQL數(shù)據(jù)

2010-06-01 15:54:46

MySQL-pytho

2010-05-17 16:52:14

MySQL limit

2010-05-20 15:39:14

MySQL支持中文

2010-04-20 11:06:33

Oracle索引

2010-05-13 17:00:32

MySQL啟動(dòng)方法

2010-12-07 09:20:44

MySQL limit

2010-05-27 14:35:25

MySQL批量導(dǎo)入

2010-06-10 09:05:37

MySQL自動(dòng)遞增字段

2010-06-12 10:41:23

MySQL修改數(shù)據(jù)

2010-05-28 18:16:43

MySQL 操作日志
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)