테이블 쪼개기

 

표를 쪼개고 중복성을 제거한다

 

트레이드오프란 상황 속에서 장점만 즉 읽기도 쉽고 쓰기도 쉬운 그런 프로그램을 만드는 것이 목표

 

LEFT JOIN

 

데이터베이스에서 Left (outer) Join이 많이 사용된다

 

SELECT * FROM topic

 

SELECT * FROM topic LEFT JOIN author //topic테이블은 왼쪽에 두고 오른쪽에 author테이블 두겠다

 

SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.aid

//topic 테이블의 author_id와 author테이블의 aid식별자가 값이 같은데 그걸 참고하여 두개테이블을 하나로 만들어줘

 

이것이 LEFT JOIN이다

 

SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.aid LEFT JOIN profile ON author.profile_id = profile.pid;

 

이렇게 여러개를 붙일수도 있다

 

SELECT tid, topic.title, author_id, name, profile.title AS job_title FROM topic LEFT JOIN author ON topic.author_id = author.aid LEFT JOIN profile ON author.profile_id = profile.pid;

 

이것은 위 빨간글씨부분만 가져오세요 라는 뜻이다

 

profile.title은 중복되는 title이 있으므로 profile의 title을 가져오라는 뜻이고

 

AS는 profile의 title을 job_title로 별명을 준것으로 보면 된다

 

SELECT tid, topic.title, author_id, name, profile.title AS job_title FROM topic LEFT JOIN author ON topic.author_id = author.aid LEFT JOIN profile ON author.profile_id = profile.pid WHERE aid = 1;

 

aid 값이 1인 값들만 불러오게 된다

 

'DATABASE > Database' 카테고리의 다른 글

공부(6)  (0) 2021.08.31
공부(5)  (0) 2021.08.30
공부(4)  (0) 2021.08.30
공부(3)  (0) 2021.08.30
공부(1)  (0) 2021.08.29

+ Recent posts