创建具有有限行数的表

时间:2017-03-29 09:56:49

标签: oracle11g oracle-sqldeveloper

我想创建行数有限的表。 例如,如果我们尝试将数据插入到rownumber大于2.000的表中,则返回一些错误或其他内容。

如何管理?

1 个答案:

答案 0 :(得分:1)

一种方法可以是创建一个触发器来检查插入的行数;例如,假设你有这张表

create table notManyRows(n number)

并且您希望将行数限制为3,您可以添加如下触发器:

create or replace trigger notManyRowsTrg
after insert on notManyRows
declare
    vCheck number;
begin
    select count(*)
    into vCheck
    from notManyRows;
    --
    if vCheck > 3 then
        raise_application_error(-20001, 'Too many rows in the table');
    end if;
end;

工作原理:

SQL> insert into notManyRows values (1);

1 row created.

SQL> insert into notManyRows values (1);

1 row created.

SQL> insert into notManyRows values (1);

1 row created.

SQL> insert into notManyRows values (1);
insert into notManyRows values (1)
*
ERROR at line 1:
ORA-20001: Too many rows in the table
ORA-06512: at "ALEK.NOTMANYROWSTRG", line 9
ORA-04088: error during execution of trigger 'ALEK.NOTMANYROWSTRG'


SQL>
相关问题