前記:html
近來項目用到Oracle數據庫,大學學了點,後面基本忘記得差很少了,雖然基本語法跟sql 差很少,可是oracle知識是很是多的。sql
這裏簡單說點基礎知識,但願後面補上更多的關於ORacle知識博客。入門的朋友能夠看看,高手就能夠繞過了。數據庫
不曉得大家用的什麼工具,我用的Toad。用起來仍是不錯的。oracle
第一部分,建立數據,app
create table student ( sName varchar(20) primary key, sAge int, sEmail varchar(100), sPhone varchar(20), sAddress varchar(100) ) insert into student values('Jack',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Jack1',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Jack2',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Jack3',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Jack54',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Jack6',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Jack7',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Jack21',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Rose',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('rose1',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('rose2',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('rose4',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Adi',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Aditt',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Niyes',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Jassic',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Carken',21,'dfdf@qq.com','2134343','Singapore'); insert into student values('Donview',21,'dfdf@qq.com','2134343','Singapore'); commit;
執行其餘都會報錯的.工具
第二部分,看幾個關於Spool的命令oop
spool c:/test.log; --將下面的查詢結果放在這個文件中,若是文件不存在,會自動建立 select * from student; spool off; --完成spool --執行後,你就能夠去相應的目錄去查看Log了。 --再看一個例子 set feedback on; --若是這裏設置爲off,則看不到18 rows selected set termout on; --若是這裏設置爲off,則看不到結果 set echo on; --這裏看到SQL>命令,就是這個開啓的緣由 spool c:/test.log; select * from student; spool off; exit;
結果(只顯示了一部分):測試
spool經常使用的設置
set echo on; //顯示start啓動的腳本中的每一個sql命令,缺省爲off
set feedback on; //回顯本次sql命令處理的記錄條數,缺省爲on
set heading off; //輸出域標題,缺省爲on
set pagesize 0; //輸出每頁行數,缺省爲24,爲了不分頁,可設定爲0。
set termout on; //顯示腳本中的命令的執行結果,缺省爲on
set trimout on; //去除標準輸出每行的拖尾空格,缺省爲off
set trimspool on; //去除重定向(spool)輸出每行的拖尾空格,缺省爲offfetch
上面的命令最好本身親自動手測試下。由於我發現本身測試是跟下面的連接,其餘前輩有出入。.net
因此本身動手去實踐下比較好。
對於spool的相關了解,查看下面的這個連接
http://blog.sina.com.cn/s/blog_6bccf0360101hzsh.html
http://blog.csdn.net/shangyang326/article/details/3304621
第三部分,幾個oracle 腳本知識入門。
主要查看下面這兩個連接:
http://www.oracle.com/technetwork/issue-archive/2013/13-mar/o23plsql-1906474.html
http://docs.oracle.com/cd/B10500_01/appdev.920/a96624/a_samps.htm
1,咱們想查看姓名=Jack 的信息,這裏只有一條記錄。
set serveroutput on; --要想看到打印結果,則必須開啓這個命令。 DECLARE l_name varchar(100); --l_name student.sName%TYPE; 相同的效果,推薦使用這個。 BEGIN SELECT sName INTO l_name FROM student WHERE sName = 'Jack'; DBMS_OUTPUT.put_line ('find the name: '||l_name); END; 結果: find the name: Jack PL/SQL procedure successfully completed.
%RowType 的使用,獲取某行的數據類型。
set serveroutput on; DECLARE rowData student%ROWTYPE; BEGIN SELECT * INTO rowData FROM student WHERE sName = 'Jack'; DBMS_OUTPUT.put_line ('find the name: '||rowData.sName); DBMS_OUTPUT.put_line ('find the age: '||rowData.SAGE); DBMS_OUTPUT.put_line ('find the email: '||rowData.sEmail); DBMS_OUTPUT.put_line ('find the phone: '||rowData.sPhone); DBMS_OUTPUT.put_line ('find the address: '||rowData.sAddress); END;
結果:
find the name: Jack find the age: 22 find the email: dfdf@qq.com find the phone: 2134343 find the address: Singapore PL/SQL procedure successfully completed.
關於Type的用法,查看
http://blog.csdn.net/chen_linbo/article/details/6367871
2, 查看姓名包含rose的信息(包含多條記錄)。
set serveroutput on; DECLARE cursor name_rose_cur is select sName from student where upper(sName) like upper('%rose%'); l_name student.sName%TYPE; BEGIN open name_rose_cur; Loop fetch name_rose_cur into l_name; exit when name_rose_cur%NOTFOUND; DBMS_OUTPUT.put_line ('find the name: '||l_name); end loop; close name_rose_cur; END;
結果:
find the name: Rose find the name: rose1 find the name: rose2 find the name: rose4 PL/SQL procedure successfully completed.
一樣的功能能夠用For循環來實現。
set serveroutput on; DECLARE cursor name_rose_cur is select * from student where upper(sName) like upper('%rose%'); BEGIN for student_cur in name_rose_cur Loop DBMS_OUTPUT.put_line ('find the name: '||student_cur.sName); end loop; END;
這裏的結果跟上面是同樣的。
Oracle 水很深,但願再接再礪.