PL SQL note.



大小寫不敏感。


打印單引號時,需加多一個單引號作轉義符。即連用兩個單引號纔可以打印一個。
    Begin dbms_output.put_line('I''m leaning');
    End;


註釋:
單行註釋:由兩個連字符開始,到行尾。
   --Available 
多行註釋:由“/*”開頭,“*/” 結尾。
如果在一行代碼還沒有寫完前(指分號結束此語句之前),一定要使用多行註釋格式。
SQL中的一行代碼,不是按手寫的格式定義的,而是按計算機解釋格式(沒有分號終止前,即使按了回車也都解釋爲一行) 




      PL/SQL 塊語法
[DECLARE]
        ---declaration statements
BEGIN
        ---executable statements
[EXCEPTION]
        ---exception statements
END




變量聲明:Declare
    變量名  Type [Constant] [Not Null] [:=value];
“:=”是賦值,而“=”是比較語句。


數據類型:
        %Type       可以取出某字段的類型;如:s_emp.last_name%Type  --相當於Varchar2。
        %Rowtype    返回一整行的記錄類型;如:s_emp%Rowtype       --這得注意各字段的順序。


數字型:
Number:
Binary_Integer:


BoolLean類型:只有 True 和 False 兩類。
        其中,Dull = False。


程序執行順序的類型可分三種:
順序
選擇
        If ... Then ...
        Elsif  ... Then ...
        Else ...
        End If;
循環
一、LOOP循環:
  1.   Loop ... ;  Exit When boolean_expr ; End Loop;
  2.   Loop  If boolean_expr  Then  Exit;  End If;
            ...;  End Loop;


二、While循環:
       While  boolean_expression
          Loop  ...  End Loop;
        其中boolean_expression 值爲False則立即退出循環。免另外寫結束條件。
        可以使用Exit或Exit When 語句終止循環處理。


三、For循環:
       For loop_count IN [Reverse] low_bound..high_bound
             Loop   ...;   End Loop;
        免聲明,默認loop_count爲數值型。簡化結束條件。
        IN Reverse 表示倒過來,由大值自減到小值
        例:        For cnt  IN  1..5  Loop  ...;   End Loop;        /*由1到5*/






常用PLSQL語句:
    DBMS_OUTPUT.PUT_LINE('v_Num_3 = ' || v_Num_3); --文件中要打印一些內容
    set serveroutput on; --使終端可打印出文件的結果; 使輸出無效:set serveroutput off;


MySQL 常用語句:
終端登錄: mysql -u username -p password  --進入本機的mysql;沒設密碼就留空
    執行腳本: mysql -u 用戶名 -密碼 </.../xxx.sql 
執行腳本2:先登錄,再 source /.../xxx.sql
    show variables like '%char%';  --查看數據庫的字符集;utf8或gbk的則可支持中文
    show databases; --顯示所有數據庫目錄
    use 數據庫名; --進入某個數據庫 (可以有很多個,我把這些數據庫看作目錄,這點不同於oracle) 
    show tables; --顯示此目錄下各表格的名字
    desc 表名; --我們可以查看某個表中的數據類型結構
    create database 數據庫名;  --建數據庫目錄
    drop database 數據庫名;  --刪除數據庫整個目錄
    create table 表名;  --建表(同oracle) 
    drop table 表名; --連刪表(刪多個表用逗號隔開) 






Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for 'help'.
clear     (\c) Clear command.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Donot write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Donot show warnings after every statement.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章