事項開啟和使用
//修改表的引擎alter table a engine=myisam;//開啟事務begin;//關閉自動提交set autocommit=0;//扣100update bank set money=money-100 where bid=1;//回滾,begin開始的所有sql語句操作rollback;//開啟事務begin;//關閉自動提交set autocommit=0;//扣100update bank set money=money-100 where bid=1;//加100update bank set money=money+100 where bid=2;//提交commit;
實例操作
$dsn = "mysql:host=127.0.0.1;dbname=c58";try { //通過pdo連接數據庫 $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();}
注釋:事項可以幫助我們更安全的操作數據
視圖的創建刪除和使用
//1.創建視圖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;
存儲過程的創建刪除查詢和使用
//更變邊界符
//更變邊界符/d $//創建存儲過程create procedure get_bid(inout n char(20) charset utf8)begin select bid from bank where name=n;end$//調用set @name='震'$call get_bid(@name)$//存儲過程作業//1. 創建刪除班級的存儲過程//2. 實現刪除班級時一并刪除此班級中的學生//3. 調用方式call del_class(1);//創建表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)begin delete from class where cid=id; delete from stu where cid=id;end$set @id=1$call del_class(@id)$//1.in(輸出外面傳入的值,不能改變外面傳入的值)create procedure a(in id int)begin select id; set id=100;end$//2.out(不可以輸出外面傳入的值,能改變外面傳入的值)create procedure b(out id int)begin select id; set id=100;end$//3.inout(綜合上述兩種情況)create procedure insert_data(in num int)begin while num > 0 do insert into class set cname=num; set num = num - 1; end while;end$//查看狀態show procedure status;//刪除get_bid這個存儲過程drop procedure get_bid;
存儲函數創建刪除和使用
//創建create function hello(s char(20) charset utf8)returns char(50)reads sql databegin return concat('hello ',s,' !');end$//調用select hello('hdw')$+--------------+| hello('hdw') |+--------------+| hello hdw ! |+--------------+//刪除drop function hello$//創建存儲函數create function getcid(n char(20) charset utf8)returns intreads sql databegin return (select cid from stu where sname=n);end$//存儲函數可以用在sql語句中select cname from class where cid=getcid('小貓')$
觸發器創建刪除和使用
//刪除班級自動觸發刪除學生create trigger del_class_stu after delete on classfor each rowbegin delete from stu where cid=old.cid;end$//觸發器作業創建文章表含標題、作者、發布時間字段如果只添加了標題,發布時間字段自動設置為當前時間,作者字段設置為123網/d $create trigger this_name before insert on this_table for each rowbeginif new.uname is null thenset new.uname='123';end if;if new.timer is null thenset new.timer=unix_timestamp(now());end if;end$//查詢已有觸發器show triggers;
注釋:觸發器是設置好當執行某一個行為時執行另一個方法!
以上這篇Mysql事項,視圖,函數,觸發器命令(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答