zoukankan      html  css  js  c++  java
  • oracle中如何指定表字段自增

    MySql中可以使用“auto_increment”即可。但是oracle有点麻烦,需要使用序列和触发器达到目的。

    --用户表
    create table userTable (
    id int,
    username varchar(20),
    password varchar(20),
    constraint pk_userTable primary key(id));

    -- 序列 (序列与触发器实现userTable表中id字段的自动增长)
    create sequence userTable_id_autoinc
    minvalue 1
    maxvalue 9999999999999999999999999999
    start with 1
    increment by 1
    nocache;


    --触发器 (序列与触发器实现userTable表中id字段的自动增长)
    create or replace trigger insert_userTable_id_autoinc
    before insert on userTable
    for each row
    begin
    select userTable_id_autoinc.nextval into :new.id from dual;

    end;

    -- 测试结果

    SQL> insert into school values(100,'a',0,001,001,'964955634@qq.com',66666);


    1 row created.

  • 相关阅读:
    linux脚本mysql服务与keepalived服务高可用脚本
    linux脚本启动服务脚本
    linux脚本数据库备份脚本
    linux脚本清理日志文件脚本
    最大数字
    求阶乘
    素数
    水仙花数
    js基础1
    css常用属性2
  • 原文地址:https://www.cnblogs.com/tongying/p/14464361.html
Copyright © 2011-2022 走看看