Oracle資料庫(三)表操作,串連查詢,分頁

來源:互聯網
上載者:User

標籤:cache   new   join   eve   let   串連   解決   acl   產生   

複製表

--複製表create table new_table as select * from Product
--複製表結構不要資料create table new_table as select * from Product where 1=2

在where後面跟一個不成立的條件,就會僅複製表的結構而不複製表的內容。

刪除表

--刪除表delete table new_table--刪除表,無法找回truncate table new_table

序列

序列(SEQUENCE)其實是序號產生器,可以為表中的行自動產生序號,產生一組等間隔的數值(類型為數字)。其主要的用途是產生表的主索引值,可以在插入語句中引用,也可以

通過查詢檢查當前值,或使序列增至下一個值。

 使用語句建立序列

----建立序列create sequence user_seqincrement by 1 start with 1nomaxvaluenominvaluenocache

多表查詢

select * from p_emp e ,p_dept d where e.deptno=d.deptno

 

笛卡爾積

 笛卡爾積在sql中實現的方式是交叉串連,所有串連方式都會先產生臨時笛卡爾積表,笛卡爾積是關係代數的一個概念,表示兩個表中每一行資料任意組合。

簡單來說,就是兩個表不加條件限制的進行串連,出現的資料行數是兩個表資料行數的乘積。

內串連

select * from p_emp e ,p_dept d where e.deptno=d.deptno

內串連的局限性:如果有空值,查詢結果可能會有缺失。

解決辦法:

以一個表為基準進行外連結:

--左外連結select * from p_emp e left join p_dept d on e.deptno=d.deptno

或者使用 +  符號

select * from p_emp e, p_dept d where e.deptno=d.deptno(+)

 查詢使用者的表

--查詢所有表select * from user_tables

自串連

有些情況可能會遇到,將一個表的相同或者不同列的資料進行比較,需要將一個表來進行當做兩個表進行自串連,進而比較其中的資料再進行查詢

--自串連select e1.ename,e2.ename from p_emp e1,p_emp e2 where e1.empno=e2.mgr

層次查詢

oracle中的select語句可以用START WITH...CONNECT BY PRIOR子句實現遞迴查詢,connect by 是結構化查詢中用到的,其基本文法是:

select ... from <TableName>

where <Conditional-1>

start with <Conditional-2>

connect by <Conditional-3>

;

<Conditional-1>:過濾條件,用於對返回的所有記錄進行過濾。

<Conditional-2>:查詢結果重起始根結點的限定條件。

<Conditional-3>:串連條件

--層次查詢select e.*,level from p_emp e connect by prior e.empno=e.mgr start with e.ename=‘KING‘ order by level

偽列:
level
rownum

rownum是oracle系統順序分配為從查詢返回的行的編號,返回的第一行分配的是1,第二行是2,依此類推,這個偽欄位可以用於限制查詢返回的總行數,

而且rownum不能以任何錶的名稱作為首碼。

需要注意的是:如果按照主鍵排序,rownum的順序會跟著變化,如果不是按照主鍵排序,rownum不會變。

--查詢前十列資料select e.*,rownum from p_emp e where rownum <=10--6-10列資料,巢狀查詢select * from (select e.*,rownum rownu from p_emp e where rownum <=10) r where r.rownu >5 

Oracle資料庫(三)表操作,串連查詢,分頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.