DB/명령프롬프트 - mysql 예시 24

[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] 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] 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] 뷰 view 생성 예제

1학기 수업을 듣는 학생들을 검색 출력: 학번, 이름 조건: 1학기 / 중복제거 추가 출력: 과목이름 중복제거를 하지 않는 경우 ! 조건에 맞는 학생이름 전체 출력, 조건에 맞는 번호 전체 출력 = 2번출력 -- 학번, 이름, 중복제거 mysql> create or replace view 1term_view as -> select distinct s.st_name, s.st_num -> from student as s, course as c -> where c.co_term = 1; Query OK, 0 rows affected (0.00 sec) mysql> select * from 1term_view; +---------+------------+ | st_name | st_num | +--------..

[MySQL] ERD 대학수강프로그램 - 4. 테이블 업데이트 (update, case, if, modify)

1. at_mid, at_final, at_attend, at_homework 값 업데이트 (40/40/10/10) update attend set at_homework = 8 where at_num=1; update attend set at_homework = 5 where at_num=2; update attend set at_homework = 2 where at_num=3; update attend set at_homework = 10 where at_num=4; update attend set at_homework = 7 where at_num=5; update attend set at_homework = 5 where at_num=6; update attend set at_homework = ..

[MySQL] ERD 대학수강프로그램 3. table이용 예시 (join)

( join으로 가져올 경우, from뒤에는 관계테이블 명 = 가운데 연결되어 있는 테이블 을 넣기! ) 1. 김영철이 수강하는 과목명을 출력 ( 중복과목이 있는 경우 (학기)도 함께 출력) mysql> select concat(su.su_title, '(' ,c.co_term, '학기)' ) as "김영철이 수강하는 과목명( 학기 )" -> from attend a -> join student s on at_st_num = s.st_num -> join course c on at_co_num = c.co_num -> join subject su on co_su_num = su.su_num -> where st_name = "김영철" -> order by co_term; +-----------------..