首页 > 数据库 >Oracle创建自增列的两种方式

Oracle创建自增列的两种方式

时间:2023-03-10 14:12:24浏览次数:49  
标签:info -- 创建 into cache 增列 序列 user Oracle

Oracle含有序列的事务进行回滚,序列不会回到事务前状态。

-- 方法一: 序列
-- Created on 2023/3/10 by 112691 
declare 
  -- Local variables here
  v_cnt number;
begin
  -- Test statements here
  select count(*) into v_cnt from user_tables where table_name = upper('user_info');
  if v_cnt>0 then
    execute immediate 'drop table user_info';
    dbms_output.put_line('该表已删除');
  else
    execute immediate 'create table user_info(
                      id number(10) not null,
                      username varchar(20) not null,
                      constraints PK_USERINFO primary key(id)
                      )';
    dbms_output.put_line('该表已创建成功');
  end if;
end;

create sequence USER_INFO_SEQ
start with 1
minvalue 1
maxvalue 999999
increment by 1
-- cache: 申请序列时Oracle将序列提前生成cache x先存入内存。在发送大量的申请序列请求时,会优先去内存中取预存序列。但如果数据库重启时,预存内存中的序列值会被丢失,再次启动时候,预存重上次内存中最大序列号+1开始cache。	
cache 20; 

insert into user_info values(user_info_seq.nextval,'username1');
insert into user_info values(user_info_seq.nextval,'username2');
-- 方法二: 序列+触发器
create or replace trigger TIG_USER_INFO_INSERT
  before insert
  on user_info
  for each row
declare
  -- local variables here
begin
  select user_info_seq.nextval into :new.id from dual;
end TIG_USER_INFO_INSERT;

insert into user_info(username) values('username3');
commit;

drop table user_info ;
drop sequence USER_INFO_SEQ;

参考博客:
https://blog.csdn.net/qq_45554909/article/details/116912915
https://blog.csdn.net/fn0723/article/details/81104861

标签:info,--,创建,into,cache,增列,序列,user,Oracle
From: https://www.cnblogs.com/wuzimeimei/p/17203155.html

相关文章

  • powershell创建文件/文件夹
     powershell创建文件夹 powershell可以直接创建多层文件夹1.使用mkdir  mkdir"D:\a\b\c\d"如果文件夹已存在则报错  2.使用New-Item New-Item-PathD:......
  • LoadRunner——安装教程以及创建与录制(一)
    theme:channing-cyan1.loadrunner12|loadrunner12官方版下载(附安装教程)+网盘下载+汉化包CSDN下载及安装教程:https://blog.csdn.net/weixin_41585557/article/detai......
  • jquery创建添加append、prepend、appendTo、prependTo、after、insertAfter、before、
    ​​​​全栈工程师开发手册(作者:栾鹏)​​jquery系列教程2-DOM操作全解​​jquery创建添加元素jquery支持直接使用h5代码作为参数创建元素,将元素添加到dom树中append、p......
  • oracle---explain plan FOR 索引使用
        explainplanFORselect1from表名xxxx;selectplan_table_outputfromTABLE(DBMS_XPLAN.DISPLAY('PLAN_TABLE'));......
  • pytest---创建临时文件来存储测试数据(tmpdir)
    前言在跑自动化测试中,测试过程中会用到一些测试数据,其中这些测试数据包括临时测试数据和常用到的数据,经常用到的数据,我们可以通过Excel或者yaml文件的方式进行存储,那......
  • oracle19c rac升级补丁至19.18
    更新opatch,根据README要求,opatch工具版本至少12.2.0.1.34或更高版本,两节点oracle用户和grid用户都需要更新opatch[oracle@test02:/home/oracle]$cd$ORACLE_HOME/OPa......
  • oracle触发器详解
    原文地址: https://www.cnblogs.com/programmer-wind/archive/2011/09/10/2919585.html触发器是许多关系数据库系统都提供的一项技术。在ORACLE系统里,触发器类似过程和函......
  • laravel migration创建表
    一,phpartisanmake:magrationcreate_test_table命令行字段如何设置 二,phpartisanmigrate命令行如果出现错误就添加两行代码最后成功了 ......
  • java-IO-java类创建功能
       ......
  • Oracle定时任务之Job和Dbms_job(转)
    感谢老哥,解决了我的疑惑https://www.cnblogs.com/yscit/p/10376005.htmlhttps://www.cnblogs.com/ybhcolin/archive/2013/04/17/3026646.html一、概述Oralce中的任务有2......