博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle SQL (2):SQL Fundamentals(Day II)
阅读量:6248 次
发布时间:2019-06-22

本文共 4880 字,大约阅读时间需要 16 分钟。

联合查询:

1.等值连接:(内连接,自然连接)

SQL> select d.deptno, d.dname, e.empno, e.ename, e.sal

  2  from dept d, emp e
  3  where d.deptno=e.deptno;

13 rows selected.

2.非等值连接:

SQL> select e.ename, e.sal, s.grade

  2  from emp e, salgrade s
  3* where e.sal between s.losal and s.hisal

3.自连接:

SQL> select e.ename, m.ename

  2  from emp e, emp m
  3  where e.mgr=m.empno;

4.外连接:

sql99(sql2003) standard:

SQL> select e.empno, e.ename, d.dname

  2  from emp e left outer join dept d
  3  on e.deptno = d.deptno;

14 rows selected.

oracle standard:

SQL> select e.empno, e.ename, d.dname

  2  from emp e, dept d
  3  where e.deptno=d.deptno(+); 

14 rows selected.

oracle can't but sql99 can:

SQL> select e.empno, e.ename, d.dname

  2  from emp e full outer join dept d
  3  on e.deptno=d.deptno;

15 rows selected.

5.笛卡尔乘积:

SQL> select d.deptno, d.dname, e.empno, e.ename, e.sal

  2  from dept d, emp e

56 rows selected.(14 * 4)

子查询:

SQL> select empno, ename, sal        

  2  from emp
  3  where sal > (select sal                     
  4               from emp
  5               where ename='BLAKE');

null值处理:

SQL> select empno, ename, sal

  2  from emp
  3  where empno not in (select mgr
  4                      from emp);

no rows selected. (And results to now result).

SQL> select empno, ename, sal

  2  from emp
  3  where empno in (select mgr
  4                  from emp);

EMPNO ENAME    SAL

----- ------ -----
 7566 JONES   2975
 7698 BLAKE   2850
 7782 CLARK   2450
 7788 SCOTT   3000
 7839 KING    5000
 7902 FORD    3000

6 rows selected. (Or results to records).

SQL> select empno, ename, sal

  2  from emp
  3* where empno not in (select mgr
                         from emp
                         where mgr is not null)

EMPNO ENAME    SAL

----- ------ -----
 7369 SMITH    800
 7499 ALLEN   1600
 7521 WARD    1250
 7654 MARTIN  1250
 7844 TURNER  1500
 7876 ADAMS   1100
 7900 JAMES    950
 7934 MILLER  1300

8 rows selected. (Remove null value results to records).

Top N分析:

SQL> select * from (select empno, ename, sal from emp order by sal desc) where rownum < 4 order by rownum;

EMPNO ENAME    SAL

----- ------ -----
 7839 KING    5000
 7788 SCOTT   3000
 7902 FORD    3000

Union/Union All/Intersect/Minus:

SQL> break on deptno skip 1;

SQL> select deptno, job, sum(sal)

  2  from emp
  3  group by deptno, job
  4  union
  5  select deptno, to_char(null), sum(sal)
  6  from emp
  7  group by deptno
  8  union
  9  select to_number(null), to_char(null), sum(sal)
 10  from emp;

SQL> select deptno, job, sum(sal) tsal

  2  from emp
  3  group by rollup(deptno,job);

Insert/Update:

SQL> create table test as

  2  select empno, ename, sal
  3  from emp   
  4  where 1=2;

SQL> insert into test values(100, 'tom', null);

SQL> insert into test(empno, ename) values (101, 'mike');

SQL> truncate table test;

SQL> insert into test select empno, ename, sal from emp;
14 rows created.

SQL> show feedback;

FEEDBACK ON for 6 or more rows
SQL> set feedback off;
SQL> set feedback on;
SQL> set feedback 6;

SQL> update test set sal = (select sal from emp where empno=7499) where empno=7369;

Truncate/Delete:

DML(Insert, Update, Delete, Merge)

