ORACLE第四天txt.doc
文本预览下载声明
数据库对象
1表(约束)
2如何自动编号
SQLserver
--IDENTITY属性
create table test(
xh int identity(1,2) primary key,
name varchar(20)
);
insert into test(name) values (mike);
ORACLE
一个对象(序列sequence)
--最简单的一个序列,从1开始每次增加1,最大值38位精度10的38次方
和SQLSERVEr中的IDENTITY(1,1)类似
create sequence seq1;
访问其中的值
使用2个伪列 nextval,currval
select seq1.nextval from dual;--新值
select seq1.currval from dual; --当前值
从3开始每次增加2
3,5,7,9
create sequence seq2
start with 3
increment by 2;
从5开始每次增加5,最大值30,最小值是1
循环序列(到最大值后回到最小值)
5,10,15,20,25,30,1,6,11,......
create sequence seq3
start with 5 --起始值
increment by 5 --步长
maxvalue 30 --最大值
minvalue 1 --最小值
cycle --循环
cache 4 --缓存4个数
默认值cache是20个
取5的时候,内存中已经算出了10,15,20,25
取10的时候,直接取内存中的数
如何用到表中
create table testa(
xh number(4) primary key,
nm varchar2(20));
xh字段要实现自动编号
insert into testa values (seq3.nextval,MIKE);
insert into testa values (seq3.nextval,JOHN);
自动编号 保证唯一性 实际中一般用它做主键
a) 对emp新加入的员工的编号是7944,7954,7964,....
--建立sequence
create sequence s_emp
start with 7935;
--使用 nextval
insert into emp(empno,ename) values (s_emp.nextval,张三);
insert into emp(empno,ename) values (s_emp.nextval,李四);
b) 排名次
把员工按工资高低按名次排列
--加一个名次字段ord
alter table emp add (ord number(3));
--更新ord(名次=工资比我高的人数+1)
update emp a set ord = (select count(*)+1
from emp where sal a.sal);
--删除序列seq1
drop sequence seq1;
3 视图(VIEW)
create or replace view 名字 as
select 语句
例子
create or replace view emp_view as
select ename,empno,sal from emp;
select * from emp_view;
转化为一个子查询来执行
select * from (select ename,empno,sal from emp);
视图中是否存放数据???
不存放数据,存放的是查询语句
隐藏数据
通过视图能改表中的数据吗??
是可以的,但是有条件限制的
限制条件是:建立视图的查询语句必须是一个简单的select(只查询一个表,并且不含有分组函数),就可以改表中的数据了
select * from emp_view;
update emp_view set sal = 3000;
select a.*,(select count(*) from
emp where deptno = a.deptno) as rs
from dept a;
create or replace view d
显示全部