> For the complete documentation index, see [llms.txt](https://docs.tibero.com/tibero-manuals/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tibero.com/tibero-manuals/7.2.5.manuals/tibero-development-guide/trigger.md).

# 트리거의 사용

## 개요

트리거(Trigger)는 스키마 객체의 일종으로, 데이터베이스가 미리 정해 놓은 특정 조건이 만족되거나 어떤 동작이 수행되면 자동으로 실행되도록 정의한 동작입니다. 예를 들어 데이터베이스에 특정한 이벤트가 발생하거나 사용자가 설정한 DDL 문장이 수행될 때 트리거가 실행될 수 있습니다.

트리거의 내용은 PSM으로 이루어져 있습니다. 트리거는 이미 정의된 PSM 객체를 호출하여 사용하거나 트리거를 생성할 때 이름 없는 블록을 함께 선언해주는 방식을 모두 이용할 수 있습니다.

### 트리거 구성요소

트리거는 다음과 같은 세 가지 구성요소를 갖춰야만 생성할 수 있습니다.

* 트리거가 실행될 조건이 되는 문장이나 이벤트
* 실행 조건의 제약
* 실행될 내용

다음은 **alarm\_for\_balance**라는 트리거를 생성하는 예입니다.

```
CREATE OR REPLACE TRIGGER alarm_for_balance 
BEFORE INSERT OR UPDATE ON balance_tab                  ... ⓐ ...
FOR EACH ROW
WHEN (new.balance < 3000)                               .. ⓑ ...
      CALL alarm_for_balance_fn()                       .. ⓒ ...
```

ⓐ 테이블 balance\_tab의 balance 컬럼에 로우가 삽입되거나 수정이 발생했을 때 ⓑ로 이동합니다.

ⓑ 해당 로우의 값이 3000 이하인지를 검사합니다.

ⓒ 검사한 결과가 맞으면 alarm\_for\_balance\_fn 함수를 호출하고, 함수에 정의한 동작이 실행됩니다.

### 트리거 타입

트리거의 타입은 다음과 같이 나눌 수 있습니다.

* 로우(row) 및 문장(statement)

<table><thead><tr><th width="132">타입</th><th>설명</th></tr></thead><tbody><tr><td>로우</td><td><ul><li>테이블에 INSERT, UPDATE, DELETE가 발생하는 로우마다 트리거의 내용이 실행되는 타입</li><li>이 타입의 트리거는 각 로우에 연산이 발생할 때마다 연산 직전 또는 직후에 트리거가 실행됨</li></ul></td></tr><tr><td>문장</td><td>로우의 개수에 상관없이 문장 단위로 한 번만 실행되는 타입</td></tr></tbody></table>

* BEFORE 및 AFTER

<table><thead><tr><th width="135">타입</th><th>설명</th></tr></thead><tbody><tr><td>BEFORE</td><td>조건 문장이 실행되기 전에 트리거의 내용이 실행되는 타입</td></tr><tr><td>AFTER</td><td>조건 문장이 실행된 후 트리거의 내용이 실행되는 타입</td></tr></tbody></table>

트리거는 두 종류의 타입 중에서 하나씩을 각각 가질 수 있습니다. 즉, BEFORE 로우(BEFORE row), BEFORE 문장(BEFORE statement), AFTER 로우(AFTER row), AFTER 문장(AFTER statement)의 타입으로 생성할 수 있습니다.

## 트리거 생성

트리거를 생성하는 방법은 다음과 같습니다.

```
CREATE [OR REPLACE] TRIGGER 트리거_이름
        {BEFORE | AFTER} {INSERT | UPDATE | DELETE} ON 테이블_이름
[FOR EACH ROW]
WHEN (조건_제약)
{[선언부] BEGIN
... END;} |
CALL 함수_또는_프러시저_이름
```

{% hint style="info" %}
**참고**

트리거의 문법에 대한 자세한 내용은 "Tibero SQL 참조 안내서"를 참고합니다.
{% endhint %}

다음은 로우 타입의 트리거로, 테이블 BODY\_LOGS의 WEIGHT 컬럼 값이 1000을 초과할 때마다 로그를 기록하도록 작성한 예입니다.

**\[예 1] 트리거의 생성**

```
CREATE TABLE Deck_tbl (
    Deck_no    NUMBER PRIMARY KEY,
    Deck_name  VARCHAR2(100),
    count      NUMBER
);

-- 로그 테이블 생성 (오버플로우 기록용)
CREATE TABLE Deck_log (
    Deck_id    NUMBER,
    Timestamp  TIMESTAMP,
    New_count  NUMBER,
    Action     VARCHAR2(20)
);

CREATE OR REPLACE TRIGGER Log_overflow
AFTER UPDATE ON Deck_tbl
FOR EACH ROW
WHEN (new.count > 1000)
BEGIN
    INSERT
    INTO Deck_log (Deck_id, Timestamp, New_count, Action)
    VALUES (:new.Deck_no, SYSTIMESTAMP, :new.count, 'overflow');
END;
/

INSERT INTO Deck_tbl VALUES (101, 'Main Deck', 500);
SELECT * FROM Deck_log; -- 0 row selected.

UPDATE Deck_tbl SET count = 1200 WHERE Deck_no = 101;
SELECT * FROM Deck_log; -- 1 row selected.
```

CREATE 문장은 Tibero 내부에서는 BEGIN – END 사이의 부분을 PSM으로 인식합니다. 이 문장이 컴파일되면 PSM 스키마 객체가 생성되고 이를 데이터베이스에 저장합니다. 만약 컴파일 에러가 발생한다면 정적 뷰를 통해 에러의 내용을 확인할 수 있습니다.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.tibero.com/tibero-manuals/7.2.5.manuals/tibero-development-guide/trigger.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
