Mysql事项,视图,函数,触发器命令
生活随笔
收集整理的這篇文章主要介紹了
Mysql事项,视图,函数,触发器命令
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
事項開啟和使用
//修改表的引擎 alter table a engine=myisam; //開啟事務 begin; //關閉自動提交 set autocommit=0; //扣100 update bank set money=money-100 where bid=1; //回滾,begin開始的所有sql語句操作 rollback;//開啟事務 begin; //關閉自動提交 set autocommit=0; //扣100 update bank set money=money-100 where bid=1; //加100 update bank set money=money+100 where bid=2; //提交 commit;實例操作
$dsn = "mysql:host=127.0.0.1;dbname=c58"; try {//通過pdo連接數(shù)據(jù)庫$pdo = new Pdo($dsn,'root','');//把錯誤設置成異常模式,才能try catch接收$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);//設置字符集$pdo->query("SET NAMES utf8");//開啟事務$pdo->query("BEGIN");//關閉自動提交$pdo->query("SET AUTOCOMMIT=0");//轉賬//扣掉100$pdo->exec('UPDATE bank SET money=money-100 WHERE bid=1');//加上100$pdo->exec('UPDATE bank SET money=money+100 WHERE bid=2');//提交$pdo->query('COMMIT'); } catch (PDOException $e) {$pdo->query('ROLLBACK');echo $e->getMessage(); }注釋:事項可以幫助我們更安全的操作數(shù)據(jù)
視圖的創(chuàng)建刪除和使用
//1.創(chuàng)建視圖 create view bankview as select bid,bname from bank; //2.查看視圖 show table status where comment='VIEW'; //3.修改視圖 alter view bankview as select bid from bank; //4.刪除視圖 drop view bankview;注釋:視圖是一個虛擬表,可以查詢和設置權限
存儲過程的創(chuàng)建刪除查詢和使用
//更變邊界符 \d $//創(chuàng)建存儲過程 create procedure get_bid(inout n char(20) charset utf8) beginselect bid from bank where name=n; end $//調用 set @name='字符'$ call get_bid(@name)$//存儲過程 //1. 創(chuàng)建刪除班級的存儲過程 //2. 實現(xiàn)刪除班級時一并刪除此班級中的學生 //3. 調用方式call del_class(1); //創(chuàng)建表 create table class(cid int unsigned primary key auto_increment,cname char(20) not null default '' ); create table stu(sid int unsigned primary key auto_increment,sname char(20) not null default '',cid int unsigned not null default 0 ); \d $ create procedure del_class(inout id smallint) begindelete from class where cid=id;delete from stu where cid=id; end $ set @id=1$ call del_class(@id)$//1.in(輸出外面?zhèn)魅氲闹?#xff0c;不能改變外面?zhèn)魅氲闹? create procedure a(in id int) beginselect id;set id=100; end $//2.out(不可以輸出外面?zhèn)魅氲闹?#xff0c;能改變外面?zhèn)魅氲闹? create procedure b(out id int) beginselect id;set id=100; end $//3.inout(綜合上述兩種情況) create procedure insert_data(in num int) beginwhile num > 0 doinsert into class set cname=num;set num = num - 1;end while; end $//查看狀態(tài) show procedure status;//刪除get_bid這個存儲過程 drop procedure get_bid;存儲函數(shù)創(chuàng)建刪除和使用
//創(chuàng)建 create function hello(s char(20) charset utf8) returns char(50) reads sql data beginreturn concat('hello ',s,' !'); end $//調用 select hello('hdw')$ +--------------+ | hello('hdw') | +--------------+ | hello hdw ! | +--------------+//刪除 drop function hello$//創(chuàng)建存儲函數(shù) create function getcid(n char(20) charset utf8) returns int reads sql data beginreturn (select cid from stu where sname=n); end $ //存儲函數(shù)可以用在sql語句中 select cname from class where cid=getcid('小貓')$觸發(fā)器創(chuàng)建刪除和使用
//刪除班級自動觸發(fā)刪除學生 create trigger del_class_stu after delete on class for each row begindelete from stu where cid=old.cid; end $//觸發(fā)器 創(chuàng)建文章表含標題、作者、發(fā)布時間字段 如果只添加了標題,發(fā)布時間字段自動設置為當前時間, 作者字段設置為123網(wǎng) \d $ create trigger this_name before insert on this_table for each row begin if new.uname is null then set new.uname='123'; end if; if new.timer is null then set new.timer=unix_timestamp(now()); end if; end $//查詢已有觸發(fā)器 show triggers;注釋:觸發(fā)器是設置好當執(zhí)行某一個行為時執(zhí)行另一個方法!
總結
以上是生活随笔為你收集整理的Mysql事项,视图,函数,触发器命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求最大值c语言常用方法,c语言如何求最大
- 下一篇: 网易云课堂解析_使用SQL分析网易云课堂