事項開啟和使用
| //修改表的引擎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; | 
存儲函數創建刪除和使用
新聞熱點
疑難解答