oracle中的自动增量
我是一名学生 i正在执行分配,执行自动递增 如何在Oracle中创建oracle中的自动增量,oracle,auto-increment,Oracle,Auto Increment,我是一名学生 i正在执行分配,执行自动递增 如何在Oracle中创建自动增量 CREATE TABLE mua_thi ( mamuathi varchar2(10) not null, check(mamuathi like 'MT%') ) mamuathi = MT + auto_increment; create or replace trigger tangmuathi before insert or update on mua_thi begin set new.m
自动增量
CREATE TABLE mua_thi
(
mamuathi varchar2(10) not null,
check(mamuathi like 'MT%')
)
mamuathi = MT + auto_increment;
create or replace trigger tangmuathi
before insert or update
on mua_thi
begin
set new.mamuathi := MT + muathitang.nextval from Dual;
end;
create sequence muathitang start
with 1 increment by 1;
不要那样安排你的桌子。这是一个智能钥匙(由多个组件连接的单个字符串)。智能钥匙是哑的。如果“MT”很重要(为什么键带有硬编码的、不变的元素?)
CREATE TABLE mua_thi ( mamuathi varchar2(2) not null
, id number (8) not null
, primary key (mamuathi, id )
, check(mamuathi = 'MT')
);
实际上那里还有一些不好的做法。第一,说出限制条件——它让生活更轻松:
, constraint mt_pk primary key (mamuathi, id )
, constraint mt_ck check(mamuathi = 'MT')
第二,如果mamuati
确实是一个常量,那么在键中使用它是没有意义的:
, constraint mt_pk primary key ( id )
第三,mamuati
可能会演变成几个值,所以请考虑一下查找表的外键是否更好
显然,拆分智能钥匙的缺点是需要引用多个列。在11g中,我们可以使用虚拟列功能来避免这种不一致:
CREATE TABLE mua_thi ( mamuathi varchar2(2) not null
, id number (8) not null
, mamuathi_disp AS mamuathi||lpad(id,8,'0')
, primary key (mamuathi, id )
, check(mamuathi = 'MT')
);
CREATE TABLE mua_thi ( mamuathi varchar2(2) not null
, id number (8) not null
, mamuathi_disp AS mamuathi||lpad(id,8,'0')
, primary key (mamuathi, id )
, check(mamuathi = 'MT')
);