잠시만 기다려 주세요

     '참사 일주일이 지났다.. 민주당은 눈치만 보며 아무것도 하지 않는다... 세월호 시즌2가 되어간다.. 뉴스가 사라지고 점점 잊혀지고 있다. 민주당의 방조 살인이 또 다시 시작되고 있다.'
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (19)  |  여러가지 팁 (1095)  |  추천 및 재미 (163)  |  자료실 (28)  |  
시사, 이슈, 칼럼, 평론, 비평 (795)  |  끄적거림 (142)  |  문예 창작 (719)  |  바람 따라 (75)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    postgresql

postgresql - PostgreSQL의 트리거(TRIGGER)와 함수(function) 예제
이 름 : 바다아이   |   조회수 : 10713         짧은 주소 : https://www.bada-ie.com/su/?591591789061

PostgreSQL의 트리거와 펑션 예제를 만들어 봤다.

자료를 찾는데 너무 힘들어서 나중에 잊기전에 정리해봤다.

 

 

 

● 우선 테스트용 테이블 작성 

 

CREATE TABLE datalog(

    logtime timestamp PRIMARY KEY,

    data varchar,

    content varchar

);

 

* 테이블 지우기

DROP TABLE datalog;

 

● 펑션(Function) 함수 예제

    이 함수가 실행되면 지정 테이블의 레코드가 10개를 넘으면 가장 오래된 것부터 지운다.

 

CREATE FUNCTION data_fun() RETURNS trigger AS $data_fun$

    DECLARE

rowcount integer;

        delcount integer;

        maxcount integer;

    BEGIN

        maxcount := 10;

        SELECT count(*) logtime into rowcount  FROM datalog;

        delcount := rowcount - maxcount + 1;

 

        IF delcount > 0 THEN

   DELETE FROM datalog WHERE logtime IN (SELECT logtime FROM datalog ORDER BY logtime LIMIT delcount);

        END IF;

 

RETURN NEW;

    END;

$data_fun$ LANGUAGE plpgsql;

 

* 펑션 지우기

DROP FUNCTION data_fun();

 

● 트리거(Trigger) 함수 예제

    지정 Table에서 추가나 변경이 이루어질 때 지정 펑션이 실행된다.

 

CREATE TRIGGER data_trg BEFORE INSERT OR UPDATE ON datalog

    FOR EACH ROW EXECUTE PROCEDURE data_fun();

 

* 트리거 지우기

DROP TRIGGER data_trg ON datalog;

 

 

● 테스트용 데이터

 

insert into datalog(logtime, data) values('2015-01-01 12:12:01', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:02', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:03', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:04', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:05', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:06', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:07', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:08', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:09', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:01', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:02', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:03', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:04', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:05', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:06', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:07', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:08', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:09', 'aa');

 
위의 데이터를 입력하면 로그는 10개 행만 테이블에 저장된다.
데이터를 추가하기 전에 트리거의 연결된 펑션이 실행되어
테이블의 행이 10개를 넘어가면 가장 옛날 데이터를 삭제한다.


출처 : http://wwwi.tistory.com/348
 
| |





      1 page / 2 page
번 호 카테고리 제 목 이름 조회수
45 postgresql postgresql ... postgresql 14 .. postgresql.conf port 5432 .. 바다아이 1821
44 postgresql , count(*) .... 바다아이 8488
43 postgresql How to do an update + join in PostgreSQL?, 바다아이 7764
42 postgresql sequence(퀀) 바다아이 10234
41 postgresql , , , index create, , 바다아이 10227
40 postgresql postgresql log_timezone .... 바다아이 8274
39 postgresql postgresql SEQUENCE reset .... 바다아이 10198
38 postgresql [PostgreSql] WITH , , Operator 바다아이 9407
37 postgresql postgresql for, foreach , 바다아이 11189
36 postgresql postgresql , , into ... 바다아이 11326
35 postgresql postgresql PL/pgSQL - SQL Procedural Language, , 바다아이 12909
34 postgresql postgresql ... .. , , 바다아이 14442
33 postgresql postgresql CSV export/import 바다아이 10565
32 postgresql postgresql tablespace , .... 바다아이 15785
31 postgresql postgresql 10 partitioning, ... , ... 바다아이 11107
30 postgresql Using PostgreSQL Arrays, ... ... 바다아이 11145
현재글 postgresql PostgreSQL (TRIGGER) (function) 바다아이 10714
28 postgresql Optimize and Improve PostgreSQL Performance with VACUUM, ANALYZE, and REINDEX 바다아이 11324
27 postgresql postgresql tuple . vacuumdb .. , . 바다아이 10914
26 postgresql postgresql , .. 바다아이 11114
25 postgresql postgresql , size, 바다아이 13049
24 postgresql postgresql , , .... 바다아이 9889
23 postgresql PostgreSQL Replication, , , master, slave 바다아이 13466
22 postgresql postgresql case 바다아이 9948
21 postgresql postgresql with 바다아이 10393
20 postgresql postgresql , , string 바다아이 14338
19 postgresql Postgresql partitioning table , , , 바다아이 10761
18 postgresql PostgreSQL 바다아이 12389
17 postgresql postgresql vacuumdb, psql, pg_dump password crontab , pgpass 바다아이 12378
16 postgresql postgresql sequence 퀀 auto_increment . 바다아이 11324
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.18301
to webmaster... gogo sea. gogo sea.