數(shù)據(jù)庫基礎

一、環(huán)境搭建

1、安裝軟件

  1. 安裝oracle_client
  2. 安裝plsql

2、配置數(shù)據(jù)庫連接信息

新建文件:
C:\Oracle\Ora81\network\ADMIN\tnsnames.ora
文件內(nèi)容:

aliyun_oradb =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 120.132.0.117)(PORT = 1521))
   )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME= CCSDB1)
    )
)

3、配置plsql

plsql封裝了oracle_clent
plsql——>工具——>首選項——>連接:
選擇:1. oracle主目錄名;2. OCI庫


image.png

4、登錄數(shù)據(jù)庫

  1. 數(shù)據(jù)庫:tnsname(本質(zhì)是變量參數(shù)化,包含IP、端口、實例名)
  2. 用戶名
  3. 密碼


    image.png

一、對象

  1. 數(shù)據(jù)

二、操作

三、表的基本元素

    • 表名
    • 歸屬用戶(所有的表,都有一個歸屬用戶o用戶owner,使用用戶c用戶customer)
    • 注釋(comment on table表名 is ‘注釋內(nèi)容’)
    • 表空間tablespace
    • 列名
    • 類型(varchar2、number、date)
    • 是否可為空(not null)
    • 默認值(default 值)
    • 注釋(comment on column 表名.字段名 is ‘注釋內(nèi)容’)(工作中遇到?jīng)]加的重要字段,盡量補上)
  1. 字段約束(鍵)

  • 主鍵(不為空,不重復,給該字段自動生成索引,primary key )
  • 唯一(不重復,空可以出現(xiàn)1次,unique)
  • 外鍵(該字段的值,必須在指定外部表中存在,foreign key)
    (選課表的課程號加外鍵,外鍵指向課程表的課程號字段,insert到選課表的時候,會拿課程號到課程表查詢,查不到,不讓加)
  1. 索引(index)
  • 主鍵索引(默認的,不用寫)
  • 普通索引
    (考慮弊端:增刪改,優(yōu)勢:查。如何使用,被動使用,主動使用)
  1. 權(quán)限(o用戶:owner所有權(quán)用戶、c用戶:customer使用客戶用戶;grant)
    把表的增刪改查權(quán)限有選擇性的賦權(quán)給其它用戶(比如程序連接的數(shù)據(jù)庫用戶名)

歸屬用戶

  1. 創(chuàng)建表的用戶,也即o用戶,owner
  2. 命名:devxxx1o、devxxx1c;tstxxx1o、tstxxx1c;proxxx1o、proxxx1c

表空間tablespace

  1. 表都是建立在表空間上的,類似windows的c盤、d盤、e盤
  2. 建表的時候,一要選用戶,二選表空間
  3. 磁盤監(jiān)控(linux剩余空間、內(nèi)存剩余量、數(shù)據(jù)庫表空間)
    • 空間滿了處理方式1:統(tǒng)計數(shù)據(jù)庫對象大小、刪記錄(delete無效,truncat降水位線)
    • 空間滿了處理方式2:加空間

日期

變量類型 變量名 變量值
varchar2 name guoyasoft
number age 23
date 6個變量 封裝成對象

時間內(nèi)容:

內(nèi)容 變量名
yyyy
mm
dd
hh或者hh24
mi
ss

取值函數(shù):

  1. to_char(date,'日期格式')
  2. to_date(日期字符串,'日期格式')
--查詢在1987年及以后入職的員工
select * from emp t where to_char(t.hiredate,'yyyy')>='1987';

--查詢在2月份入職的員工
select * from emp t where to_char(t.hiredate,'mm')='02';

--查詢員工信息,入職日期按照yyyy-mm-dd hh:mi:ss格式顯示
select to_char(t.hiredate,'yyyy-mm-dd hh:mi:ss') from emp t ;

--新增一條記錄,員工入職日期是2017年12月15日
insert into emp(empno,hiredate)values(1000,to_date('2017-12-15','yyyy-mm-dd'));

系統(tǒng)當前時間:

