MySQL資料庫的儲存結構

來源:互聯網
上載者:User

標籤:

--把若干條sql語句封裝起來,起個名字,叫做過程,也是沒有傳回值的函數--把這個過程儲存在資料庫中->預存程序

 

--預存程序的建立過程create procedure proceduceName()beginsql 語句end$--查看show procedure status \G--調用call procedureName()$--聲明變數declare age int default 18;--運算set age:=age+10;--改變邊界delimiter $--例子create procedure p1()begindeclare age int default 18;set age:=age+10;select age;end$--用call p1()$調用時出現 age    28

 

--建立表create table test(id int,name varchar(30) not null default ‘‘,age int,content varchar(40))engine myisam charset utf8$--插入資料insert into test values (1,‘zhangsan‘,13,‘學習好‘),(2,‘lisi‘,15,‘智慧的化身‘),(2,‘wangwu‘,14,‘不起眼‘)$

 

--預存程序儲存sql語句create procedure p2()beginselect content from test where age=15;end$--預存程序是可以編程的--意味著可以使用變數,運算式,控制結構完成複雜的功能--if/else語句的使用if 條件 then語句;else if 條件 then語句;else 語句;end if;create procedure p3()begindeclare age int default 18;if age>=18 thenselect ‘你已經成年了‘;elseselect ‘未成年‘;end if;end$--while 迴圈while 條件 do語句;end while;--括弧裡可以聲明參數--參數類型 [in/out/inout] 參數名  參數類型create procedure p4(agee int)beginselect name from test where age=agee;end$in 表示輸入參數 傳參out 表示輸出參數 傳變數名 向外傳值 要設定初始化值inout 輸入參數執行後再輸出 傳入一個聲明過的變數名create procedure p5(in age int)beginselect age as ‘你幾歲了‘; end$--out 參數,要在procedure 內部初始化參數create procedure p6(out result int)begindeclare i int default 1;set result:=0;while i<=100 doset result:=result+i;set i:=i+1;end while;end$--調用call p6(@num)$select @num$--inout 參數create procedure p7(inout age int)beginset age:=age+10;select age as ‘10年後你幾歲了‘;end$set @age=10$call p7(@age)$select @age$--case語句case 變數名when 值1 then 操作1;when 值2 then 操作2;...else 操作n;end case;--repeat 迴圈repeat 動作陳述式until 條件 end repeat;

 

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.