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

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

congs 2023. 3. 30. 17:50

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 ) ;

-- 알리야스(a c)를 사용하지 않아도 실행은 가능
mysql> update course c set co_degree =
    -> (select count(a.at_num) from attend a
    -> where c.co_code = a.at_co_code ) ;

 

3. attend에 수강신청을 하면 course의 co_degree가 자동으로 증가하는 trigger 생성

-- trigger가 있다면 삭제
mysql> drop trigger if exists insert_attend; 

-- trigger 생성
mysql> delimiter //

mysql> create trigger insert_attend after insert on attend
    -> for each row
    -> begin
    -> update course set co_degree = co_degree + 1
    -> where co_code = new.at_co_code;
    -> end //
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;

-- trigger 확인
mysql> insert into attend (at_std_num, at_co_code)
    -> values ('2020160002', '20201ipc001');

 

결과

-- 100에 추가된 것을 확인
mysql> select * from attend;
+--------+------------+-------------+---------+---------+--------+----------+-----------+-------+---------------+----------+
| at_num | at_std_num | at_co_code  | at_year | at_term | at_mid | at_final | at_attend | at_hw | at_repetition | at_score |
+--------+------------+-------------+---------+---------+--------+----------+-----------+-------+---------------+----------+
|     81 | 2020160001 | 20201msc001 |    2023 |       1 |     38 |       35 |         8 |     9 | n             | A        |
|     82 | 2020160002 | 20201msc001 |    2023 |       1 |     30 |       40 |         8 |     5 | n             | B        |
|     83 | 2019160123 | 20202msc002 |    2023 |       1 |     32 |       20 |         8 |     8 | n             | D        |
|     84 | 2019456001 | 20202msc002 |    2023 |       1 |     35 |       22 |         8 |     9 | n             | C        |
|     85 | 2020123001 | 20201ipc001 |    2023 |       1 |     25 |       12 |         1 |     6 | y             | F        |
|     86 | 2020123020 | 20201ipc001 |    2023 |       1 |     40 |       40 |         8 |     3 | n             | A        |
|     87 | 2019456001 | 20202ipc002 |    2023 |       1 |     15 |       35 |         8 |     2 | n             | D        |
|     88 | 2019160123 | 20202ipc002 |    2023 |       1 |     22 |       33 |         5 |     5 | n             | D        |
|     89 | 2020160001 | 20202msc002 |    2023 |       2 |      0 |        0 |         0 |     0 | n             | NULL     |
|     90 | 2020160002 | 20202msc002 |    2023 |       2 |      0 |        0 |         0 |     0 | n             | NULL     |
|     91 | 2019160123 | 20201msc001 |    2023 |       2 |      0 |        0 |         0 |     0 | n             | NULL     |
|     92 | 2019456001 | 20201msc001 |    2023 |       2 |      0 |        0 |         0 |     0 | n             | NULL     |
|     93 | 2020123001 | 20201msc001 |    2023 |       2 |      0 |        0 |         0 |     0 | n             | NULL     |
|     94 | 2020123020 | 20201msc001 |    2023 |       2 |      0 |        0 |         0 |     0 | n             | NULL     |
|     95 | 2019456001 | 20202msc002 |    2023 |       2 |      0 |        0 |         0 |     0 | n             | NULL     |
|     96 | 2019160123 | 20202msc002 |    2023 |       2 |      0 |        0 |         0 |     0 | n             | NULL     |
|    100 | 2020160002 | 20201ipc001 |    NULL |    NULL |      0 |        0 |         0 |     0 | n             | NULL     |
+--------+------------+-------------+---------+---------+--------+----------+-----------+-------+---------------+----------+
17 rows in set (0.00 sec)

-- 컴퓨터 개론의 co_degree가 +1된 것을 확인
mysql> select * from coures;
ERROR 1146 (42S02): Table 'school.coures' doesn't exist
mysql> select * from course;
+-------------+----------------+--------------+----------+---------+-----------------------+-----------+-----------+
| co_code     | co_name        | co_professor | co_point | co_time | co_timetable          | co_pr_num | co_degree |
+-------------+----------------+--------------+----------+---------+-----------------------+-----------+-----------+
| 20201ipc001 | 컴퓨터개론     | 유관순       |        2 |       2 | 화1A,1B,2A,2B         |         0 |         3 |
| 20201msc001 | 대학수학기초   | 홍길동       |        3 |       3 | 월1A,1B,2A,수1A,1B,2A |         0 |         6 |
| 20202ipc002 | 기초전기       | 이순신       |        3 |       4 | 월1A,1B,2A,목1A,1B,2A |         0 |         2 |
| 20202msc002 | 프로그래밍일반 | 임꺽정       |        3 |       3 | 월1A,1B,2A목1A,1B,2A  |         0 |         6 |
+-------------+----------------+--------------+----------+---------+-----------------------+-----------+-----------+
4 rows in set (0.00 sec)