본문 바로가기
프로그래밍 개발/DB

Mysql - 테이블 분리하기와 JOIN

by Jinseok Kim 2021. 1. 9.
반응형

 

 

 

테이블 분리하기

 

 

테이블 분리하기를 구현하기 위하여 아래와 같이 테이블을 각자 만들어주었다.

mysql> DESC topic;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| id          | int         | NO   | PRI | NULL    | auto_increment |
| title       | varchar(30) | NO   |     | NULL    |                |
| description | text        | YES  |     | NULL    |                |
| created     | datetime    | NO   |     | NULL    |                |
| author_id   | int         | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> DESC author;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int          | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20)  | NO   |     | NULL    |                |
| profile | varchar(200) | YES  |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> DESC topic_backup;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int          | NO   | PRI | NULL    | auto_increment |
| title       | varchar(100) | NO   |     | NULL    |                |
| description | text         | YES  |     | NULL    |                |
| created     | datetime     | NO   |     | NULL    |                |
| author      | varchar(15)  | YES  |     | NULL    |                |
| profile     | varchar(200) | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

 

 

 

 

아래와 같은 방식으로 테이블을 분리하여 저자가 id 1 값인 형식을 만들어 주었다.

mysql> INSERT INTO `author` VALUES (1,'egoing','developer');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM author;
+----+--------+-----------+
| id | name   | profile   |
+----+--------+-----------+
|  1 | egoing | developer |
+----+--------+-----------+
1 row in set (0.00 sec)

 

mysql>  INSERT INTO `topic` VALUES (1,'MySQL','MySQL is...','2018-01-01 12:10:11',1);

mysql> SELECT *FROM topic;
+----+-------+-------------+---------------------+-----------+
| id | title | description | created             | author_id |
+----+-------+-------------+---------------------+-----------+
|  1 | MySQL | MySQL is... | 2018-01-01 12:10:11 |         1 |
+----+-------+-------------+---------------------+-----------+
1 row in set (0.00 sec)

 

 

 

 

 

 

계속 반복적으로 위의 예시처럼 만들면 아래와 같이 테이블을 분리하는 것이 가능하다.

INSERT INTO `author` VALUES (2,'duru','database administrator');
INSERT INTO `author` VALUES (3,'taeho','data scientist, developer');


INSERT INTO `topic` VALUES (2,'Oracle','Oracle is ...','2018-01-03 13:01:10',1);
INSERT INTO `topic` VALUES (3,'SQL Server','SQL Server is ...','2018-01-20 11:01:10',2);
INSERT INTO `topic` VALUES (4,'PostgreSQL','PostgreSQL is ...','2018-01-23 01:03:03',3);
INSERT INTO `topic` VALUES (5,'MongoDB','MongoDB is ...','2018-01-30 12:31:03',1);

//위의 명령어들을 하나하나 입력하면 테이블이 분리된다.

mysql> SELECT *FROM topic;
+----+------------+-------------------+---------------------+-----------+
| id | title      | description       | created             | author_id |
+----+------------+-------------------+---------------------+-----------+
|  1 | MySQL      | MySQL is...       | 2018-01-01 12:10:11 |         1 |
|  2 | Oracle     | Oracle is ...     | 2018-01-03 13:01:10 |         1 |
|  3 | SQL Server | SQL Server is ... | 2018-01-20 11:01:10 |         2 |
|  4 | PostgreSQL | PostgreSQL is ... | 2018-01-23 01:03:03 |         3 |
|  5 | MongoDB    | MongoDB is ...    | 2018-01-30 12:31:03 |         1 |
+----+------------+-------------------+---------------------+-----------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM author;
+----+--------+---------------------------+
| id | name   | profile                   |
+----+--------+---------------------------+
|  1 | egoing | developer                 |
|  2 | duru   | database administrator    |
|  3 | taeho  | data scientist, developer |
+----+--------+---------------------------+
3 rows in set (0.00 sec)

 

  • 이와같이 topic 테이블을 보면 author_id값이 추가되었는데 이것이 바로 author 테이블이 저자인 데이터를 가여져왔다고 볼 수 있다.
  • 이러한 분리를 통해 실제 실무에서 유지보수하는데 아주 유용하다. 만약 topic 테이블이 수천개 이상의 데이터였으며 수백개의 중복된 데이터가 있었다면 author 테이블의 데이터만 고쳐주면 한꺼번에 수정할 수 있는 편리함을 제공받을 수 있다.

 

 

 

JOIN

 

 

이제 테이블을 분리했으면 실제 사용자가 데이터를 저자 아이디를 통해 불러올텐데 이 원리를 JOIN이라고 말할 수 있다.

 

 

 

아래와 같이 분리한 테이블들이 있다.

