Oracle-数据库对象的使用
一、序列
序列可以使用在数据库表的自动增长列中来用,假如我们的学生表的id想从1开始,然后自动增长,每次增加2,也就是学生id是1、3、5、7、9这种的,我们就可以配合序列来使用 序列有以下属性 sequence_name:序列名称 min_value:当前序列的最小值 max_value:当前序列的最大值 increment_by:每次序列增长的步长 cy:是否为循环序列 or:是否需要排序 cache_size:缓存个数,默认为20个 last_number:最后的数值 nextval:取得序列下一个内容,每调用一次序列的值都会增长 currval:表示取得序列的当前内容,每一次调用序列不会增长
1、创建一个序列
create sequence myseq;
2、向学生表插入一条数据,学生id使用myseq序列
insert into student(id,name) values (myseq.nextval;张三);
3、改变myseq序列的步长,每步增加2
create sequence myseq increment by 2;
4、改变myseq序列的开始值为10000
create sequence myseq increment by 2 start with 10000;
5、创建一个循环序列,并且不使用缓存
如果我们希望某一列的数据只是1到9的数据,那么我们可以使用循环序列来操作
create sequence myseq minvalue 1 maxvalue 9 cycle nocache;
二、同义词的作用
同义词可以将一个模式的表给另一个模式来访问。
1、将scott用户下的student表同义给sys用户使用
create sysnoym student for scott.emp;
2、将scott用户下的student表同义给所有用户使用
create public sysnoym student for scott.emp;
三、视图的定义及使用
1、创建一个视图,从学生表中名字为‘张三’的学生中取
create view studentview as select * from student where name = 张三;
2、查询学生视图
select * from studentview;
3、更新学生视图,将name为‘张三’的年龄更新为20
update studentview set age = 20 where name = 张三;
这个时候我们发现真实表student中张三的年龄也被修改了,所以这样修改视图会影响真实表的数据,那么我们接下来创建一个视图让他修改后不影响真实表。
4、创建一个视图,从学生表中名字为‘张三’的学生中取,并且修改视图不能影响原来真实表的数据
create or replace view studentview as select * from student where name = 张三 with check option;
5、创建一个视图,从学生表中名字为‘张三’的学生中取,并且视图设置为只读
create or replace view studentview as select * from student where name = 张三 with read only;
四、索引的定义及使用
1、创建一个学生表,并给name建立索引
create index name_index on student (name);
五、总结
文章中涉及到的命令大家一定要像我一样每个都敲几遍,只有在敲的过程中才能发现自己对命令是否真正的掌握了。