做到这一点的唯一方法是使用代码控制表......
create table code_control
(year number(4,0) not null
, type varchar2(1) not null
, last_number number(38,0) default 1 not null
, primary key (year,type)
)
organization index
/
......这样维护......
create or replace function get_next_number
(p_year in number, p_type in varchar2)
return number
is
pragma autonomous_transaction;
cursor cur_cc is
select last_number + 1
from code_control cc
where cc.year= p_year
and cc.type = p_type
for update of last_number;
next_number number;
begin
open cur_cc;
fetch cur_cc into next_number;
if cur_cc%found then
update code_control
set last_number = next_number
where current of cur_cc;
else
insert into code_control (year,type)
values (p_year, p_type)
returning last_number into next_number;
end if;
commit;
return next_number;
end;
/
重要的是SELECT ... FOR UPDATE。悲观锁定保证了多用户环境中的唯一性。PRAGMA 确保维护code_control
不会污染更广泛的事务。它允许我们在没有死锁的情况下在触发器中调用函数。
这是一个带有像您这样的键的表:
create table t42
(year number(4,0) not null
, type varchar2(1) not null
, id number(38,0)
, primary key (year,type, id)
)
/
create or replace trigger t42_trg
before insert on t42 for each row
begin
:new.id := get_next_number(:new.year, :new.type);
end;
/
在我填充之前,我什么都没有t42
:
SQL> select * from code_control;
no rows selected
SQL> select * from t42;
no rows selected
SQL> insert into t42 (year, type) values (2016, 'A');
1 row created.
SQL> insert into t42 (year, type) values (2016, 'A');
1 row created.
SQL> insert into t42 (year, type) values (2016, 'A');
1 row created.
SQL> insert into t42 (year, type) values (2016, 'B');
1 row created.
SQL> insert into t42 (year, type) values (2016, 'A');
1 row created.
SQL> insert into t42 (year, type) values (2017, 'A');
1 row created.
SQL> select * from t42;
YEAR T ID
---------- - ----------
2016 A 1
2016 A 2
2016 A 3
2016 A 4
2016 B 1
2017 A 1
6 rows selected.
SQL> select * from code_control;
YEAR T LAST_NUMBER
---------- - -----------
2016 A 4
2016 B 1
2017 A 1
SQL>
因此,对这种实现的明显反对意见是可扩展性。插入事务在code_control
表上被序列化。这是绝对正确的。然而,锁的持有时间尽可能短,因此即使该t42
表每秒填充多次,这也不应该成为问题。
但是,如果表受到大量并发插入的影响,则锁定可能会成为问题。表有足够的兴趣事务槽(INITRANS、MAXTRANS)来处理并发需求是至关重要的。但是非常繁忙的系统可能需要更智能的实现(可能批量生成 ID);否则放弃复合键以支持序列(因为序列确实在多用户环境中扩展)。