【sql语句】实验五 函数(续)与索引

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40307434/article/details/84349683

 查看系统当前时间 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)+ (salary12*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 约束索引。也不能删除系统表中的索引。

猜你喜欢

转载自blog.csdn.net/qq_40307434/article/details/84349683