您的位置:首页 > 数据库 > Oracle

Oracle中添加自动编号的序列

2015-02-06 15:40 162 查看
1. 创建表T_Test

create table T_Test(id int ,address char(25), pay int);

2. 创建自增序列

create sequence SEQ_T_Test_ID //创建名为zc的序列

increment by 1 //自增长度为1

start with 1 //从1开始计数

minvalue 1 //最小值为1

nomaxvalue //没有最大值

nocache; //不设置缓存

3. 为表T_Test创建触发器

create or replace trigger T_Test_id //将触发器绑定在 id 这一列

before insert

on T_Test

for each row

when(new.id is null)

begin

select SEQ_T_Test_ID.nextval into:new.id from dual;

end;

4. 插入数据

insert into T_Test (address,pay) values('anh3u1i',345);

或者

insert into T_Test values(SEQ_T_Test_ID.nextval,'anh3u1i',345);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: