Mysql事項,視圖,函數,觸發器命令(詳解)_Mysql

來源:互聯網
上載者:User

事項開啟和使用

//修改表的引擎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事項,視圖,函數,觸發器命令(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.