2013년 8월 31일 토요일

오라클 트리거, oracle trigger

오라클 Trigger


구로디지털단지역 오엔제이프로그래밍 실무교육센터
(Java , Oracle, SQL, Oracle Tuning, BackUP& Recovery, ASP.NET, C#, C#Network ,채용확정 무상교육)  

www.onjprogramming.co.kr  오라클자바커뮤니티


1. Trigger란?
- 임의의 Table에 대해 Insert/Update/Delete 등의 SQL이 실행되면
 암시적으로 실행되는 프로시저
- Trigger는 관련된 Table과는 별개로 DB내에 저장된다.
- Trigger는 뷰가 아닌 Table에 대해서만 정의가 가능하다. 그러나
 View의 기본 Table에 있는 Trigger는 View에 대해 Insert/Update/Delete
 가 발생하게 되면 동작한다.

** Oracle Forms에서의 Trigger와는 구분되어야 한다.
  Forms에서 애기되는 Trigger는 일반 Tools(VB,,,)의
  Event와 같은 개념이다. 즉 Oracle Forms 응용프로그램의
  한부분이며 특정 Trigger 지점이 특정 Forms 응용프로그램
  내에서 실행될때만 수행된다.
  그와반해 DB Trigger는 Table에 대해
  Insert/Update/Delete시 할 행위를 정의하는 것이다.

 
2. Trigger의 구성
a. Triggering Event 또는 명령문
  - Trigger를 실행시키는 SQL 명령
  - 예를들면 아래와 같은것이 해당된다.
    create trigger 다음의 bdfore update of amount on sales.... 
    이는 sales Table에 대해  amount column의 변경될때 동작하는
    Trigger를 만든다는 의미이다.

b. Trigger 제한사항
  - 트리거 제한사항의 TRUE일때 Trigger는 동작한다.
  - 각 행당 실행되는 트리거에 대해 사용가능한 Option으로
    조건에 따라 Trigger 실행을 제어한다.

    when (new.amount < old.amount)
 
    즉 이같은 조건은 갱신되는 금액이 이전의 값보다 작을때
    Trigger를 실행하라는 의미이다.

c. Trigger 작업
  - PL/SQL Code를 포한하는 Trigger 처리부

d. Trigger 유형
  - 행 트리거
    에를들면 Update 명령이 Table의 여러행에 실행될때
    행 트리거는 Update 명령에 의해 영향을 받는 각행에 한번씩
    실행된다.

  - 명령문 트리거
    Table에 대해 명령에 영향을 받는 행에 관계없이 한번 실행된다.
   

e. Before대 after Trigger
  - Before Trigger
    명령문이 수행되기 전에 실행되는 Trigger
    Insert또는 Update 명령문을 완료하기전에 특정 열의 값을
    구할때 사용한다.
  - After Trigger
    명령문이 수행된 후에 실행되는 Trigger
   

f. Trigger예제


아래의 Trigger는  Statement Trigger임(Statement Trigger)
Oracle Tip에 Trigger Mutation Error처리예가 있는데
아래의 Trigger도 처음 작성시 sa_contrct Table에 대해
Mutating Error가 발생하여 Package를 만들어 구성한 예이다.
Mutate Error에 대해 궁굼하다면 Oracle Tip을 살펴보시면
설면해 놓았습니다.

/*  **********************************  */
/*        계약변경시 매출,입금원장 변경  */
/*  **********************************  */
create or replace trigger tr_sa_contrct
after insert or update on sa_contrct
declare
begin
 for i in 1..pkg_sa_contrct.sa_contrct_index loop
  if    inserting      then
      /************* 매출원장 생성 **************/
      insert into sa_ledger 
              (contrct_ilja,  emp_id,    cust_cd,    brand_cd,  adv_cd,
              contrct_gbn,    supply_amt, receipt_amt, regi_ilja,  modi_ilja, misu)
      values
              (pkg_sa_contrct.contrct_ilja_new(i),
              pkg_sa_contrct.emp_id_new(i),
              pkg_sa_contrct.cust_cd_new(i),
              pkg_sa_contrct.brand_cd_new(i),
              pkg_sa_contrct.adv_cd_new(i),
              pkg_sa_contrct.contrct_gbn_new(i),
              pkg_sa_contrct.amount_new(i),
              pkg_sa_contrct.contrct_amt_new(i),
              to_char(sysdate,'yyyymmdd'),
              to_char(sysdate,'yyyymmdd'),
              pkg_sa_contrct.amount_new(i) - pkg_sa_contrct.contrct_amt_new(i)     
              );                                                           
     
  end if;


  if    updating        then 
      update sa_ledger set contrct_ilja = pkg_sa_contrct.contrct_ilja_new(i) ,
                            emp_id      = pkg_sa_contrct.emp_id_new(i)      ,                           
                            cust_cd      = pkg_sa_contrct.cust_cd_new(i)      ,
                            brand_cd    = pkg_sa_contrct.brand_cd_new(i)    ,
                            adv_cd      = pkg_sa_contrct.adv_cd_new(i)      ,
                            contrct_gbn  = pkg_sa_contrct.contrct_gbn_new(i)  ,
                            supply_amt  = pkg_sa_contrct.amount_new(i)      ,
                            receipt_amt  = receipt_amt -
                                          (pkg_sa_contrct.contrct_amt_old(i) - pkg_sa_contrct.contrct_amt_new(i)),
                            modi_ilja    = to_char(sysdate,'yyyymmdd')        ,
                            misu        = misu - (pkg_sa_contrct.amount_new(i) - pkg_sa_contrct.contrct_amt_old(i))
      where  contrct_ilja    = pkg_sa_contrct.contrct_ilja_old(i)
and    emp_id          = pkg_sa_contrct.emp_id_old(i)     
and    cust_cd        = pkg_sa_contrct.cust_cd_old(i)   
and    brand_cd        = pkg_sa_contrct.brand_cd_old(i)   
and    adv_cd          = pkg_sa_contrct.adv_cd_old(i)     
and    contrct_gbn    = pkg_sa_contrct.contrct_gbn_old(i);


  end if;   

 end loop;
 pkg_sa_contrct.sa_contrct_index := 0;
end;
/



create or replace package pkg_sa_contrct as

      type contrct_ilja is table of sa_contrct.contrct_ilja%type
      index by binary_integer;

      type emp_id is table of sa_contrct.emp_id%type     
      index by binary_integer;

      type cust_cd is table of sa_contrct.cust_cd%type
      index by binary_integer;
     
      type brand_cd is table of sa_contrct.brand_cd%type
      index by binary_integer;

      type adv_cd is table of sa_contrct.adv_cd%type
      index by binary_integer;

      type contrct_gbn is table of sa_contrct.contrct_gbn%type
      index by binary_integer;

      type amount is table of sa_contrct.amount%type
      index by binary_integer;

      type contrct_amt is table of sa_contrct.contrct_amt%type
      index by binary_integer;
 
      type ipgum_cd is table of sa_contrct.ipgum_cd%type
      index by binary_integer;

      type card_gbn is table of sa_contrct.card_gbn%type
      index by binary_integer;

      type nurak_amt is table of sa_contrct.nurak_amt%type
      index by binary_integer;
     
      sa_contrct_index  binary_integer;

      contrct_ilja_old  contrct_ilja;
      emp_id_old        emp_id;
      cust_cd_old      cust_cd;   
      adv_cd_old        adv_cd;
      brand_cd_old      brand_cd;
      contrct_gbn_old  contrct_gbn;
      amount_old        amount;
      contrct_amt_old  contrct_amt;
      ipgum_cd_old      ipgum_cd;
      card_gbn_old      card_gbn;
      nurak_amt_old    nurak_amt;

      contrct_ilja_new  contrct_ilja;
      emp_id_new        emp_id;
      cust_cd_new      cust_cd;   
      adv_cd_new        adv_cd;
      brand_cd_new      brand_cd;
      contrct_gbn_new  contrct_gbn;
      amount_new        amount;
      contrct_amt_new  contrct_amt;
      ipgum_cd_new      ipgum_cd;
      card_gbn_new      card_gbn;
      nurak_amt_new    nurak_amt;

end pkg_sa_contrct;
/



create or replace trigger tr_aft_sa_contrct
after insert or update on sa_contrct
for each row
begin
  pkg_sa_contrct.sa_contrct_index := pkg_sa_contrct.sa_contrct_index + 1;
  pkg_sa_contrct.contrct_ilja_old(pkg_sa_contrct.sa_contrct_index) := :old.contrct_ilja;
  pkg_sa_contrct.contrct_ilja_new(pkg_sa_contrct.sa_contrct_index) := :new.contrct_ilja;
  pkg_sa_contrct.emp_id_old(pkg_sa_contrct.sa_contrct_index) := :old.emp_id;
  pkg_sa_contrct.emp_id_new(pkg_sa_contrct.sa_contrct_index) := :new.emp_id;
  pkg_sa_contrct.cust_cd_old(pkg_sa_contrct.sa_contrct_index) := :old.cust_cd;
  pkg_sa_contrct.cust_cd_new(pkg_sa_contrct.sa_contrct_index) := :new.cust_cd;
  pkg_sa_contrct.adv_cd_old(pkg_sa_contrct.sa_contrct_index) := :old.adv_cd;
  pkg_sa_contrct.adv_cd_new(pkg_sa_contrct.sa_contrct_index) := :new.adv_cd;
  pkg_sa_contrct.brand_cd_old(pkg_sa_contrct.sa_contrct_index) := :old.brand_cd;
  pkg_sa_contrct.brand_cd_new(pkg_sa_contrct.sa_contrct_index) := :new.brand_cd;
  pkg_sa_contrct.contrct_gbn_old(pkg_sa_contrct.sa_contrct_index) := :old.contrct_gbn;
  pkg_sa_contrct.contrct_gbn_new(pkg_sa_contrct.sa_contrct_index) := :new.contrct_gbn;

  pkg_sa_contrct.amount_old(pkg_sa_contrct.sa_contrct_index) := :old.amount;
  pkg_sa_contrct.amount_new(pkg_sa_contrct.sa_contrct_index) := :new.amount;
  pkg_sa_contrct.contrct_amt_old(pkg_sa_contrct.sa_contrct_index) := :old.contrct_amt;
  pkg_sa_contrct.contrct_amt_new(pkg_sa_contrct.sa_contrct_index) := :new.contrct_amt;

  pkg_sa_contrct.ipgum_cd_old(pkg_sa_contrct.sa_contrct_index) := :old.ipgum_cd;
  pkg_sa_contrct.ipgum_cd_new(pkg_sa_contrct.sa_contrct_index) := :new.ipgum_cd;
  pkg_sa_contrct.card_gbn_old(pkg_sa_contrct.sa_contrct_index) := :old.card_gbn;
  pkg_sa_contrct.card_gbn_new(pkg_sa_contrct.sa_contrct_index) := :new.card_gbn;

  pkg_sa_contrct.nurak_amt_old(pkg_sa_contrct.sa_contrct_index) := :old.nurak_amt;
  pkg_sa_contrct.nurak_amt_new(pkg_sa_contrct.sa_contrct_index) := :new.nurak_amt;
 
end;
/



create or replace trigger tr_bef_sa_contrct
before insert or update on sa_contrct
begin
  pkg_sa_contrct.sa_contrct_index := 0;
end;


오라클자바커뮤니티 추천강좌-JAVA&WEB프레임워크실무과정
(8/31온라인 지원가능!! 홈페이지에서 지원신청 바랍니다.)

강좌명 JAVA&WEB프레임워크실무과정(주말주간(토/일))
교재 자체교재 무료제공
강좌 일정 09월01일(일) ~ 10월26일(토)((주말주간(토/일)) 10:00~18:00, 14일) 총 98시간
강의 장소 [B강의장]구로디지털단지역2번 출구-> 미니스톱끼고 우회전 -> 100m 직진 후 골목길 끝에서 이마트방향 우회전 -> 50m 직진 후 우체국 옆골목으로 길건너서 직진 -> 150미터 직진 후 JnK 타워에서 우회전 -> 50미터 직진 후 우측에 코오롱빌란트2차 803호 (구로구 구로3동 222-8 코오롱디지털타워 빌란트2차 803호)
[약도보기]
수강절차 - 강좌내용 확인
- 전화 또는 홈페이지(www.onjprogramming.co.kr)를 통한 수강지원 및 수강료 결제(무통장입금, 온라인 카드결제)
- 고용보험 가입자(재직자)인 경우 고용보험환급 관련 서류 제출
- 수강전 : 커리큘럼 및 장소에 대해 다시 한번 공지
- 교육 전 설문 작성(간단한 개발 경력, 수강 목적, 강좌진행방식 등)
- 강좌 수강
- 수강후 : 교육 후 설문 작성
수강료 - 1,200,000원
[고용주환급]대기업:40만원 전후,중소기업:48만원 전후 환급
[개인수강지원(개인환급)]정규직960,000원 ,비정규직:전액환급

대기업(상시근로자 300인 이상 대기업)은 개인환급 불가합니다.

재직자 내일배움카드 : 정부지원금 80% 자기부담금 20%
(구 능력개발카드 명칭이 내일배움카드로 변경 / 연간 총한도 200만원



* 휴강 :법정공휴일 / 추석 연휴 9월17일 휴강
수강료
입금안내
- 온/오프라인 카드결제, 계좌이체(수강안내->입금안내 참조)
문의사항 02-851-4790 번으로 연락 부탁 드립니다.
교육개요 본과정은 프로그래밍 언어의 경험이 있는 분이지만 자바를 처음하시는 분들을 위해
현장에서 필요로 하는 기술들을 최적화된 커리큘럼 및 강사를 통해 배울 수 있도록 하는 과정 입니다.

자바의 기본적인 사항부터 JDBC 프로그래밍, 모든 개발의 근간이 되는 자바네트워크 프로그래밍(이거 안하시면 2~3년지나서 UI개발자의 틀을 벗어 날 수 없습니다), 자바웹의 기본이되는 JSP, 그리고 최근 가장널리 사용되는 Ajax, jQuery를 통해 화면 깜박임없이 웹페이지를 역동적으로 구성할 수 있도록 배우고 최근 가장 많이 사용되는 JAVA기반의 프레임워크인 Spring Framework, SQL Data Mapper인 MyBatis까지 배울 수 있는 과정으로 자바 웹 개발자로 가시고자 하는 분들을 위한 최적의 과정 입니다.

본과정을 통해 기초부터 하나씩 배우신다면 내공 있는 자바 개발자가 되실것을 확신합니다!
교육목표 - 자바 기본문법의 이해
- 자바 네트워크 프로그래밍에 대한 이해
- JDBC 개발에 대한 이해
- 자바 웹개발에 대한 이해
- JSP의 작동원리 및 기본문법의 이해
- Ajax 및 jQuery에 대한 이해
- WAS(Web Application Server) 및 Web Server에 대한 이해
- 자바기반 프레임워크에 대한 이해(Spring3.X)
- SQL Mapper(MyBatis)에 대한 이해
- 실무 자바기술의 전반적인 이해 및 활용
교육대상 - 자바 초보 개발자
- 신입개발자
- 다른 언어를 사용하다가 자바쪽으로 전향을 원하는 개발자
- 자바를 배우고자 하는 학생
선수학습 - 프로그래밍에 대한 이해
 

Java Fundamental 자바 언어 소개,기본 문법
Virtual Machine 소개/메모리 영역
클래스 패스(Class Path) 개요
Array 이론/실습
클래스와 객체(Class & Object)
Abstarct Data Type, 상속(Inheritance)과 다형성
추상클래스(Abstract Class)와 다형성
인터페이스(Interface)와 다형성
연관(Aggregation & Composition)
오버로딩(OverLoading)과 오버라이딩(Overriding)
this/super/constructor
Package 만들기 이론/실습
Java에서 예외 처리 요령
사용자 예외 처리 방법
스트림(Stream) 입출력 관련 클래스, InputStream/OutputStream,
FileInputStream/FileOutputStream
Reader/Writer등 입출력 관련 클래스
표준 입출력/FILE 처리, 객체 직렬화 이론/실습
Thread 개요
Java에서의 Process
Thread Joining/Interrupt
Java Network URL/HTTP URL, URLConnection, HttpURLConnection, URLEncoding, URLDecoding 클래스 개요
URL을 다루는 예제 실습(Get/Post)

Client Socket과 Server Socket의 개요
MultiThread EchoServer
Socket을 이용한 예제 구현

UDP Programming(Multicasting programming)
UDP/Datagram 개요
DatagramSocket, DatagramPacket 소개
UDP를 이용한 예제 구현
Multicast 소개
Multicast Client/Server 구현
Distributed Computing(java RMI)
Distributed Computing, Object 소개
Java RMI를 이용한 “Hello World” 제작
RMI 응용예제 실습
JDBC Programming JDBC Driver 소개
JDBC 연결방법
Connection, Statement, ResultSet, PreparedStatement
Oracle의 function, procedure 다루기
DBCP, DataSource, Connection Pool
JSP(Java Server Page) JSP 기본문법, 작동원리
JSP 내장 객체,Java Beans
JSP에서의 Session, Cookie 다루기
Custom Tag
MVC Model(Model2)의 이해 및 활용
Ajax/jQuery Ajax 개발환경 구축
왜 Ajax 인가?
Ajax의 기본 구성
XMLHttpRequest 객체
innerHTML의 사용
DOM(Document Object Model) 다루기
Ajax MVC
jQuery 소개, 개요
jQuery 응용 예제
Spring Framework3.2 J2EE Framework에 대한 흐름과 Spring Framework에 대한 이해
개발 환경 설정(Eclipse4.2, Tomcat7, Spring3.2 다운로드 및 설치)
Spring IoC
DL(Dependency LookUp) &DI(Dependency Injection)
DL. DI 예제를 통한 이해
Spring 설정 상세
Spring AOP 란 ?Code, Advice, JoinPoint, PointCut, Aspect, WeavingProxyFactoryBean
Annotation기반 AOP(AspectJ)
Spring JDBC
Spring Web MVC
Sprint Web Flow
Spring Controller
Spring MVC TEST Framework
Spring3.2 New Feature
MyBatis/Hibernate [MyBatis]
개요 및 소개
개발환경 설정 및 설치
Data Mapper란
sqlMapConfig 이해 및 환경설정
Spring, MyBatis 연동
SQL Map XML File 이해
SqlMapClient 이해
SQL의 실행(Insert/update/delete) 이해와 실습
고급 쿼리 사용방법의 이해와 실습
Spring MyBatis 응용예제 작성

[Hibernate]
Hibernate 소개
SessionFactory 설정
1:1, 1:다 매핑
Session Interface
Hibernate DML
Spring, Hibernate 예제 프로그램 작성
Mini Project Spring 게시판 작성 실습
-- 게시판 구현에 대해 철저하게 이해한다면 실무에서의 개발도 충분히 가능 합니다.
  

댓글 없음:

댓글 쓰기