Oracle 基礎知識入門


前記:

近來項目用到Oracle數據庫,大學學了點,后面基本忘記得差不多了,雖然基本語法跟sql 差不多,但是oracle知識是非常多的。

這里簡單說點基礎知識,希望后面補上更多的關於ORacle知識博客。入門的朋友可以看看,高手就可以繞過了。

 

不曉得你們用的什么工具,我用的Toad。用起來還是不錯的。

 

第一部分,創建數據,

 

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的命令

  

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)輸出每行的拖尾空格,缺省為off

上面的命令最好自己親自動手測試下。因為我發現自己測試是跟下面的鏈接,其他前輩有出入。

所以自己動手去實踐下比較好。

對於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 水很深,希望再接再厲.

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM