oracle基本查詢操作子查詢用法實例分析
本文實例講述了oracle基本查詢操作子查詢用法。分享給大家供大家參考,具體如下:
一、子查詢語法
SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table);
子查詢在主查詢之前一次執(zhí)行完成。
子查詢的結(jié)果被主查詢使用。
select ename from emp where sal > (select sal from emp where ename='SCOTT');
(*注意:子查詢要包含在括號內(nèi),將子查詢放在比較條件的右側(cè)。單行操作符對應(yīng)單行子查詢,多行操作符對應(yīng)多行子查詢。)
單行子查詢,只返回一行,使用單行比較符(> = < >= <= != <>)
--子查詢中使用組函數(shù) select ename,sal from emp where sal=(select min(sal) from emp); --子查詢中的having子句 --首先執(zhí)行子查詢 --向主查詢中的having子句返回結(jié)果 select deptno, min(sal) from emp group by deptno having min(sal) > (select min(sal) from emp);
多行子查詢,返回多行,使用多行比較符(IN ANY ALL)
--查詢比部門10里任意一個人工資高的員工信息 select ename, sal from emp where sal > any (select sal from emp where deptno = 10); --查詢比部門20里所有人工資高的員工信息 select ename, sal from emp where sal > all (select sal from emp where deptno = 20); --查詢不是老板的員工信息 select ename from emp where empno not in(select mgr from emp);
二、集合運算
并集
UNION運算符返回兩個集合去掉重復(fù)元素后的所有記錄。
UNION ALL 返回兩個集合的所有記錄,包括重復(fù)的。
交集
INTERSECT 運算符返回同時屬于兩個集合的記錄
--返回工資在500-1000和900-1200的員工信息 select ename, sal from emp where sal between 500 and 1000 intersect select ename, sal from emp where sal between 900 and 1200;
差集
MINUS 返回屬于第一個集合,但不屬于第二個集合的記錄。
--返回工資屬于500-1000,但不屬于900-1200的員工信息 select ename, sal from emp where sal between 500 and 1000 minus select ename, sal from emp where sal between 900 and 1200;
集合使用的注意事項
1、select語句中參數(shù)類型和個數(shù)保持一致。
2、可以使用括號改變集合執(zhí)行的順序。
3、如果有order by,必須放到最后一句查詢語句后。
4、集合運算采用第一個語句的表頭作為表頭。
三、數(shù)據(jù)操作語言
插入數(shù)據(jù)
INSERT INTO table [(column [,column...])] VALUES (value [,value...]);
insert into dept(deptno,dname,loc) values(50,'test','test');
從其他表中拷貝數(shù)據(jù)
insert into dept(deptno, dname, loc) select 60, dname, loc from dept where deptno = 10;
更新數(shù)據(jù)
UPDATE table SET column=value [, column=value, ...] [WHERE codition]
--更新一條數(shù)據(jù) update emp set sal=sal+100 where empno=7369;
--update使用子查詢 update emp set sal = (select max(sal) from emp) where empno = (select empno from emp where sal = (select min(sal) from emp));
刪除數(shù)據(jù)
DELETE [FROM] table [WHERE condition];
--刪除一條數(shù)據(jù) delete from dept where deptno=60;
delete和truncate
1、都是刪除表中的數(shù)據(jù)。
2、delete操作可以rollback,可以閃回。
3、delete可能產(chǎn)生碎片,并且不釋放空間。
4、truncate清空表。
四、數(shù)據(jù)庫事務(wù)
數(shù)據(jù)庫事務(wù)由以下的部分組成:
1、一個或多個DML語句
2、一個DDL數(shù)據(jù)定義語句
3、一個DCL數(shù)據(jù)控制語句
以第一個DML語句的執(zhí)行作為開始
以下面的其中之一作為結(jié)束:
顯示結(jié)束:commit rollback
隱式結(jié)束(自動提交):DDL語句,DCL語句,exit(事務(wù)正常退出)
隱式回滾(系統(tǒng)異常終了):關(guān)閉窗口,死機,掉電
commit和rollback語句的優(yōu)點
1、確保數(shù)據(jù)完整性。
2、數(shù)據(jù)改變被提交之前預(yù)覽。
3、將邏輯上相關(guān)的操作分組。
回滾到保留點
使用savepoint語句在當前事務(wù)中創(chuàng)建保存點。
使用rollback to savepoint語句回滾到創(chuàng)建的保存點。
update emp set sal=sal+100 where empno=7369; savepoint update_empno7369; delete from emp where empno=7369; rollback to update_empno7369;
五、創(chuàng)建和管理表
常見的數(shù)據(jù)庫對象
如下:
表 基本的數(shù)據(jù)存儲集合,由行和列組成。
視圖 從表中抽出的邏輯上相關(guān)的數(shù)據(jù)集合。
序列 提供有規(guī)律的數(shù)值。
索引 提高查詢的效率。
同義詞 給對象起別名。
創(chuàng)建表
CREATE TABLE [schema.]table (column datatype [DEFAULT expr][, ...]);
create table test( id number(12), name varchar2(32));
通過子查詢創(chuàng)建表
CREATE TABLE table [(column, column...)] AS subquery;
create table test2 as select empno,ename from emp where sal>1000;
修改表
--添加列 ALTER TABLE table ADD (column datatype [DEFAULT expr] [, column datatype] ...); --添加info列 alter table test add (info varchar2(256) default ''); --修改列 ALTER TABLE table MODIFY (column datatype [DEFAULT expr] [, column datatype] ...); --修改info列 alter table test modify (info varchar2(64) default ''); --刪除列 ALTER TABLE table DROP column (column); --刪除info列 alter table test drop column info; --修改列名 ALTER TABLE table rename column old_column_name to new_column_name; --修改name列名 alter table test rename column name to name2;
刪除表
1、數(shù)據(jù)和結(jié)構(gòu)都被刪除
2、所有正在運行的相關(guān)事物被提交
3、所有相關(guān)索引被刪除
4、DROP TABLE語句不能回滾,但是可以閃回。
drop table test;
改變對象的名稱
rename dept to newDept;
清空表
1、刪除表中所有數(shù)據(jù)。
2、釋放表的存儲空間。
3、truncate不能回滾。
truncate table test;
更多關(guān)于Oracle相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Oracle常用函數(shù)匯總》、《Oracle日期與時間操作技巧總結(jié)》及《php+Oracle數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》
希望本文所述對大家Oracle數(shù)據(jù)庫程序設(shè)計有所幫助。
相關(guān)文章
Oracle如何批量將表中字段名全轉(zhuǎn)換為大寫(利用簡單存儲過程)
這篇文章主要給大家介紹了關(guān)于Oracle如何批量將表中字段名全轉(zhuǎn)換為大寫的相關(guān)資料,主要利用的就是一個簡單的存儲過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11Oracle 11g Release (11.1) 索引底層的數(shù)據(jù)結(jié)構(gòu)
本文介紹關(guān)于 Oracle 索引的結(jié)構(gòu)。大概了解 Oracle 索引底層的數(shù)據(jù)結(jié)構(gòu),從而更好地理解 Oracle 索引對增、刪、改、查的性能2012-11-11DBA_Oracle Startup / Shutdown啟動和關(guān)閉過程詳解(概念)(對數(shù)據(jù)庫進行各種維護操作)
對于大多數(shù)Oracle DBA來說,啟動和關(guān)閉Oracle數(shù)據(jù)庫最常用的方式就是在命令行方式下的Server Manager。從Oracle 8i以后,系統(tǒng)將Server Manager的所有功能都集中到了SQL*Plus中,也就是說從8i以后對于數(shù)據(jù)庫的啟動和關(guān)閉可以直接通過SQL*Plus來完成2014-08-08簡單說明Oracle數(shù)據(jù)庫中對死鎖的查詢及解決方法
這篇文章主要介紹了Oracle數(shù)據(jù)庫中對死鎖的查詢及解決方法,文中用兩個表創(chuàng)造死鎖的簡單例子來說明對死鎖的撤銷方法,需要的朋友可以參考下2016-01-01