sysdate

虛擬字段和空表

## student表沒有classno,可以在返回結(jié)果中虛擬添加一個字段
select t.* ,'1710' classno from tb_student t ;

### 空表dual
select '1710' classno from dual;
select sysdate from dual;
select to_char(sysdate,'yyyy"年"mm"月"') from dual;

默認值

  1. 固定默認值:字段定義后面加default 默認值
  2. 變動的值:比如ID(序列sequence生成唯一值,觸發(fā)器trigger在insert的時候觸發(fā)取序列的值,并賦值給觸發(fā)字段上)
-- Create sequence 
create sequence S_T_CO_BATCH_DETAIL
minvalue 1
maxvalue 9999999999999999999999999999
start with 10001
increment by 1
cache 10;
CREATE OR REPLACE TRIGGER "TIB_T_CO_BATCH_DETAIL" BEFORE INSERT
ON "T_CO_BATCH_DETAIL" FOR EACH ROW
DECLARE
    INTEGRITY_ERROR  EXCEPTION;
    ERRNO            INTEGER;
    ERRMSG           CHAR(200);
    DUMMY            INTEGER;
    FOUND            BOOLEAN;

BEGIN
    --  COLUMN ""ID"" USES SEQUENCE S_T_CO_BATCH_DETAIL
    SELECT S_T_CO_BATCH_DETAIL.NEXTVAL INTO :NEW."ID" FROM DUAL;

--  ERRORS HANDLING
EXCEPTION
    WHEN INTEGRITY_ERROR THEN
       RAISE_APPLICATION_ERROR(ERRNO, ERRMSG);
END;

運行順序:

  1. 程序員寫insert語句
  2. oracle解析成執(zhí)行指令
  3. 執(zhí)行之前,觸發(fā)定義的觸發(fā)器,觸發(fā)器取序列的值,并替換執(zhí)行指令id的值
  4. oracle執(zhí)行新的指令(所以不管sql寫不寫id,都沒用)

索引

  1. 不加索引
    1000萬條數(shù)據(jù),查詢的時候,先把1000萬條數(shù)據(jù)的全部記錄的所有字段都取出來,從第1條查詢到最后1條
    select * from tb_student t where t.age=28;

  2. 加索引(age字段加索引)

  • 先把age字段的全部值和記錄對應的地址取出來,生成一個index對象
  • 把取出的鍵值對值,生成一個b-tree樹
2的n次方
image.png
image.png

權(quán)限、賦權(quán)、同義詞和dblink

同一個數(shù)據(jù)庫

tstccs1o和tstccs1c兩個用戶

  1. o用戶建了一張表:tb_student
  2. o用戶給c用戶賦權(quán):grant insert,update,select,delete on tstccs1c;
  3. c用戶就可以查詢tb_student表了,select * from tstccs1o.tb_student t ;
  4. c用戶建一個同義詞synonyms
create or replace sysnonyms tb_student for tstccs1o.tb_student;
select * from tstccs1o.tb_student t ;
簡化成:
select * from tb_student t ;
``
### 不同的2個數(shù)據(jù)庫
數(shù)據(jù)庫1的tstccs1o,tstccs1c兩個數(shù)據(jù)庫
1. o用戶建了一張表:tb_student
2. o用戶給c用戶賦權(quán):grant insert,update,select,delete on tstccs1c;
3. c用戶就可以查詢tb_student表了,select * from tstccs1o.tb_student t ;
4. c用戶建一個同義詞synonyms

create or replace sysnonyms tb_student for tstccs1o.tb_student;

select * from tstccs1o.tb_student t ;
簡化成:
select * from tb_student t ;
  1. 數(shù)據(jù)庫2去訪問數(shù)據(jù)1的tb_student
    創(chuàng)建dblink,把數(shù)據(jù)庫1的ip、端口、實例名、用戶名、密碼打包成一個別名
    登錄后,找到tstccs1c用戶的tb_student表
create /* public */ database link orcl  
  connect to dbusername identified by dbpassword  
  using '(DESCRIPTION =(ADDRESS_LIST =
(ADDRESS =(PROTOCOL = TCP)
(HOST = 192.168.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = orcl)))';  

select * from tstccs1c.tb_student@orcl

再取個同義詞

create or replace sysnonyms tb_student for tstccs1c.tb_student@orcl;

數(shù)據(jù)庫2,訪問數(shù)據(jù)庫1的tb_student,就可以像自己的表一樣用

select * from tb_student ;

四、數(shù)據(jù)的基本元素

image.png

1. 組合因子

  1. 視圖:可以是表、可以是sql處理結(jié)果
  2. 操作關鍵字(對oracle,1個關鍵字,就是一個處理方法)
  • from
  • join on
  • where
  • group by
  • having
  • select :rownum、rowid
  • top
  • order by

2. 組合規(guī)則

  1. 把1個視圖傳給操作方法,操作方法處理,再輸出一個視圖
  2. 操作方法可以套接成串執(zhí)行,但是有固定的先后順序
  3. sql和sql的結(jié)果可以做加減運算(minus、union、union all、intersect)

五、表的增刪改查

  1. 增:create table 表名(屬性)
## 正常的建表
create table t1(字段)values(值)
## 第2種,根據(jù)SQL建表(不要數(shù)據(jù),字段也可以指定)
create table t2
as
select * from t1 where 1=2 ;
## 第3種,根據(jù)已有的表建表(要指定數(shù)據(jù),一般用于備份數(shù)據(jù))
create table t3
as
select * from t1 ;
  1. 刪:drop table 表名;
  2. 改:alter table 表名
  3. 查:select * from user_tables t ;

六、數(shù)據(jù)的增刪改查

  1. 增:insert into 表名(字段名,字段名)values(字段值,字段值);
  2. 刪:delete 表名 where 條件;(或者全刪 truncat table 表名,這個會釋放空間降低水位線);
  3. 改:update 表名 set 字段名=字段值,字段名=字段值 where 條件
  4. 查:select * from 表名 where 條件 ;

連接

image.png
  1. 內(nèi)連接(B)
  2. 左連接(A+B)
  3. 右連接(C+B)
  4. 全連接(A+B+C)
image.png

內(nèi)連接
select * from student a, score b where a.sno=b.sno and b.score>=60
select * from student a inner join score b on a.sno=b.sno where and b.score>=60


image.png

左連接
select * from student a left join score b on a.sno=b.sno where and b.score>=60


image.png

右連接
select * from student a right join score b on a.sno=b.sno where and b.score>=60


image.png

全連接
select * from student a full join score b on a.sno=b.sno where and b.score>=60


image.png

SQL組合

image.png
  1. minus(A)
    sql1
    minus
    sql2

  2. union(A+B+C)
    sql1
    union
    sql2

  3. union all(A+2B+C)
    sql1
    union all
    sql2

  4. intersect(B)
    sq1
    intersect
    sql2

實踐

給定一個學生列表,到數(shù)據(jù)庫統(tǒng)計分析
S001
S002
S004
S005
S006
S007
S008
S0012
S0014
S0015

select * from tb_student t ;
S001
S002
S003
S004
S005
S006
S007
S008
S0011
S009
S0010


## 第1步:創(chuàng)建一個臨時表,將列表存入數(shù)據(jù)庫
create table tb_student_wl_1217
as
select * from tb_student t where 1=2;

## 第2步:將列表插入tb_student_wl_1217表
select t.*,t.rowid from tb_student_wl_1217 t ;

## 第3步:統(tǒng)計分析

--數(shù)據(jù)庫有,列表沒有
select t.student_id from tb_student t
minus
select t.student_id from tb_student_wl_1217 t;
S0010
S0011
S003
S009

--數(shù)據(jù)庫沒有,列表有
select t.student_id from tb_student_wl_1217 t
minus
select t.student_id from tb_student t;
S0012
S0014
S0015

--數(shù)據(jù)庫和列表都有
select t.student_id from tb_student_wl_1217 t
intersect
select t.student_id from tb_student t;
S001
S002
S004
S005
S006
S007
S008

--數(shù)據(jù)庫和列表的合集(去重)
select t.student_id from tb_student_wl_1217 t
union
select t.student_id from tb_student t;
S001
S0010
S0011
S0012
S0014
S0015
S002
S003
S004
S005
S006
S007
S008
S009

八、練習

練習1

1. 查詢tb_student表,去掉重復數(shù)據(jù)
## 不去重
select t.password,t.student_name from tb_student t ;
## 去重distinct
select distinct t.password,t.student_name from tb_student t ;

附帶練習:

  • F6提示不會的關鍵字
  • 表別名選擇字段(一定不要手寫字段名)
  • 局部注釋(手工和功能按鈕2種,/**/)
  1. 按照姓名字段排序
## 不排序
select * from tb_student t ;

## 按姓名排序(正序)
select * from tb_student t order by t.student_name ;
或
select * from tb_student t order by t.student_name asc;

## 按姓名排序(倒序)
select * from tb_student t order by t.student_name desc;

## 按照按照班級倒序、姓名正序排序
select * from tb_student t order by t.sclass desc,t.student_name ;

練習2: 統(tǒng)計學生表中每個班的人數(shù)

  1. 永遠都是先把表看懂(有哪些字段,什么含義)
  2. 寫SQL讀題
序號 步驟 SQL 讀題
1 from select * from tb_student t 根據(jù)題目中所有用到的字段,查找需要哪幾張表
2 join on 判斷是否需要左、右、全連接
3 where 讀題,看有沒有對字段做限制條件
4 group by group by t.sclass 讀題,看有沒有聚合函數(shù)
5 having 讀題,看對聚合函數(shù)是否有限制條件
6 select t.sclass,count(*) 讀題,看需要展現(xiàn)的字段
7 distinct 讀題,看是否需要去重
8 order by 讀題,看是否需要排序
9 rownum 讀題,看是否需要分頁,或限制條數(shù)

重中之重:

  1. group by后的視圖,只有group by的字段+5個聚合函數(shù),select要展現(xiàn)的字段,只能從group by后面的字段+5個聚合函數(shù)中取,*也不能用
  2. 如果要展現(xiàn)的字段不在group by后面的字段和5個聚合函數(shù)內(nèi),缺失的字段,再引入新表進行關聯(lián)
select a.sclass, b.cname, a.scount
  from (select t.sclass, count(*) scount 
       from tb_student t group by t.sclass) a,
       tb_class b
 where a.sclass = b.cid;

分頁查詢

  1. 生成數(shù)據(jù)視圖:查詢排序,生成視圖1
  2. 添加行號:最對視圖1做查詢,添加rownum列,列名rn
  3. 限制最大行號:對視圖2用where限制rownum的最大值,生成視圖2
  4. 限制最小行號:再對視圖2做查詢,where限制rn最小值
select *
  from (select h.*, rownum rn
          from (select t.*
                  from tb_student t
                 order by t.sclass desc, t.student_name) h
         where rownum <= 10)
 where rn > 5;
select t.*,rownum rn from tb_student t order by t.sclass desc; 

STUDENT_ID  PASSWORD    STUDENT_NAME    RESULT  SCLASS  RN
1   S009    123456  周武      C004    10
2   S0011   123456  周三1     C004    9
3   S0010   123456  周三1     C004    11
4   S006    123456  孫儷      C003    6
5   S008    123456  周三      C003    8
6   S007    123456  周武      C003    7
7   S005    123456  趙倩      C002    5
8   S004    123456  麻子      C002    4
9   S002    123456  李四      C001    2
10  S001    123456  張三      C001    1
11  S003    123456  王二      C001    3

select的字段會先執(zhí)行,再執(zhí)行order by,所有序號是亂的
糾正方式:先排序,將生成的視圖傳給from,再加一層查詢

select h.*,rownum rn from (select t.* from tb_student t order by t.sclass desc) h ; 

