数据库对象
<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