Oracle建表主键策略

1.Seq自增策略

Oracle创建表和主键自增

  创建表:

  create table tuser(

  id number(11) not null,

  name varchar2(20) not null,

  password varchar2(20)

  birthday date,

  constraint tuser_pk primary key (id)

  );

  创建序列:

  create sequence increase_seq increment by 1 start with 1 nomaxvalue 

nocycle cache 10;

  创建trigger

  create or replace trigger tuser_trigger

  before insert on tuser for each row

  begin

  select increase_seq.nextval into :new.id from dual;

  end;

  /

  根据使用的工具,可能需要增加“/”来执行PL/SQL块。

  测试:

  insert into tuser(name,password,birthday) values('wujay','123456',null);

  commit;

  select * from tuser;

  ID NAME PASSWORD BIRTHDAY

  ---------- -------------------- -------------------- --------------

  1 wujay 123456

  修改表:

  alter table tuser rename column id to pk_tuser;


2.UUID策略

1、创建表:

create table table1(
  id       VARCHAR2(32) default SYS_GUID() not null;

  name     VARCHAR2(32);

)

2、插入数据

insert into table1(ID)values(SYS_GUID(),"小王"); 
commit;

3、查询数据

select * from table1;

 

注:此方法也适用于存储过程,将SYS_GUID()保存为变量,可以作为查询条件使用。

 

create or replace procedure pro is
v_name varchar2(32);
v_uuid varchar2(32);
begin
        v_uuid := SYS_GUID(); 
        insert into table1(ID)values(SYS_GUID(),"小王");  
        commit;

        select name into v_name from table1 where id=v_uuid;

        dbms_output.put_line(v_name);

end pro;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值