Mysql事項(xiàng),視圖,函數(shù),觸發(fā)器命令(詳解)
事項(xiàng)開啟和使用
//修改表的引擎 alter table a engine=myisam; //開啟事務(wù) begin; //關(guān)閉自動提交 set autocommit=0; //扣100 update bank set money=money-100 where bid=1; //回滾,begin開始的所有sql語句操作 rollback; //開啟事務(wù) begin; //關(guān)閉自動提交 set autocommit=0; //扣100 update bank set money=money-100 where bid=1; //加100 update bank set money=money+100 where bid=2; //提交 commit;
實(shí)例操作
$dsn = "mysql:host=127.0.0.1;dbname=c58";
try {
//通過pdo連接數(shù)據(jù)庫
$pdo = new Pdo($dsn,'root','');
//把錯(cuò)誤設(shè)置成異常模式,才能try catch接收
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//設(shè)置字符集
$pdo->query("SET NAMES utf8");
//開啟事務(wù)
$pdo->query("BEGIN");
//關(guān)閉自動提交
$pdo->query("SET AUTOCOMMIT=0");
//轉(zhuǎn)賬
//扣掉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();
}
注釋:事項(xiàng)可以幫助我們更安全的操作數(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) begin select bid from bank where name=n; end $ //調(diào)用 set @name='震'$ call get_bid(@name)$ //存儲過程作業(yè) //1. 創(chuàng)建刪除班級的存儲過程 //2. 實(shí)現(xiàn)刪除班級時(shí)一并刪除此班級中的學(xué)生 //3. 調(diào)用方式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) begin delete from class where cid=id; delete from stu where cid=id; end $ set @id=1$ call del_class(@id)$ //1.in(輸出外面?zhèn)魅氲闹?,不能改變外面?zhèn)魅氲闹? create procedure a(in id int) begin select id; set id=100; end $ //2.out(不可以輸出外面?zhèn)魅氲闹担芨淖兺饷鎮(zhèn)魅氲闹? 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 $ //查看狀態(tài) show procedure status; //刪除get_bid這個(gè)存儲過程 drop procedure get_bid;
存儲函數(shù)創(chuàng)建刪除和使用
//創(chuàng)建
create function hello(s char(20) charset utf8)
returns char(50)
reads sql data
begin
return concat('hello ',s,' !');
end
$
//調(diào)用
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
begin
return (select cid from stu where sname=n);
end
$
//存儲函數(shù)可以用在sql語句中
select cname from class where cid=getcid('小貓')$
觸發(fā)器創(chuàng)建刪除和使用
//刪除班級自動觸發(fā)刪除學(xué)生 create trigger del_class_stu after delete on class for each row begin delete from stu where cid=old.cid; end $ //觸發(fā)器作業(yè) 創(chuàng)建文章表含標(biāo)題、作者、發(fā)布時(shí)間字段 如果只添加了標(biāo)題,發(fā)布時(shí)間字段自動設(shè)置為當(dāng)前時(shí)間, 作者字段設(shè)置為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ā)器是設(shè)置好當(dāng)執(zhí)行某一個(gè)行為時(shí)執(zhí)行另一個(gè)方法!
以上這篇Mysql事項(xiàng),視圖,函數(shù),觸發(fā)器命令(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mysql 命令行模式訪問操作mysql數(shù)據(jù)庫操作
這篇文章主要介紹了Mysql 命令行模式訪問操作mysql數(shù)據(jù)庫操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
從MySQL5.7平滑升級到MySQL8.0數(shù)據(jù)庫的最佳實(shí)踐教程
這篇文章主要給大家介紹了關(guān)于從MySQL5.7平滑升級到MySQL8.0數(shù)據(jù)庫的最佳實(shí)踐,升級MySQL版本需要進(jìn)行一系列操作,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
MySQL IS NULL空值查詢的實(shí)現(xiàn)
MySQL 提供了?IS NULL?關(guān)鍵字,用來判斷字段的值是否為空值,本文主要介紹了MySQL IS NULL空值查詢的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
登錄MySQL時(shí)出現(xiàn)SSL connection error: unknown
這篇文章主要介紹了登錄MySQL時(shí)出現(xiàn)SSL connection error: unknown error number錯(cuò)誤的解決方法,文中通過圖文結(jié)合的形式講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-12-12

