0

我是一个学生

我做作业,做auto increment

如何auto increment 在 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.mamuathi := MT + muathitang.nextval from Dual;
end;

create sequence muathitang start 
with 1 increment by 1;
4

1 回答 1

2
mamuathi = MT + auto_increment;

不要这样组织你的桌子。这是一个智能键(连接多个组件的单个字符串)。智能钥匙很笨。如果“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')

二,如果mamuathi真的是一个常数,那么在键中使用它是没有意义的:

, constraint mt_pk primary key ( id )

三、 mamuathi可能会演变成几个值,所以想想查找表的外键是否会更好。

显然,拆分智能密钥的缺点是需要引用多个列。在 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')
  );
于 2013-05-07T10:06:08.133 回答