DDL(Create, Alter, Drop, Rename, Truncate, Comment)
DCL(Grant, Revoke)
Transaction Control(Commit, Rollback, Savepoint)

显示提交:rollback, commit

隐式提交:DDL/DCL,正常退出
          begin
              commit;
              DDL/DCL;
              commit;
          end
隐式回滚:异常退出

SQL> savepoint A;

Savepoint created.

SQL> delete from test where empno=7369;

1 row deleted.

SQL> rollback to savepoint A;

Create Table, View, Sequence, Index, Synonyms(同义词)

varchar2(10)/varchar2(10 char)

number/number(8, 2)
CLOB(text)/BLOB(Binary, media, exe)
BFile: Binary data stored in an external file

Constraint:

not null, unique, primary key, foreign key,check

SQL> purge recyclebin;

SQL> drop table test purge;

Table:

SQL> create table test(id number, name varchar2(10), create_date date default sysdate);

SQL> insert into test values(100, 'mike', default);

SQL> create table et(eno number,

  2                  ename varchar2(10),
  3                  constraint en_u_eno unique(eno));

SQL> create table dt(dno number(4) primary key,

  2                  dname varchar2(10));

SQL> create table d as select deptno dno, dname, loc from dept;    

SQL> create table e as select empno, ename, sal, deptno as dno from emp;

SQL> alter table d add constraint d_pk primary key (dno);

SQL> alter table e add constraint e_fk foreign key (dno) references d(dno);

SQL> drop table e purge;

SQL> create table e(eno number,

  2                 ename varchar2(10),
  3                 dno number references d(dno) on delete set null);

SQL> create table e1(eno number,

  2                  ename varchar2(10),
  3                  dno number references d(dno) on delete cascade);

SQL> alter table e1 read only;

SQL> create table e2 as select * from emp;

SQL> alter table e2 add constraint ck_e2 check ((sal > 0) and (sal < 10000));

View:

SQL> conn / as sysdba;

SQL> grant create view to scott;

SQL> conn scott/tiger;

SQL> create or replace view v1 as select empno, ename, sal from emp with check option;

SQL> create or replace view v2 as select empno, ename, sal from emp with read only;

Index:

{TODO:}

Sequence:

{TODO:}

Synonym:

SQL> conn / as sysdba;

SQL> grant create synonym to scott;

SQL> conn scott/tiger;

SQL> create synonym sg for salgrade;

转载于:https://www.cnblogs.com/thlzhf/archive/2013/04/21/3033563.html

你可能感兴趣的文章
“互联网+大数据”成为审讯突破口
查看>>
联发科4G方案渐趋成熟 2016市场或将迎来大反转
查看>>
商场没有永久的敌人 英特尔拟为ARM生产芯片
查看>>
大数据时代:九个大数据应用领域
查看>>
再度入场 重资产探路 公交WiFi卷土重来
查看>>
首次曝光!在线视频衣物精确检索技术,开启刷剧败明星同款时代
查看>>
实战篇-六十六行完成简洁的Rss输出类
查看>>
世界各地GSM和LTE移动网络存在严重安全漏洞
查看>>
资源编排最佳实践之入门篇:云服务器如何从1到N?
查看>>
Grumpy:Google 用 Go 开发的 Python 运行时
查看>>
欧盟取消4家中国企业在晶体硅光伏组件及关键零部件双反案中的价格承诺
查看>>
采购杀毒软件 确保网络信息安全
查看>>
破局物联网时代,海尔靠这三步棋
查看>>
Eclipse Che 5.0会带来对Docker Compose的支持、Workspace Agents等特性
查看>>
纠缠不清,微软Azure云服务看上Here地图
查看>>
面向对象的软件分析设计过程备忘
查看>>
Bash远程命令执行漏洞(CVE-2014-6271)分析利用
查看>>
meter资源监控器开发——关键代码分析
查看>>
服务器常见问题汇总
查看>>
英国核潜艇仍运行XP系统 暴露于WannaCry等威胁
查看>>