Oracle数据库的表格怎样才能想MySQL一样整数主键拥有自动增加功能呢?
前提你得创建一个表
create table tab{
id number not null primary key,--主键
.
.
.
}
然后,得创建一个序列
-- Create sequence
create sequence SEQ_tab --- SEQ_tab序列的名称
minvalue 1 --输入最小值
maxvalue 99999999999999999999999 --输入的最大值
start with 21
increment by 1 --以 1 为单位增加或减少
cache 20 --缓存
order; --排序方式
然后,建立一个触发器(在你插入一行数据之前触发)
1 create or replace trigger tab_trig 2 before insert on tab 3 for each row 4 declare 5 -- local variables here 6 begin 7 select seq_customer.nextval into :new.id from dual; 8 end tab_trig;
最后,插入一条数据测试(注意不要往id 字段(被要求自动添加列的字段)插)