STUDENT_ID  PASSWORD    STUDENT_NAME    RESULT  SCLASS  RN
1   S009    123456  周武      C004    1
2   S0011   123456  周三1     C004    2
3   S0010   123456  周三1     C004    3
4   S006    123456  孫儷      C003    4
5   S008    123456  周三      C003    5
6   S007    123456  周武      C003    6
7   S005    123456  趙倩      C002    7
8   S004    123456  麻子      C002    8
9   S002    123456  李四      C001    9
10  S001    123456  張三      C001    10
11  S003    123456  王二      C001    11

深度理解SQL執(zhí)行順序

  1. 行號亂序
    select t.*,rownum rn from tb_student t order by t.sclass desc
    分析原因:select決定展現(xiàn)字段,此時生成rownum,然后才執(zhí)行order by,所以亂序

  2. where語句rn不能用,order by能用
    分析原因:where執(zhí)行的時候,還沒執(zhí)行select,此時rownum和rn別名都還沒處理,所以where用不到

select h.*,rownum rn from (select t.* from tb_student t order by t.sclass desc) h order by rn ; 

分析原因:order by再select之后,rownum和別名rn已經(jīng)處理完成,所以可以直接用

select h.*,rownum rn from (select t.* from tb_student t order by t.sclass desc) h where rownum <=8 ; 
select *
  from (select h.*, rownum rn
          from (select t.* from tb_student t order by t.sclass desc) h
         where rownum <= 8)
 where rn >= 5

將rn拆分成2個變量,開放給用戶選擇

--變量1:第幾頁:2
--變量2:每頁多少條:3

--先按照每頁條數(shù),統(tǒng)計總記錄數(shù),總頁數(shù)(便于直接查詢最后一頁)
select count(*),ceil(count(*)/3) from tb_student t ;

--(第幾頁-1)*每頁條數(shù) < rn < 第幾頁* 每頁條數(shù)
select *
  from (select h.*, rownum rn
          from (select t.* from tb_student t order by t.sclass desc) h
         where rownum <= 2*3)
 where rn > (2-1)*3
image.png

擴展問題

為什么第一個限制行號的地方只能用rownum,用rn卻會報錯,而外面一層能夠用別名rn?
select h., rownum rn
from (select t.

from tb_student t
order by t.sclass desc, t.student_name) h
where rownum <= 10

解答:因為where先執(zhí)行,select后執(zhí)行,做where的時候,rn別名還沒生效

視圖

  1. sql的每一步,都會生成視圖
  2. 有些復雜的視圖,可以把sql存起來,當表用
  3. 存的類型,就是視圖

練習:
創(chuàng)建1個視圖
刪除1個視圖

查詢當前用戶的所有表

select * from user_tables;

其中user_tables就是系統(tǒng)自帶的一個視圖,簡化用戶查詢語句,視圖的sql

