第11节其他数据库对象
表table 存储数据的主要形式 由行和列组成
视图view 存储起来的select语句
序列sequence提供了一系列有规律的数值,主要用于给表的逐渐
供值
索引index 用于提高查询效率
同义词synonym 可以给数据库对象起别名
--1序列
create sequence emp_seq
increment by 10
start with 1
maxvalue 100
cycle
nocache
--NEXTVAL 应在 CURRVAL 之前指定
--否则会报CURRVAL 尚未在此会话中定义的错误。
select EMP_SEQ.currval from dual
select emp_seq.nexttval from dual
select * from employees
--如何使用序列给表的主键提供值
update table emp(
id number(5),
name varchar2(10),
salary number(8,2),
constraint qemid_pk primary key(id)
)
select * from emp
create sequence emp_seq1
increment by 1
start with 1001
nocycle
insert into emp
values(emp_seq1.nextval,'AA',700)
select * from emp
--使用序列可能出现的问题
--裂缝1
--多个表供用一个序列
create table emp1(
id number(5),
name varchar2(10),
salary number (8,2),
constraint emp1_id_pk primary key(id)
)
insert into emp1
values(emp_seq1.nextval,'SS',800);
insert into emp
values(emp_seq1.nextval,'DD',333);
select * from emp
--情况2:有回滚数据的情况出现
commit;
select * from emp
insert into emp
values(emp_seq1.nextval,'FF',324);
rollback
--修改序列
select emp_seq.nextval from dual
alter sequence emp_seq
increment by 1
maxvalue 60
cycle
--只能修改非start with的信息
alter sequence emp_seq
--start with 10
increment by 1
maxvalue 60;
--删除属性
drop sequence emp_seq
--2.索引(index)
select * from
employees
where employee_id =204
--2索引的作用
一方面,当通过索引作用的列对表进行查询时,可提高查询的效率。
(原因:减少了对磁盘的IO)
另一方面,如果对表中索引作用的列进行相关的DML操作的同时,还需要维护一个索引。
--2).如何创建一个索引:①自动创建 ②手动创建
--自动创建:在定义 PRIMARY KEY 或 UNIQUE
--约束后系统自动在相应的列上创建唯一性索引
手动创建
create index emp_salary_idx
on employees(salary);
select * from employees
where salary = 8000
--3).删除索引:①当索引作用的表或列不存在了,相应的索引也就被删除了
--②手动的删除一个索引
drop index emp_salary_idx
--4).查询索引(了解)
SELECT ic.index_name, ic.column_name,
ic.column_position col_pos,ix.uniqueness
FROM user_indexes ix, user_ind_columns ic
WHERE ic.index_name = ix.index_name
AND ic.table_name = 'EMPLOYEES';
--同义词synoym
select * from employees
create synonym e for employees
select * from e
drop synonym e
测 试
--创建序列dept_id_seq,开始值为200,每次增长10,最大值为10000
create sequence dep_id_seq
start with 200
increment by 10
2. 使用序列向表dept中插入数据
a) INSERT INTO dept
b) VALUES(dept_id_seq.nextval,’Account’);
create table dept as
select department_id id , department_name name
from departments
where 1=2
(尚硅谷)Orcale 其他对象
最新推荐文章于 2024-03-21 14:13:41 发布