国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > Oracle > 正文

oracle基本查詢操作子查詢用法實例分析

2020-07-26 13:25:56
字體:
供稿:網(wǎng)友

本文實例講述了oracle基本查詢操作子查詢用法。分享給大家供大家參考,具體如下:

一、子查詢語法

SELECT select_listFROM tableWHERE 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è)。單行操作符對應單行子查詢,多行操作符對應多行子查詢。)

單行子查詢,只返回一行,使用單行比較符(> = < >= <= != <>)

--子查詢中使用組函數(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 deptnohaving 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運算符返回兩個集合去掉重復元素后的所有記錄。
UNION ALL 返回兩個集合的所有記錄,包括重復的。
交集
INTERSECT 運算符返回同時屬于兩個集合的記錄

--返回工資在500-1000和900-1200的員工信息select ename, salfrom empwhere sal between 500 and 1000intersectselect ename, salfrom empwhere sal between 900 and 1200;

差集

MINUS 返回屬于第一個集合,但不屬于第二個集合的記錄。

--返回工資屬于500-1000,但不屬于900-1200的員工信息select ename, salfrom empwhere sal between 500 and 1000minusselect ename, salfrom empwhere 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 tableSET column=value [, column=value, ...][WHERE codition]
--更新一條數(shù)據(jù)update emp set sal=sal+100 where empno=7369;
--update使用子查詢update empset 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ù)庫事務

數(shù)據(jù)庫事務由以下的部分組成:
1、一個或多個DML語句
2、一個DDL數(shù)據(jù)定義語句
3、一個DCL數(shù)據(jù)控制語句

以第一個DML語句的執(zhí)行作為開始
以下面的其中之一作為結(jié)束:
顯示結(jié)束:commit rollback
隱式結(jié)束(自動提交):DDL語句,DCL語句,exit(事務正常退出)
隱式回滾(系統(tǒng)異常終了):關(guān)閉窗口,死機,掉電

commit和rollback語句的優(yōu)點
1、確保數(shù)據(jù)完整性。
2、數(shù)據(jù)改變被提交之前預覽。
3、將邏輯上相關(guān)的操作分組。

回滾到保留點
使用savepoint語句在當前事務中創(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 tableADD (column datatype [DEFAULT expr] [, column datatype] ...);--添加info列alter table test add (info varchar2(256) default '');--修改列ALTER TABLE tableMODIFY (column datatype [DEFAULT expr] [, column datatype] ...);--修改info列alter table test modify (info varchar2(64) default '');--刪除列ALTER TABLE tableDROP column (column);--刪除info列alter table test drop column info;--修改列名ALTER TABLE tablerename 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è)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汾西县| 许昌市| 白城市| 淳安县| 德昌县| 昌邑市| 巴彦县| 蒙城县| 汤阴县| 涟水县| 湘潭县| 柳河县| 茌平县| 马边| 高阳县| 梨树县| 电白县| 新河县| 黄冈市| 诸暨市| 广元市| 南京市| 饶河县| 斗六市| 株洲市| 澎湖县| 哈尔滨市| 安阳市| 沾益县| 乐业县| 东阳市| 甘洛县| 柞水县| 遂昌县| 佛学| 循化| 赫章县| 昭苏县| 出国| 怀集县| 九龙城区|