1.自己新建表设置id自增
一:设置序列和触发器(必须一起使用,不需要设置id的值相当于Mysql的autoincrement)
序列:
create sequence a_sequence
minvalue 1 --最小值是1
nomaxvalue --没有最大值
start with 1 --从1开始
increment by 1 --每次增加1
nocycle; --不循环
触发器:
CREATE TRIGGER a_trigger BEFORE
insert ON a FOR EACH ROW --行触发器表明是a
begin
select a_sequence.nextval into:New.ID from dual;
end;
设置完序列和触发器后则可以直接保存
insert into a(name,age) values ('hzz',18);
二:设置序列(则需要设置id的值才能保存)
序列:
create sequence a_sequence
minvalue 1 --最小值是1
nomaxvalue --没有最大值
start with 1 --从1开始
increment by 1 --每次增加1
nocycle; --不循环
查询所有序列:select dbms_metadata.get_ddl('SEQUENCE',u.object_name) from user_objects u where object_type='SEQUENCE';
如果知道序列的名字则直接查询id就可:
SELECT a_sequence.NEXTVAL AS ID FROM DUAL;
查询后假设id是88,则需要赋值后保存
保存数据:insert into a(id,name,age) values (88,'hzz',18);