본문 바로가기
코딩 공부/MySQL (서버)

MySQL 테이블 분리하기 (수정하기 쉽게 만들기)

by Camel_coding_food 2022. 2. 19.
반응형

그건 데이터베이스가 아니에요!

 

오늘은 두개의 테이블을 만들고,

두 데이블에 있는 같은 요소를 가진 데이터를 묶고,

수정해보도록 하겠습니다.

 

이번 글의 코드는 모두 SQL이 실행되어있는 cmd창에서 작성합니다.

 

 

따로 제가 테이블 내용을 만들지는 않을거고, 생활코딩의 이고잉님 코드를 사용하겠습니다.

 

 

일단 테이블 두개를 만듭시다.

INSERT INTO `topic` VALUES (1,'MySQL','MySQL is...','2018-01-01 12:10:11',1);
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);

첫번째 테이블입니다.

 

 

어떻게 만들어졌는지 구경해봅시다.

MariaDB [tutorials]> 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.001 sec)

 


 

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

 

두 번째 테이블입니다.

 

 

 

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

 

여기서 두 테이블의 데이터를 연동시킬 방법을 알아봅시다.

 

바로 작가들의 id 값입니다.

 

SQL에게 알아서 두 테이블의 같은 작가 id 값을 연결하라고 전달해보겠습니다.

 

 

MariaDB [tutorials]> SELECT *FROM topic LEFT JOIN author ON topic.author_id =author.id;

("마리아DB야 토픽 테이블의 작가_id 값과 작가 테이블의 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.003 sec)

그럼 이렇게 출력되죠.

 

저길 보시면 author_id 값과 id 값이 붙어있죠?

불필요한 것 같습니다.

 

 

 

원하는 칼럼만 출력해보겠습니다.

SELECT id,title,description,created,name,profile FROM topic LEFT JOIN author ON topic.author_id =author.id;

MariaDB [tutorials]> SELECT id,title,description,created,name,profile FROM topic LEFT JOIN author ON topic.author_id =author.id;
ERROR 1052 (23000): Column 'id' in field list is ambiguous
MariaDB [tutorials]>

에러가 발생하네요.

id 가 뭘 의미하는지 애매하다고 합니다.

 

 

토픽 테이블의 id만 남기라고 정해줍시다.

MariaDB [tutorials]> 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.002 sec)

잘 작동하네요.

 

 

이렇게 연결한 이후에는 테이블이 두개가 존재하더라도,

한 테이블의 정보를 바꿔주면 다른 테이블의 정보도 변경됩니다.

 

한눈에 읽기는 어려운 대신, 체계적으로 데이터를 분류할 수 있는거죠.

 

 

다음번에는 인터넷과 데이터베이스를 알아보겠습니다.

 

반응형

'코딩 공부 > MySQL (서버)' 카테고리의 다른 글

MySQL 시작하기.  (0) 2022.02.18

댓글