📄 段湘南.txt
字号:
1,给一个表创建视图,该试图不包含约束为not null的列,怎么给该视图插入数据?对于not null约束的列,可以给个常量作为该列的默认值。
alter table student_010
modify age not null;
create view student_010-view
as
select id,name,sex from student_010;
create or replace trigger tri_stu_010_view
instead of insert
on student_010_view
for each row
declare
begin
case
when inserting then
insert into student_010(id,name,sex,age) values(:new.id,:new.name,:new.sex,18);
end case;
end;
2,创建一个序列
create sequence seq_id_010
minvalue 1
maxvalue 9999999
start with 1
increment by 1
cache 30;
3,用scott用户登陆,建一张表,至少包含id,name,sex,date列,插入1000000条数据,为了防止表名重复,建议大家用自己的名字做表名。用练习2创建的序列给该表的id加值,列id为该表的主键
给name列创建B树索引,按该列查询
给sex列创建位图索引,按该列查询
给date列创建函数索引,按该列查询
把查询的结果也写到你的答案里
create table d_x_n(id int,varchar2(20),sex char(2),day date);
alter table d_x_n add constraint pri_d_id primary key(id);
declare
ate number;
begin
for ate in 1..200000 loop
insert into d_x_n values(seq_id_010.nextval,'James','男',sysdate);
insert into d_x_n values(seq_id_010.nextval,'tom','男',sysdate);
insert into d_x_n values(seq_id_010.nextval,'Kobe','男',sysdate);
insert into d_x_n values(seq_id_010.nextval,'vivian','女',sysdate);
insert into d_x_n values(seq_id_010.nextval,'ann','女',sysdate);
end loop;
end;
create index d_x_n_name on d_x_n(name);
create bitmap index d_x_n_sex on d_x_n(sex);
create index d_x_n_day_tochar on d_x_n(to_char(day,'dd-mm-yyyy'));
select name from d_x_n where name = 'Kobe';
1 Kobe
2 Kobe
3 Kobe
4 Kobe
5 Kobe
6 Kobe
7 Kobe
8 Kobe
9 Kobe
10 Kobe
11 Kobe
12 Kobe
13 Kobe
14 Kobe
15 Kobe
16 Kobe
17 Kobe
18 Kobe
19 Kobe
20 Kobe
21 Kobe
22 Kobe
23 Kobe
24 Kobe
25 Kobe
26 Kobe
27 Kobe
28 Kobe
29 Kobe
select sex from d_x_n where sex = '女';
1 女
2 女
3 女
4 女
5 女
6 女
7 女
8 女
9 女
10 女
11 女
12 女
13 女
14 女
15 女
16 女
17 女
18 女
19 女
20 女
21 女
22 女
23 女
24 女
25 女
26 女
27 女
28 女
29 女
select day from d_x_n where to_char(day,'dd-mm-yyyy')=to_char(sysdate,'dd-mm-yyyy');
1 2007-6-14 下午 02:40:05
2 2007-6-14 下午 02:40:05
3 2007-6-14 下午 02:40:05
4 2007-6-14 下午 02:40:05
5 2007-6-14 下午 02:40:05
6 2007-6-14 下午 02:40:05
7 2007-6-14 下午 02:40:05
8 2007-6-14 下午 02:40:05
9 2007-6-14 下午 02:40:05
10 2007-6-14 下午 02:40:05
11 2007-6-14 下午 02:40:05
12 2007-6-14 下午 02:40:05
13 2007-6-14 下午 02:40:05
14 2007-6-14 下午 02:40:05
15 2007-6-14 下午 02:40:05
16 2007-6-14 下午 02:40:05
17 2007-6-14 下午 02:40:05
18 2007-6-14 下午 02:40:05
19 2007-6-14 下午 02:40:05
20 2007-6-14 下午 02:40:05
21 2007-6-14 下午 02:40:05
22 2007-6-14 下午 02:40:05
23 2007-6-14 下午 02:40:05
24 2007-6-14 下午 02:40:05
25 2007-6-14 下午 02:40:05
26 2007-6-14 下午 02:40:05
27 2007-6-14 下午 02:40:05
28 2007-6-14 下午 02:40:05
29 2007-6-14 下午 02:40:05
4,根据练习3的三种查询结果,创建三个视图,然后对这三个视图做相应的查询
create view d_x_n_name_view
as
select name from d_x_n where name = 'Kobe';
create view d_x_n_sex_view
as
select sex from d_x_n where sex = '女';
create view d_x_n_day_view
as
select day from d_x_n where to_char(day,'dd-mm-yyyy') = to_char(sysdate,'dd-mm-yyyy');
select * from d_x_n_name_view;
select * from d_x_n_sex_view;
select * from d_x_n_day_view;
结果同上。
5,看书的索引那章,把创建索引的要求写下来(如:不同索引的差别,那些列应该创建索引,创建什么索引等等)
索引与键的区别:索引是存储在数据库的一种存储结构,可以通过sql语句创建,修改和删除;而键只是逻辑上的概念,是与数据完整性约束概念相一致的。
索引包括 B树索引,位图索引,函数索引。
可以从以下几个方面来考虑创建B树索引还是位图索引;
列的基数特性;位图适用于那些基数比较小的列,如果某些列虽然基数较大,同时重复值也不多,但是如果他们经常被具有逻辑(and,or)查 询条件的where子句引用,同样应该创建位图索引。B树适用于基数较大的列。
列的null值特性:位图可以记录具有null的列,B树则忽略所有null列。
是否需要节省存储空间; B树索引会占用大量的存储空间。
查询条件的逻辑运算符特性:对于特殊类型的查询语句,位图索引比B树索引更有效。如包括and,or等。
查询条件的比较运算符特性:对于另外一些查询语句, B树索引能够比位图更有效,如经常使用<,>等比较运算符的查询列。
那些表应该创建索引:
一般不需要为很小的表创建索引;
对于比较大的表来说,如果经常查询的纪录数目少于表中所有记录的15%,则可以考虑为该表创建索引。
那些列应该创建索引:
应该为大部分列值不重复的列创建索引;
对于取值范围很大的列,应该创建B树索引,对于取值范围很小的列,应该创建位图索引;
对于包含很多null值,但是经常需要查询的所有非null值记录的列,应该创建索引;
对于包含很多null值,但是不会经常查询的所有非null记录的列,没有必要创建索引;
不能在long,blob等大对象数据类型的列上创建索引;
对于经常进行连接查询的多个表来说,在用于连接的列上创建索引能够显著提高查询速度;
oracle会自动为primary,unique列创建索引。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -