森森快跑

走得累,走得苦,那是因为在走上坡路。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

不同数据库自增字段的创建实现举例

Posted on 2015-11-04 16:34  森森快跑  阅读(195)  评论(0编辑  收藏  举报

1、MySQL: create table tableName(id int auto_increment,name varchar(20),primary key(id));
      alter table tableName auto_increment=1;


2、SQLServer: CREATE TABLE tableName (id int identity(1,1), name varchar(10));


3、Oracle: 创建序列,通过序列实现。
  创建序列:create sequence autoID increment by 1 start with 1 maxvalue 999999 cycle;
  插入数据:insert into tablename values(autoID.nextval,...);

  或者通过在序列的基础上创建触发器
  create or replace trigger tableName_trigger
    before insert on tableName
    for each row
    begin
      select autoID.nextval into :new.id from dual;
    end ;
  插入数据:insert into tablename (name) values('');