同名对象:
connect yyaccp/accp;
drop user myuser;
create user myuser identified by myuser;
grant create synonym to myuser;
connect myuser / myuser;
create synonym mytable for yyaccp.mytable;
使用公共同名对象
Connect yyaccp/accp;
Grant create synonym to myuser;
Connect myuser/myuser;
Create public synonym jobs for yyaccp.jobs;
序列是用来生成唯一,连续的整数的数据库对象。序列是用来自动生成主键或唯一键的值。
以下代码创建了一个名为:emp_id的序列(序列号从10开始,自动增长为1,最大值为2000)
Create sequence mytable_id start with 10 increment by 1 maxvalue 2000
以下代码把 emp_id 序列产生的值插入到mytable表中
insert into mytable values(mytableid.nextval,''rose'',''beijing'',''rose@sdi.com'',date''2007-10-10'');
以下代码修改了mytable_id 序列的信息。重新设置了新的 maxvalue 值以及设置增量值为2
Alter sequence mytable_id increment by 2 maxvalue 5000;
********* 注意 :不能修改序列的 start with 参数。
以下代码演示了删除一个序列
Drop sequence emp_id;
可以通过查询user_sequence 字典表来获取有关序列的信息。
user_sequence 表中的部分列
Sequence_name
序列名
Min_value
最小值
Max_value
最大值
Increment_by
序列的增量
Cycle_flag
该序列是否循环,该值为Y或N
Order_flag
该序列是否有序,该值为Y或N
Cache_size
保存在内存中的序列值的个数
Last_number
该序列生成或缓存的最后一个数字
以下查询显示了user_sequence表中的详细信息
select * from user_sequences
删除序列:
drop sequence mytable_id;
视图:
视图是在一个或者多个表上的预定义查询。
以下代码基于employees2表创建一个名为: employee2_view的视图:
1. create view employee2_view as select * from employees2;
2. create view employee2_view as slect * from employees2 where division_id = ''BUS'' OR job_id=''PRE''
在视图中使用 with check option 。修改视图后在视图中不能显示修改的行。使用with check option 可以防止视图数据被修改。 create or replace view employee2_view as select * from employees2 where division_id = ''BUS'' OR job_id=''PRE'' with check option
创建只读视图(创建只读视图后,用户将不能修改视图):
create or replace view employee2_view as select * from employees2 where division_id = ''BUS'' OR job_id=''PRE'' with READ ONLY
创建带有错误视图:
下面语句创建一个基于表venmast 的视图 ,但是在数据库中并不存在名为“venmast”的表:
Create force view vtable as select * from viewtable;
创建表连接视图:
Create view emp2_job_divisions as
select e.employee_id,e.division_id,d.name as division_name,e.job_id,j.name as jobname,
first_name,last_name,salary
from employees2 e,jobs j,divisions d
where e.job_id = j.job_id
and e.division_id = d.division_id
键保留表:
在联接视图中,如果视图包含了一个表的主键,并且也是这个视图的的主键,则这个键被保留,这个表称为键保留表,oracle 可以通过此视图向表中插入行。包含外边连接的视图通常不包含键保留表
以下代码可以修改视图中的数据:
update emp2_job_divisions set first_Name = ''aaaaa'' where employee_id=1;
以下代码不能修改视图中的数据:
update emp2_job_divisions set jobname = ''aaa'' where employee_id=1;
使用索引
创建唯一索引:
以下代码在order_master 表的orderno 列上创建了一个名为order_no 的唯一索引:
create unique index mytableid on mytable(id)
创建组合索引:
组合索引是在表中的多个列上创建的索引。组合索引中列的顺序是任意的,不必是表中相邻的列。
以下代码在employee2 表中创建了组合索引:
Create index emp_first_last on employees2(first_name,last_name);
反向键索引:
反向键索引是一种特殊的索引,在索引基于含有序数的列时非常有用
以下代码在pemployee 表中创建了反向索引。
Create or replace index empid on emp(id) reverse;
位图索引适用于低基数列,也就是不同值的数目比表的行数少的列。
以下代码演示了在order_detail 表中创建位图索引。
Create bitmap index employees2_first_name on employees2(first_name);
索引组织表:
索引组织表与普通表的不同之处在于,该表的数据存储在与其关联的索引中。对表数据进行的修改,只会导致对索引的更新。
primary key 是索引组织表必须的。
以下代码创建了索引组织表:
create table ind_org_tab(
vencode number(4) primary key,
venname varchar2(20)
)
organization index;
函数索引:
create index employees2_firstname on employees2(lower(first_name));
查询索引数据:
select * from employees2 where lower(first_name) = ''john'';
索引中的分区:
create table order_mast(
orderno number(4),
venname varchar(20)
)
partition by range(orderno)(
partition oe1 values less than(1000),
partition oe2 values less than(2000),
partition oe3 values less than(maxvalue)
);
局部分区索引是在分区表上创建的一种索引,在局部分区索引中,oracle 为每一个分区建立了一个独立的索引.
在上面创建的分区表上创建局部索引。
Create index myind on order_mast(orderno) local;
查看索引信息:
select segment_name,partition_name,segment_type,tablespace_name from user_segments where segment_name=''MYIND'';
修改索引,以下代码重命名了索引。
Alter index vn_ind rename to c_vn_ind;
删除索引:
Drop index cvn_ind;
获取索引列信息:
Select index_name,table_name,column_name from user_ind_columns
Order by index_name,column_position;