2017-03-29 11 views
-1

제한된 수의 행을 가진 테이블을 만들고 싶습니다. 예를 들어 rownumber가 2.000보다 큰 테이블에 데이터를 삽입하려고하면 오류가 반환됩니다.행 수가 제한된 테이블 만들기

어떻게 관리하나요?

답변

1

삽입 된 행의 수를 검사하는 트리거를 작성하여 접근 할 수 있습니다. 작동 원리

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; 

: 같은 예를 들어,이 테이블

create table notManyRows(n number) 

가 있다고 가정하고 3 행의 수를 제한하려면, 당신은 트리거를 추가 할 수 있습니다

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> 
+0

1부터 2000까지 자동 증분되는 기본 키를 만들 수 있습니까? – Savke

+1

Oracle 버전에 따라 다릅니다. 그러나 어떻게 행을 삽입하지 못하게 할 수 있습니까? PK 값이 한계보다 큰지 확인하는 방아쇠가 필요합니다. 또한, 1 ... 19999의 ID를 가진 모든 행을 삭제하면 어떻게 될까요? – Aleksej

+0

대단히 감사합니다. 어떤 문제가 해결되면 매우 쉽게 보입니다 :) – Savke

관련 문제