mysql> SELECT *FROM topic;
+----+------------+-------------------+---------------------+-----------+
| id | title      | description       | created             | author_id |
+----+------------+-------------------+---------------------+-----------+
|  1 | MySQL      | MySQL is...       | 2018-01-01 12:10:11 |         1 |
|  2 | Oracle     | Oracle is ...     | 2018-01-03 13:01:10 |         1 |
|  3 | SQL Server | SQL Server is ... | 2018-01-20 11:01:10 |         2 |
|  4 | PostgreSQL | PostgreSQL is ... | 2018-01-23 01:03:03 |         3 |
|  5 | MongoDB    | MongoDB is ...    | 2018-01-30 12:31:03 |         1 |
+----+------------+-------------------+---------------------+-----------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM author;
+----+--------+---------------------------+
| id | name   | profile                   |
+----+--------+---------------------------+
|  1 | egoing | developer                 |
|  2 | duru   | database administrator    |
|  3 | taeho  | data scientist, developer |
+----+--------+---------------------------+
3 rows in set (0.00 sec)

 

 

 

 

 

아래와 같이 데이터가 필요하면 JOIN해주면 두 테이블이 합치게되어 불러올 수 있게 되었다.

mysql> SELECT * FROM topic LEFT JOIN author ON topic.author_id=author.id;
+----+------------+-------------------+---------------------+-----------+------+--------+---------------------------+
| id | title      | description       | created             | author_id | id   | name   | profile                   |
+----+------------+-------------------+---------------------+-----------+------+--------+---------------------------+
|  1 | MySQL      | MySQL is...       | 2018-01-01 12:10:11 |         1 |    1 | egoing | developer                 |
|  2 | Oracle     | Oracle is ...     | 2018-01-03 13:01:10 |         1 |    1 | egoing | developer                 |
|  3 | SQL Server | SQL Server is ... | 2018-01-20 11:01:10 |         2 |    2 | duru   | database administrator    |
|  4 | PostgreSQL | PostgreSQL is ... | 2018-01-23 01:03:03 |         3 |    3 | taeho  | data scientist, developer |
|  5 | MongoDB    | MongoDB is ...    | 2018-01-30 12:31:03 |         1 |    1 | egoing | developer                 |
+----+------------+-------------------+---------------------+-----------+------+--------+---------------------------+
5 rows in set (0.00 sec)

 

 

 

 

 

 

또 테이블에 보여주고 싶은 부분만 보여지기 위하여 author_id, id 을 뺴고 데이터 상위 제목들을 지정하여 명령하는 것도 가능하다.

이때 주의할점은 id값의 출저를 정확하게 표시해야하는데 열에 id란 값이 2개 중복되므로 id->topic.id 로 열 구분을 해줘야함topic 테이블의 id이면 topic.id라고 입력해주어야 한다.

mysql> SELECT topic.id, title, description, created, name, profile FROM topic LEFT JOIN author ON topic.author_id = author.id;
+----+------------+-------------------+---------------------+--------+---------------------------+
| id | title      | description       | created             | name   | profile                   |
+----+------------+-------------------+---------------------+--------+---------------------------+
|  1 | MySQL      | MySQL is...       | 2018-01-01 12:10:11 | egoing | developer                 |
|  2 | Oracle     | Oracle is ...     | 2018-01-03 13:01:10 | egoing | developer                 |
|  3 | SQL Server | SQL Server is ... | 2018-01-20 11:01:10 | duru   | database administrator    |
|  4 | PostgreSQL | PostgreSQL is ... | 2018-01-23 01:03:03 | taeho  | data scientist, developer |
|  5 | MongoDB    | MongoDB is ...    | 2018-01-30 12:31:03 | egoing | developer                 |
+----+------------+-------------------+---------------------+--------+---------------------------+
5 rows in set (0.00 sec)

 

 

 

 

이렇게 topic.id AS topic_id, AS를 이용해 이름 변경하여 출력 가능하다.

SELECT topic.id AS topic_id, title, description, created, name, profile FROM topic LEFT JOIN author ON topic.author_id = author.id;
+----------+------------+-------------------+---------------------+--------+---------------------------+
| topic_id | title      | description       | created             | name   | profile                   |
+----------+------------+-------------------+---------------------+--------+---------------------------+
|        1 | MySQL      | MySQL is...       | 2018-01-01 12:10:11 | egoing | developer                 |
|        2 | Oracle     | Oracle is ...     | 2018-01-03 13:01:10 | egoing | developer                 |
|        3 | SQL Server | SQL Server is ... | 2018-01-20 11:01:10 | duru   | database administrator    |
|        4 | PostgreSQL | PostgreSQL is ... | 2018-01-23 01:03:03 | taeho  | data scientist, developer |
|        5 | MongoDB    | MongoDB is ...    | 2018-01-30 12:31:03 | egoing | developer                 |
+----------+------------+-------------------+---------------------+--------+---------------------------+
5 rows in set (0.00 sec)

 

 

 

 

 

즉, 테이블을 분리한다는 것은, 모든 테이블이 식별자 값만 행에 포함하고 있다면 JOIN을 통해 얼마든지 관계를 맺을 수 있다는 사실을 알 수 있다.

반응형

'프로그래밍 개발 > DB' 카테고리의 다른 글

MongoDB - 생성 및 제거하기  (0) 2021.01.15
MongoDB - 기본 설치 및 개념  (0) 2021.01.14
MySQL - CRUD  (0) 2021.01.02
MySQL - 테이블 생성  (0) 2021.01.02
MySQL - 기본  (0) 2021.01.02

댓글