索引
索引的目的是提高存储数据页面的速度
服务器扫描索引以获得数据存储单元的地址
索引的优点为:提高查询的执行速度、实施数据的唯一性、加速了表之间的连接
索引的缺点为:创建索引需要花费较多的时间、每个创建的索引连同原先的数据源都需要空间来存储数据、每次修改数据时索引都要更新
--
按照索引数据的存储方式可以分为:
B*树索引
位图索引
按照索引个数分为:
单列索引
复合索引
索引列值唯一性可分为:
唯一索引
非唯一索引(默认)
--
Create [unique] index index_name
On table_name(column_name,column_name…)
[tablespace tablespace_name];
select * from emp where ename=‘SCOTT’
那么我们可以在ename列上创建B树索引,语句如下:
create index ind_ename on emp(ename) tablespace users
--
位图所以
以位置来表示索引数据
相比B树索引,位图索引可以节省大量磁盘空间
基于每个不同值建立一个位图
语法:
CREATE BITMAP INDEX idx_name
ON tab_name(col_name)
TABLESPACE space_name
例:经常执行语句:
Select count(*) from tab_name where gender = ‘F’
那么可以在性别列上建立位图索引:
Create bitmap index idx_gender
On tab_name(gender)
--
基于函数或表达式所建立的索引
语法:
CREATE INDEX idx_name
ON tab_name(func_name(col_name))
如:
create index funidx_emp_empno on emp substr(empno,1,2);
--
显示表的所有索引:
显示EMP表的索引信息
SELECT index_name, index_type, uniqueness
from user_indexes where table_name='EMP';
==================================================
视图是一个虚拟的表
视图提供一种安全机制,保证只能检索和修改他们看得到的数据
复杂查询的使用也可以通过视图来简化
为用户提供相关数据,提供一种安全机制
隐藏数据复杂性
从异构源组织数据,比如远程服务器和不是同一个数据库的表
减少对象的大小
创建视图
create view v_emp
as
select emp.*,dept.dname from emp,dept where emp.deptno=dept.deptno;
Select * from v_emp;
更新视图记录
Create or replace view view_name as subquery;
Rename <old_view_name> to <new_view_name>
增加 with read only,则视图只能查询,不能修改。
如:
create view aa
as
select * from emp with read only;
insert into aa (empno,ename) values(8888,'abc');
======================================
sqlplus基础
格式化结果集的命令包括column、pause、pagesize、linesize、feedback、numformat和log等
Descirbe命令返回数据库中所有存储对象的描述
语法格式为:descirbe object_name;
可以缩写为desc
help命令
help index
help copy
=======================================
分区的方法
范围分区
HASH分区(散列分区)
列表分区
复合分区(范围+HASH)(范围+列表)
CREATE TABLESPACE TEST1 DATAFILE 'C:/TEMP/TEST1.DBF' SIZE 10M
CREATE TABLESPACE TEST2 DATAFILE 'C:/TEMP/TEST2.DBF' SIZE 10M
CREATE TABLESPACE TEST3 DATAFILE 'C:/TEMP/TEST3.DBF' SIZE 10M
范围分区
Create table material_test
(transaction_id number primary key,
Item_id number(8) not null,
Item_description varchar2(300),
Transaction_date date not null)
Partition by range(transaction_id)
(partition part_01 values less than(30000000) tablespace test1,
partition part_02 values less than(60000000) tablespace test2,
partition part_03 values less than(maxvalue) tablespace test3)
HASH分区
Create table material_test2
(transaction_id number primary key,
Item_id number(8) not null,
Item_description varchar2(300),
Transaction_date date not null)
Partition by hash(transaction_id)
(partition part_01 tablespace test1,
partition part_02 tablespace test2,
partition part_03 tablespace test3)
列表分区
Create table material_test3
(transaction_id number primary key,
Item_id number(8) not null,
Item_description varchar2(300),
Transaction_date date not null,
city varchar2(100))
Partition by list(city)
(partition part_01 values('北京') tablespace test1,
partition part_02 values('上海') tablespace test2,
partition part_03 values(default) tablespace test3)