Oracle初级测试题十六(17-17)--序列索引同义词
原创约 1026 字大约 3 分钟...
Oracle初级测试题十六(17-17)--序列索引同义词
注意
本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。
练习1
- 创建一个序列,该序列起始值从1开始,无最大值,增量是1,不循环。
create sequence test_seq1
start with 1 -- 始值从1开始 可省略
nomaxvalue -- 无最大值
increment by 1 -- 增量是1 可省略
nocycle; -- 不循环
drop sequence test_seq1;
- 查询序列的当前值及下一个值
select test_seq1.nextval from dual;-- 查询序列的下一个值
select test_seq1.currval from dual; -- 查询序列的当前值
- 使用第1题所建的序列,向部门表中插入两条记录,部门编号使用序列值,部门名称分别为:Education、Market,城市分别为:DALLAS、WASHTON
create table copy_dept
as select * from dept;
select * from copy_dept;
insert into copy_dept
values(test_seq1.nextval,'Education','DALLAS');
insert into copy_dept
values(test_seq1.nextval,'Market','WASHTON');
select * from copy_dept;

练习2
- 使用子查询的方式,创建test表。
create table test
as select * from emp;
select * from test;

- 快速复制test表中的数据,复制到100w条左右
insert into test select * from test;
-- 执行咯 13将 147456条数据 大概要执行16次左右
select * from test;
- 更新test表中的empno字段为rownum
alter table test modify empno number(10);
--先要修改精度,否则报错 值大于为此列指定的允许精度
update test set empno = rownum;
select * from test;

- 查询test中empno为800000的记录值,记录查询执行时间。
select * from test where empno in 800000;-- 0.128s
- 在test表的empno字段上创建索引
create index idx_test_empno
on test(empno);
select * from test;
- 重新执行第4题,对比查询时间
select * from test where empno in 800000;-- 0.039s
练习3
- 有如下关系模式,
student(sno,sname,gender,birthday,email);--学生
course(cno,cname,type,credit);--课程
sc(sno,cno,grade);--选课
试分析哪些列上适合创建索引?
create index idx_student_sno on student(sno);
create index idx_course_cno on course(cno);
create index idx_sc_sno on sc(sno);
课后作业
- 创建序列,起始位1,自增为1,最小值为1,最大值为9999
create sequence test_seq16_1
start with 1 -- 起始位1,默认为1 可不写
increment by 1 -- 自增为1,默认为1,可不写
minvalue 1 -- 最小值为1
maxvalue 9999; -- 最大值为9999
- 创建序列,起始值为50,每次增加5;
create sequence test_seq16_2
start with 50 -- 起始值为50
increment by 5; --自增为5
- 在表copy_dept中插入记录,其中部门号码采用上一步中创建的序列生成;
create table copy_dept2
as select * from dept
where 1 = 0;
insert into copy_dept2(deptno,dname)
values(test_seq16_2.nextval,'序列测试');
select * from copy_dept2;

- 请为工资创建索引,比较<10000,>1000,与round(sal)>10000,哪个索引有效,哪个索引无效;
create index idx_emp_sal on emp(sal); -- 0.018s
select * from emp where sal < 10000;-- 0.083s 有索引,因为顺序排序
select * from emp where sal > 1000;-- 0.040s 有索引,因为顺序排序
select * from emp where round(sal) > 10000;-- 0.041s 无索引,有函数
- 创建表,采用“create table copy_emp_index as select * from emp”,生成500万条数据,把其中的“员工号”字段修改为唯一;
create table copy_emp_index as select * from emp;
insert into copy_emp_index select * from copy_emp_index;
-- 执行23次
alter table copy_emp_index modify empno number(20);
--先要修改精度,否则报错 值大于为此列指定的允许精度
update copy_emp_index set empno = rownum;
- 查询表copy_emp_index表中员工号为200001的员工姓名,工资,记录执行时间;
select ename,sal from copy_emp_index where empno in 200001;
--3.488s
- 在copy_emp_index表的empno字段上创建索引,再次执行第6题语句,记录执行时间并做对比;
create index idx_copy_emp_index_empno
on copy_emp_index(empno);
select ename,sal from copy_emp_index where empno in 200001;
--0.313s
分割线
相关信息
以上就是我关于 Oracle初级测试题十六(17-17)--序列索引同义词 知识点的整理与总结的全部内容,希望对你有帮助。。。。。。。
Powered by Waline v2.15.4