1. 程式人生 > >【sql語句】實驗五 函式(續)與索引

【sql語句】實驗五 函式(續)與索引

 檢視系統當前時間 select sysdate from dual;
 當計算器使用 select 3+3 from dual;
 檢視系統當前使用者 select user from dual;
 將系統當前時間轉換為一定的格式:select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’) from dual;
 檢視序列下一個值:select aaa.nextval from dual;
create sequence myseq;
select myseq.nextval from dual;
select myseq.currval from dual;

1、TO_CHAR
select to_char(salary,’$99,999.00’)salary from employees
where last_name=‘Ernst’;//用hr/hr登入,$6,000.00
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’) from dual;// 2018-11-22 20:33:22

2、TO_NUMBER(char[, ‘format_model’])
select to_number(‘123,456,789.0569’,‘999,999,999.0000’) from dual;// 123456789
3、TO_DATE(char[, ‘format_model’])
select to_date(‘2008-08-08 20:00:00’,‘yyyy-mm-dd hh24:mi:ss’) from dual;
4、NVL 第一個為null,就第二個
select last_name, salary,nvl(commission_pct,0),(salary12)+ (salary

12*nvl(commission_pct, 0)) an_sal from employees;
select nvl(null,0) from dual;//0
5、NVL2(a,b,c) //a不為null就顯示b,否則c
select last_name, salary, commission_pct, nvl2(commission_pct, ‘sal+comm’, ‘sal’) income
from employees where department_id in (50, 80);
6、組函式

  1. select avg(salary),max(salary),min(salary),sum(salary) from employees
    where job_id like’%REP%’;
  2. select department_id, avg(salary)
    from employees
    group by department_id ;
  3. select department_id dept_id, job_id, sum(salary)
    from employees
    group by department_id,job_id;
  4. select department_id dept_id,max(salary)
    from employees
    group by department_id
    having max(salary)>10000;//組條件
    5) select job_id,sum(salary) payroll
    from employees
    where job_id not like ‘%REP%’//元組條件
    group by job_id
    having sum(salary)>13000//組條件
    order by sum(salary);//排序條件desc asc
    練習
    1、 查詢emp表每個部門有多少人,平均工資,最高工資和最低工資。
    select count(),avg(salary),max(salary),min(salary)
    from employees
    group by department_id;
    2、 查詢emp表每個部門工資低於3000人的總人數。
    select distinct department_id deptn,
    (select count(
    ) from employees x
    where x.department_id=y.department_id and x.salary<3000) cnt
    from employees y order by deptn;
    3、 查詢emp表平均工資高於2000的部門。
    select nvl(department_id,0) from employees
    group by department_id
    having avg(salary)>2000;
    (二) 索引
    CREATE [UNIQUE] INDEX <索引名>
    ON <表名>(<列名>[<次序>][,<列名>[<次序>] ]…)
    1、 寫出對S表的S#建立索引index_S#的SQL語句。
    create index index_s# on s s(s#);//此列列表已索引
    2、 寫出對S表的SNAME建立不重名索引index_SNAME的SQL語句。
    create unique index index_sname on s(sname);
    3、 寫出對SC表的主鍵+成績(降序)的索引
    create index index_sc on sc(s#,c#,grade desc);

附、關於索引的參考內容:
 --供應商資訊:供應商編號wh_no、供應商名稱wh_name 、供應商型別wh_type (1:國有;2:中外合資;3:外資;4:私營;5:其它)、應付金額wh_deposit;
create table wh(
wh_no varchar2(30) not null,
wh_name varchar2(30) not null,
wh_type varchar2(30) not null,
wh_deposit number(9,2),
constraint wh_pk primary key(wh_no),//constraint 讓該條限制也擁有姓名
check ( wh_type in(‘1:國有’,‘2:中外合資’,‘3:外資’,‘4:私營’,‘5:其它’))
);
–注意:插入倉庫型別的變數只能是1:國有;2:中外合資;3:外資;4:私營;5:其它

-原材料資訊:原材料編號item_no、原材料名稱 item_name、計量單位item_per、數量item_qulity、計劃單價item_perprice;
create table item(
item_no varchar2(30) not null,
item_name varchar2(30) not null,
item_per varchar2(10) default ‘萬件’,
item_qulity number(9,4),
item_perprice number(9,2),
primary key(item_no)
);

–物品入庫資訊:入庫單編號deposit_no、入庫日期deposit_date、供應商編號wh_no、原材料編號 item_no 、數量deposit_quantity、入庫單價deposit_price。
create table deposit(
deposit_no varchar2(30) not null,
deposit_date date default sysdate,
wh_no varchar2(30),
item_no varchar2(30),
deposit_quantity number(38),
deposit_price number(9,2),
primary key(wh_no,item_no),
foreign key (wh_no) references wh(wh_no),
foreign key (item_no) references item(item_no)
);

(1) 建立物品名稱不能重名的索引
SQL> create unique index in_na on wh(WH_NAME) pctfree 20; //索引已建立
SQL> analyze index in_na validate structure;//索引已分析
SQL> select name ,height, lf_blks, pct_used from index_stats;
NAME HEIGHT LF_BLKS PCT_USED


IN_NA 1 1 4

(2)建立物品入庫資訊中主關鍵字加入庫日期的索引。
Create unique index deposit_index on deposit(wh_no,item_no,deposit_date);//索引已建立。
analyze index deposit_index validate structure;//索引已分析
select name,height,lf_blks,pct_used from index_stats;
NAME HEIGHT LF_BLKS PCT_USED


DEPOSIT_INDEX 1 1

 索引的總結:

  1. 建立索引的目的
     提高對錶的查詢速度
     對錶有關列的取值進行檢查
  2. 索引的建立方法
    (1)BTree索引。
    Create index indexname on tablename(columnname[columnname…])
    (2)反向索引。
    Create index indexname on tablename(columnname[columnname…]) reverse
    (3)降序索引。
    Create index indexname on tablename(columnname DESC[columnname…])
    (4)點陣圖索引。
    Create BITMAP index indexname on tablename(columnname[columnname…])
    (5)函式索引。
    Create index indexname on tablename(functionname(columnname))
    注意:建立索引後要分析索引才能起作用。
    analyze index deposit_index validate structure;

3.各種索引使用場合及建議
(1)BTree索引。
常規索引,多用於OLTP(聯機事務處理)系統,快速定位行,應建立於高cardinality列(即列的唯一值除以行數為一個很大的值,存在很少的相同值)。
(2)反向索引。
B
Tree的衍生產物,應用於特殊場合,在ops環境加序列增加的列上建立,不適合做區域掃描。
(3)降序索引。
BTree的衍生產物,應用於有降序排列的搜尋語句中,索引中儲存了降序排列的索引碼,提供了快速的降序搜尋。
(4)點陣圖索引。
點陣圖方式管理的索引,適用於OLAP(線上分析)和DSS(決策處理)系統,應建立於低cardinality列,適合集中讀取,不適合插入和修改,提供比B
Tree索引更節省的空間。

(5)函式索引。
B*Tree的衍生產物,應用於查詢語句條件列上包含函式的情況,索引中儲存了經過函式計算的索引碼值。可以在不修改應用程式的基礎上能提高查詢效率。

4.索引的刪除方法
drop index 命令可以刪除一個或多個當前資料庫中的索引。其語法如下:
drop index ‘tablename.indexname’ [,…n]
drop index 命令不能刪除由create table 或alter table 命令建立的primary key 或unique 約束索引。也不能刪除系統表中的索引。