[PostgreSQL] Inherits 상속

Ja L·2023년 4월 18일
0

AgensSQL/PostgreSQL

목록 보기
9/34

ERD 로 모델링을 마치고 이를 바탕으로 테이블에 데이터를 적재하는 과정을 살펴본 적이 있다.
SQL문을 보고 살펴보던 중 몰랐던 내용이 있어 기록해둔다.

상황은 이렇다.

						Place
           ┌────────────────┴────────────────┐
           City			Country 	  Continent

place 가 parent,
city, country, continent 가 child가 된다.

cf) inherit 하는 방법

-- place table 생성
create table place(
id int,
name varchar,
url varchar, 
type varchar)

-- place 상속받아 city, country, continent 테이블 생성
-- city
create table city () inherits(place) ; 

                     Table "public.city"
 Column |       Type        | Collation | Nullable | Default
--------+-------------------+-----------+----------+---------
 id     | integer           |           |          |
 name   | character varying |           |          |
 url    | character varying |           |          |
 type   | character varying |           |          |
Inherits: place


-- country 
create table country () inherits(place) ; 
                   Table "public.country"
 Column |       Type        | Collation | Nullable | Default
--------+-------------------+-----------+----------+---------
 id     | integer           |           |          |
 name   | character varying |           |          |
 url    | character varying |           |          |
 type   | character varying |           |          |
Inherits: place

-- continent (continent_id 칼럼을 추가)
create table country (continent_id int) inherits(place) ; 

                     Table "public.continent"
    Column    |       Type        | Collation | Nullable | Default
--------------+-------------------+-----------+----------+---------
 id           | integer           |           |          |
 name         | character varying |           |          |
 url          | character varying |           |          |
 type         | character varying |           |          |
 continent_id | integer           |           |          |
Inherits: place

부모 테이블의 내용을 조회하면 자식 테이블의 내용까지 조회된다.

이를 확인하기 위해 부모 테이블인 place 테이블에는 데이터를 넣지 않고,
, country, continent 테이블에 insert를 하나씩 해보자.

insert into city values( 
1, 'lee', 'www.naver.com', 'city'
) ;

insert into country values(
2, 'kim', 'www.northkorea.com', 'city'
) ;

insert into continent values(
3, 'peggy', 'www.asia.com', 'continent', 4
) ;

값을 잘 넣었다.

이제 부모 테이블을 조회해보자.

select * from place ;

 id | name  |        url         |   type
----+-------+--------------------+-----------
  1 | lee   | www.naver.com      | city
  2 | kim   | www.northkorea.com | city
  3 | peggy | www.asia.com       | continent
(3 rows)

place 테이블에 값을 insert 하지 않았지만 자식 테이블의 값이 모두 조회되어 3개의 row를 보여준다.

온전히 place 테이블의 값만 보고싶다면 only 포함하여 질의하면된다.

select * from only place ;
 id | name | url | type
----+------+-----+------
(0 rows)

그리고 자식테이블은 당연하게도 조회가 잘 된다.

select * from continent ;

 id | name  |     url      |   type    | continent_id
----+-------+--------------+-----------+--------------
  3 | peggy | www.asia.com | continent |            4
profile
DB Engineer

0개의 댓글