csjoz11

导航

统计

Hana 通过序列来创建自增

    • sql server
      • 1
        2
        3
        4
        5
        6
        7
        8
        IF OBJECT_ID( 'Autotest.SA.CUSTOMER' , 'U' is not null
            Drop table Autotest.SA.CUSTOMER;
        CREATE TABLE Autotest.SA.CUSTOMER (
        customer_id  int IDENTITY (1,1)  PRIMARY KEY ,
        c_custkey nVarChar(50)  default '' ,
        c_mktsegment nVarChar(50)  default '' ,
        c_privilege_level  int
        );
      • 使用IDENTITY(m,n)
        • m表示的是初始值,n表示的是每次自动增加的值
        • m和n的值都没有指定,则默认为(1,1)
    • mysql
      • 1
        2
        3
        4
        5
        6
        7
        8
        drop table if exists Autotest.CUSTOMER;
         
        CREATE TABLE autotest.CUSTOMER (
        customer_id  int auto_increment  PRIMARY KEY ,
        c_custkey nVarChar(50)  default '' ,
        c_mktsegment nVarChar(50)  default '' ,
        c_privilege_level  int
        );
      • 使用auto_increament

        • 必须指定索引,上面的例子用PRIMARY KEY定义了主键索引
        • 默认自增从1开始  
        • 设置自增的起始值n:  alter table table_name AUTO_INCREMENT=n  
  • Oracle
    • 通过序列+触发器建立自增序列
    • 创建table
    • 1
      2
      3
      4
      5
      create table SYSTEM.customer(
      id  int not null PRIMARY KEY ,
      column1  varchar (50)  null ,
      column2  varchar (50)  null
      )
    • 创建序列
    • 1
      2
      3
      4
      5
      6
      create sequence seq_perftest
      minvalue 1
      maxvalue 99999999
      start  with 1
      increment  by 1
      cache 50<br><br> drop sequence seq_perftest

        起始值为1,增量为1

    • 创建触发器
    • 1
      2
      3
      4
      5
      6
      create or replace trigger "perfest_trig"
          before  insert on SYSTEM.customer
          for each row
      begin
        select seq_perftest.nextval  into :new.id  from dual;
      end ;
    • 验证自增是否生效  insert into system.customer(column1,column2)  values('test1','test2')
    • 用存储过程批量生成数据
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      create procedure system.pro_test(
      init  in number,
      loop_time  in number
      )
      IS
      vars number;
      INTEGER ;
      begin
       vars := 0;
       i := init;
        while vars<br> exec system.pro_test (1,100000)
  • Hana
    • 通过序列来创建自增
    • 创建table
    • 1
      2
      3
      4
      5
      create table SYSTEM.customer(
      id  int ,
      column1  varchar (50)  null ,
      column2  varchar (50)  null
      );

        

    • 创建序列
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      create sequence system.seq_perftest
      increment  by 1
      maxvalue 99999999
      minvalue 1
      NO CYCLE
      start  with 1;
       
       
      drop sequence seq_perftest;

        

    • 验证自增是否生效  insert into system.customer(id, column1) values(system.seq_perftest.nextval, 'test123')
    • 使用存储过程批量增加数据
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      create procedure system.pro_test(
      INTEGER ,
      loop_time  INTEGER
      )
      As
      begin
        For in 1 .. loop_time DO
         insert into system.customer(id,column1,column2) values (system.seq_perftest.nextval,( 'test' ||i),( 'test' ||i);
          i := i+1;
        end FOR ;
      end ;
       
       
       
      call system.pro_test(1, 100000)

        

posted on   csjoz11  阅读(856)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示