DB 63

[MySQL] 프로시저 생성 예제 (declare, join 사용)

student 테이블의 st_point(이수학점)를 업데이트 하세요 st_point = 이수학점 attend / student 테이블 사용 attend테이블의 at_repetition = 'y' 인 경우 학점을 받음 학점을 주는 조건 at_reprtition = 'n' 이면 학점을 얻을 수 있음 attend 테이블의 at_co_num가 어느과목인지 먼저 확인 subject 테이블에서 과목의 su_point 체크 후 sum(su_point) 하여 student 테이블의 st_point에 update call update_stpoint(학번) ; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3..

[MySQL] 프로시저 procedure 생성 예제

product_name을 넣으면 구매한 customer 이름이 검색되는 프로시져 product_name을 in_product_name에 저장하고, buy 테이블에서 in_product_name에 맞는 customer 칼럼 출력하는 프로시저 생성 in in_product_name varchar(45) 를 select_buy라는 프로시저의 속성에 저장 in은 생략이 가능 mysql> drop procedure if exists select_buy; mysql> delimiter // mysql> create procedure select_buy ( -> in in_product_name varchar(45) -> ) -> begin -> select customer from buy -> where produ..

[MySQL] 프로시저 procedure 생성 예제 (in)

product_name을 넣으면 구매한 customer 이름이 검색되는 프로시져 product_name을 in_product_name에 저장하고, buy 테이블에서 in_product_name에 맞는 customer 칼럼 출력하는 프로시저 생성 in in_product_name varchar(45) 를 select_buy라는 프로시저의 속성에 저장 in은 생략이 가능 mysql> drop procedure if exists select_buy; mysql> delimiter // mysql> create procedure select_buy ( -> in in_product_name varchar(45) -> ) -> begin -> select customer from buy -> where produ..

[MySQL] 프로시저 procedure (함수)

프로시저 procedure : 쿼리의 집합 일련의 쿼리를 마치 하나의 함수처럼 실행하기 위한 쿼리의 집합 메서드와 동일한 역할 어떠한 동작을 절차적 일괄처리 작업하는데 사용 장점 처리시간의 단축 : 한번에 SQL 구문 처리가 가능 유지 보수에 적합 프로시저 procedure 삭제 drop pricedure if exists 프로시저명 ; 프로시저 procedure 매개변수 in 외부에서 가져와 내부에서만 사용하는 매개변수 프로시저에 값을 전달, 프로시저 내부에서 값 수정 가능 in 매개변수는 복사본만을 사용 가장 많이 사용 out 내부에서 생성되어 외부로 리턴하는 매개변수 프로시저의 값을 호출자에게 다시 전달 ( = 리턴) 프로시저가 반환되는 경우, 새로운 값이 호출자에게 리턴 초기값 = 프로시저 내부에..

[MySQL] trigger 생성 예제 : update after, old, new

trigger 생성 문제 attend테이블의 co_code를 update하면 course테이블의 at_co_code 와 attend테이블의 co_code가 연결되어 있으니, co_code에 맞는 old.co_degree는 -1 / new.co_degree는 +1 되는 트리거 생성 1. update를 두번 사용하는 방법 mysql> delimiter // mysql> create trigger update_attend after update on attend -> for each row -> begin -> update course set co_degree = co_degree -1 -> where co_code = old.at_co_code; -> update course set co_degree = c..

[MySQL] table 생성, 데이터 입력 create, insert

table생성 및 데이터 삽입 create 삽입할 칼럼명과 칼럼속성을 나열 마지막에 기본키, 연결키를 지정 (추후 add로 지정가능) 단, auto_increment를 사용하는 경우 기본키 설정 필수! create table 테이블명 ( 칼럼명1 칼럼속성1, 칼럼명2 칼럼속성2, ... primary key(칼럼명) foreign key(연결칼럼명1) references 연결테이블명1(연결테이블의 연결키1) foreign key(연결칼럼명2) references 연결테이블명2(연결테이블의 연결키2) ... ) ; -- 생성 후 키 지정시 alter table attend add primary key(기본키) ; alter table attend add foreign key(연결키) references 타..

[MySQL] trigger 생성 예제 : after insert, new (함수사용X)

1. school 데이터 베이스 - course테이블의 해당 코스의 수강인원을 집계하는 필드 생성 co_degree필드 생성 mysql> alter table course add column co_degree int default 0; 2. co_degree 필드에 해당 코스를 듣는 인원수를 집계하여 업데이트 join을 사용하지 않아도 참고가 가능한 이유 update의 구문 = course 사용가능 select의 구문 = attend 사용가능 join을 이용하는 경우, 해당테이블을 두번 가져오는 것으로 오류 발생 update course c set co_degree = (select count(a.at_num) from attend where a.at_co_code = c.co_code ) ; -- 알리..

[MySQL] 트리거 trigger 예제 : before delete, old 사용

buy 테이블의 값을 삭제하면 product 테이블의 amount, sale_amount 값이 변경되는 트리거 생성 ⭐ 변수명 설정 유의 트리거나 프로시저(함수) 안에서 변수를 사용하는 경우, declare를 사용하여 선선 후 변수 사용(지역변수 개념) 트리거나 프로시저안에서의 변수는 전역변수(@변수명)을 사용할 수 없음 트리거나 프로시저(함수)안에는 일반적으로 _를 사용하여 일반 칼럼과 구분함 trigger 생성 mysql> delimiter // mysql> create trigger delete_buy before delete on buy -> for each row -> begin -> declare _amount int default 0; -> set _amount = old.amount; ->..

[MySQL] 트리거 trigger 예제 : after insert, new 사용

트리거 생성 : buy테이블의 값이 생성되면 product테이블의 재고량, 판매량이 변동되는 트리거 생성 : insert 재고량 : amout 판매량 : sale_amount 예) 홍길동이 에어나시를 3개 구매 => 재고량 -3, 판매량 +3 mysql> drop trigger if exists insert_buy; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> delimiter // mysql> create trigger insert_buy after insert on buy -> for each row -> begin -> declare _amount int default 0; -> set _amount = new.amount; -> update p..

[MySQL] 트리거 trigger 생성, 삭제

트리거 trigger : 연쇄반응(작용) 이벤트에 반응하여 자동으로 실행되는 구문 이벤트가 발생하는 경우 데이터의 무결성(일관성)을 지켜야 하는 경우 사용 예) 재고량과 판매량을 동일하게 업데이트 해야하는 경우 이벤트 Insert : old = null / new = 입력값 (after) Delete : old = 변경전 값 / new = null (before) Update : old = 변경전 값 / new = 변경후 값 (old=before, new=after) 트리거 trigger 특징 특정 테이블에 INSERT, DELETE, UPDATE 같은 "DML"문이 수행될 때, 데이터베이스에서 자동으로 실행 사용자호출 필요없음, 자동으로 실행됨 한번 생성이 되면 중복 생성X = 삭제 후 재생성 필요 트..