중요
번역은 여러분이 참여할 수 있는 커뮤니티 활동입니다. 이 페이지는 현재 60.00% 번역되었습니다.
15.6. 수업: 규칙
규칙은 들어오는 쿼리의 “쿼리 트리”를 재작성할 수 있게 해준다. 업데이트 가능한 뷰를 포함한 뷰를 구현하는 데 흔히 쓰인다. - 위키백과
이 수업의 목표: 데이터베이스에 대해 새 규칙을 생성하는 방법을 배우기.
15.6.1. 로그 작업 규칙 생성하기
Say you want to log every change of phone_no in your people table in to a
people_log table. So you set up a new table:
create table people_log (name text, time timestamp default NOW());
In the next step, create a rule that logs every change of a phone_no in the
people table into the people_log table:
create rule people_log as on update to people
where NEW.phone_no <> OLD.phone_no
do insert into people_log values (OLD.name);
이 규칙이 작동하는지 테스트하기 위해, 전화번호를 수정해봅시다:
update people set phone_no = '082 555 1234' where id = 2;
Check that the people table was updated correctly:
select * from people where id=2;
id | name | house_no | street_id | phone_no
----+------------+----------+-----------+--------------
2 | Joe Bloggs | 3 | 2 | 082 555 1234
(1 row)
Now, thanks to the rule we created, the people_log table will look like
this:
select * from people_log;
name | time
------------+----------------------------
Joe Bloggs | 2014-01-11 14:15:11.953141
(1 row)
참고
The value of the time field will depend on the current date
and time.
15.6.2. 결론
규칙을 사용하면 여러분의 데이터베이스에 데이터베이스의 다른 부분에서 일어난 변경 사항을 자동적으로 반영하는 데이터를 추가하거나 변경하도록 할 수 있습니다.
15.6.3. 다음은 무엇을 배우게 될까요?
The next module will introduce you to Spatial Database using PostgreSQL with PostGIS extension, which takes these database concepts and applies them to GIS data.