亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

oracle基本查詢(xún)操作子查詢(xún)用法實(shí)例分析

 更新時(shí)間:2020年02月20日 10:26:01   作者:懷素真  
這篇文章主要介紹了oracle基本查詢(xún)操作子查詢(xún)用法,結(jié)合實(shí)例形式分析了oracle數(shù)據(jù)庫(kù)子查詢(xún)相關(guān)概念、原理、語(yǔ)法、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下

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

一、子查詢(xún)語(yǔ)法

SELECT select_list
FROM table
WHERE expr operator (SELECT select_list FROM table);

子查詢(xún)?cè)谥鞑樵?xún)之前一次執(zhí)行完成。
子查詢(xún)的結(jié)果被主查詢(xún)使用。

select ename from emp where sal > (select sal from emp where ename='SCOTT');

(*注意:子查詢(xún)要包含在括號(hào)內(nèi),將子查詢(xún)放在比較條件的右側(cè)。單行操作符對(duì)應(yīng)單行子查詢(xún),多行操作符對(duì)應(yīng)多行子查詢(xún)。)

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

--子查詢(xún)中使用組函數(shù)
select ename,sal from emp where sal=(select min(sal) from emp);
--子查詢(xún)中的having子句
--首先執(zhí)行子查詢(xún)
--向主查詢(xún)中的having子句返回結(jié)果
select deptno, min(sal)
 from emp
 group by deptno
having min(sal) > (select min(sal) from emp);

多行子查詢(xún),返回多行,使用多行比較符(IN ANY ALL)

--查詢(xún)比部門(mén)10里任意一個(gè)人工資高的員工信息
select ename, sal
 from emp
 where sal > any (select sal from emp where deptno = 10);
--查詢(xún)比部門(mén)20里所有人工資高的員工信息
select ename, sal
 from emp
 where sal > all (select sal from emp where deptno = 20);
--查詢(xún)不是老板的員工信息
select ename from emp where empno not in(select mgr from emp);

二、集合運(yùn)算

并集
UNION運(yùn)算符返回兩個(gè)集合去掉重復(fù)元素后的所有記錄。
UNION ALL 返回兩個(gè)集合的所有記錄,包括重復(fù)的。
交集
INTERSECT 運(yùn)算符返回同時(shí)屬于兩個(gè)集合的記錄

--返回工資在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 返回屬于第一個(gè)集合,但不屬于第二個(gè)集合的記錄。

--返回工資屬于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;

集合使用的注意事項(xiàng)

1、select語(yǔ)句中參數(shù)類(lèi)型和個(gè)數(shù)保持一致。
2、可以使用括號(hào)改變集合執(zhí)行的順序。
3、如果有order by,必須放到最后一句查詢(xún)語(yǔ)句后。
4、集合運(yùn)算采用第一個(gè)語(yǔ)句的表頭作為表頭。

三、數(shù)據(jù)操作語(yǔ)言

插入數(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使用子查詢(xún)
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ù)庫(kù)事務(wù)

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

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

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

回滾到保留點(diǎn)
使用savepoint語(yǔ)句在當(dāng)前事務(wù)中創(chuàng)建保存點(diǎn)。
使用rollback to savepoint語(yǔ)句回滾到創(chuàng)建的保存點(diǎn)。

update emp set sal=sal+100 where empno=7369;
savepoint update_empno7369;
delete from emp where empno=7369;
rollback to update_empno7369;

五、創(chuàng)建和管理表

常見(jiàn)的數(shù)據(jù)庫(kù)對(duì)象
如下:
表        基本的數(shù)據(jù)存儲(chǔ)集合,由行和列組成。
視圖     從表中抽出的邏輯上相關(guān)的數(shù)據(jù)集合。
序列     提供有規(guī)律的數(shù)值。
索引     提高查詢(xún)的效率。
同義詞  給對(duì)象起別名。

創(chuàng)建表

CREATE TABLE [schema.]table (column datatype [DEFAULT expr][, ...]);
create table test(
id number(12),
name varchar2(32));

通過(guò)子查詢(xún)創(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、所有正在運(yùn)行的相關(guān)事物被提交
3、所有相關(guān)索引被刪除
4、DROP TABLE語(yǔ)句不能回滾,但是可以閃回。

drop table test;

改變對(duì)象的名稱(chēng)

rename dept to newDept;

清空表
1、刪除表中所有數(shù)據(jù)。
2、釋放表的存儲(chǔ)空間。
3、truncate不能回滾。

truncate table test;

更多關(guān)于Oracle相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Oracle常用函數(shù)匯總》、《Oracle日期與時(shí)間操作技巧總結(jié)》及《php+Oracle數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)

希望本文所述對(duì)大家Oracle數(shù)據(jù)庫(kù)程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論