create or replace view sys.user_tables as
select o.name,
       decode(bitand(t.property,2151678048), 0, ts.name,
              decode(t.ts#, 0, null, ts.name)),
       decode(bitand(t.property, 1024), 0, null, co.name),
       decode((bitand(t.property, 512)+bitand(t.flags, 536870912)),
              0, null, co.name),
       decode(bitand(t.trigflag, 1073741824), 1073741824, 'UNUSABLE', 'VALID'),
       decode(bitand(t.property, 32+64), 0, mod(t.pctfree$, 100), 64, 0, null),
       decode(bitand(ts.flags, 32), 32, to_number(NULL),
          decode(bitand(t.property, 32+64), 0, t.pctused$, 64, 0, null)),
       decode(bitand(t.property, 32), 0, t.initrans, null),
       decode(bitand(t.property, 32), 0, t.maxtrans, null),
       decode(bitand(t.property, 17179869184), 17179869184,
                     ds.initial_stg * ts.blocksize,
                     s.iniexts * ts.blocksize),
       decode(bitand(t.property, 17179869184), 17179869184,
              ds.next_stg * ts.blocksize,
              s.extsize * ts.blocksize),
       decode(bitand(t.property, 17179869184), 17179869184,
              ds.minext_stg, s.minexts),
       decode(bitand(t.property, 17179869184), 17179869184,
              ds.maxext_stg, s.maxexts),
       decode(bitand(ts.flags, 3), 1, to_number(NULL),
              decode(bitand(t.property, 17179869184), 17179869184,
                            ds.pctinc_stg, s.extpct)),
       decode(bitand(ts.flags, 32), 32, to_number(NULL),
         decode(bitand(o.flags, 2), 2, 1,
                decode(bitand(t.property, 17179869184), 17179869184,
                       decode(ds.frlins_stg, 0, 1, ds.frlins_stg),
                       decode(s.lists, 0, 1, s.lists)))),
       decode(bitand(ts.flags, 32), 32, to_number(NULL),
         decode(bitand(o.flags, 2), 2, 1,
                decode(bitand(t.property, 17179869184), 17179869184,
                       decode(ds.maxins_stg, 0, 1, ds.maxins_stg),
                       decode(s.groups, 0, 1, s.groups)))),
       decode(bitand(t.property, 32+64), 0,
                decode(bitand(t.flags, 32), 0, 'YES', 'NO'), null),
       decode(bitand(t.flags,1), 0, 'Y', 1, 'N', '?'),
       t.rowcnt,
       decode(bitand(t.property, 64), 0, t.blkcnt, null),
       decode(bitand(t.property, 64), 0, t.empcnt, null),
       decode(bitand(t.property, 64), 0, t.avgspc, null),
       t.chncnt, t.avgrln, t.avgspc_flb,
       decode(bitand(t.property, 64), 0, t.flbcnt, null),
       lpad(decode(t.degree, 32767, 'DEFAULT', nvl(t.degree,1)),10),
       lpad(decode(t.instances, 32767, 'DEFAULT', nvl(t.instances,1)),10),
       lpad(decode(bitand(t.flags, 8), 8, 'Y', 'N'),5),
       decode(bitand(t.flags, 6), 0, 'ENABLED', 'DISABLED'),
       t.samplesize, t.analyzetime,
       decode(bitand(t.property, 32), 32, 'YES', 'NO'),
       decode(bitand(t.property, 64), 64, 'IOT',
               decode(bitand(t.property, 512), 512, 'IOT_OVERFLOW',
               decode(bitand(t.flags, 536870912), 536870912, 'IOT_MAPPING', null))),
       decode(bitand(o.flags, 2), 0, 'N', 2, 'Y', 'N'),
       decode(bitand(o.flags, 16), 0, 'N', 16, 'Y', 'N'),
       decode(bitand(t.property, 8192), 8192, 'YES',
              decode(bitand(t.property, 1), 0, 'NO', 'YES')),
       decode(bitand(o.flags, 2), 2, 'DEFAULT',
              decode(bitand(decode(bitand(t.property, 17179869184), 17179869184,
                            ds.bfp_stg, s.cachehint), 3),
                            1, 'KEEP', 2, 'RECYCLE', 'DEFAULT')),
       decode(bitand(o.flags, 2), 2, 'DEFAULT',
              decode(bitand(decode(bitand(t.property, 17179869184), 17179869184,
                            ds.bfp_stg, s.cachehint), 12)/4,
                            1, 'KEEP', 2, 'NONE', 'DEFAULT')),
       decode(bitand(o.flags, 2), 2, 'DEFAULT',
              decode(bitand(decode(bitand(t.property, 17179869184), 17179869184,
                            ds.bfp_stg, s.cachehint), 48)/16,
                            1, 'KEEP', 2, 'NONE', 'DEFAULT')),
       decode(bitand(t.flags, 131072), 131072, 'ENABLED', 'DISABLED'),
       decode(bitand(t.flags, 512), 0, 'NO', 'YES'),
       decode(bitand(t.flags, 256), 0, 'NO', 'YES'),
       decode(bitand(o.flags, 2), 0, NULL,
           decode(bitand(t.property, 8388608), 8388608,
                  'SYS$SESSION', 'SYS$TRANSACTION')),
       decode(bitand(t.flags, 1024), 1024, 'ENABLED', 'DISABLED'),
       decode(bitand(o.flags, 2), 2, 'NO',
           decode(bitand(t.property, 2147483648), 2147483648, 'NO',
              decode(ksppcv.ksppstvl, 'TRUE', 'YES', 'NO'))),
       decode(bitand(t.property, 1024), 0, null, cu.name),
       decode(bitand(t.flags, 8388608), 8388608, 'ENABLED', 'DISABLED'),
       case when (bitand(t.property, 32) = 32) then
         null
       when (bitand(t.property, 17179869184) = 17179869184) then
         decode(bitand(ds.flags_stg, 4), 4, 'ENABLED', 'DISABLED')
       else
         decode(bitand(s.spare1, 2048), 2048, 'ENABLED', 'DISABLED')
       end,
       case when (bitand(t.property, 32) = 32) then
         null
       when (bitand(t.property, 17179869184) = 17179869184) then
          decode(bitand(ds.flags_stg, 4), 4,
          case when bitand(ds.cmpflag_stg, 3) = 1 then 'BASIC'
               when bitand(ds.cmpflag_stg, 3) = 2 then 'OLTP'
               else decode(ds.cmplvl_stg, 1, 'QUERY LOW',
                                          2, 'QUERY HIGH',
                                          3, 'ARCHIVE LOW',
                                             'ARCHIVE HIGH') end,
               null)
       else
         decode(bitand(s.spare1, 2048), 0, null,
         case when bitand(s.spare1, 16777216) = 16777216   -- 0x1000000
                   then 'OLTP'
              when bitand(s.spare1, 100663296) = 33554432  -- 0x2000000
                   then 'QUERY LOW'
              when bitand(s.spare1, 100663296) = 67108864  -- 0x4000000
                   then 'QUERY HIGH'
              when bitand(s.spare1, 100663296) = 100663296 -- 0x2000000+0x4000000
                   then 'ARCHIVE LOW'
              when bitand(s.spare1, 134217728) = 134217728 -- 0x8000000
                   then 'ARCHIVE HIGH'
              else 'BASIC' end)
       end,
       decode(bitand(o.flags, 128), 128, 'YES', 'NO'),
       decode(bitand(t.trigflag, 2097152), 2097152, 'YES', 'NO'),
       decode(bitand(t.property, 17179869184), 17179869184, 'NO',
              decode(bitand(t.property, 32), 32, 'N/A', 'YES')),
       decode(bitand(t.property,16492674416640),2199023255552,'FORCE',
                     4398046511104,'MANUAL','DEFAULT')
from sys.ts$ ts, sys.seg$ s, sys.obj$ co, sys.tab$ t, sys.obj$ o,
     sys.deferred_stg$ ds, sys.obj$ cx, sys.user$ cu, x$ksppcv ksppcv,
     x$ksppi ksppi
where o.owner# = userenv('SCHEMAID')
  and o.obj# = t.obj#
  and bitand(t.property, 1) = 0
  and bitand(o.flags, 128) = 0
  and t.bobj# = co.obj# (+)
  and t.ts# = ts.ts#
  and t.file# = s.file# (+)
  and t.block# = s.block# (+)
  and t.ts# = s.ts# (+)
  and t.obj# = ds.obj# (+)
  and t.dataobj# = cx.obj# (+)
  and cx.owner# = cu.user# (+)
  and ksppi.indx = ksppcv.indx
  and ksppi.ksppinm = '_dml_monitoring_enabled';

綜合練習

1. 題目

學生表(學生id,姓名,性別,分數(shù))student(s_id,name,sex,score)
班級表(班級id,班級名稱)class(c_id,c_name)
學生班級表(班級id,學生id)student_class(s_id,c_id)
1.查詢一班得分在80分以上的學生
2.查詢所有班級的名稱,和所有班中女生人數(shù)和女生的平均分

2. 核對數(shù)據(jù)

給定一個學生列表,和數(shù)據(jù)庫的學生表對比差異

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容