WHERE절에서 사용하는 서브쿼리.WHERE절에서 사용한다는 것은 조건절의 일부로 사용된다는 뜻. 즉, 메인쿼리 테이블의 특정 컬럼 값과 비교한 값을 반환하는 용도로 사용
SELECT works_with.emp_id FROM works_with
WHERE works_with.total_sales > 30000;
↓
SELECT employee.first_name, employee.last_name
FROM employee
WHERE employee.emp_id IN(
SELECT works_with.emp_id FROM works_with
WHERE works_with.total_sales > 30000
);
SELECT client.client_name FROM client WHERE client.branch_id = (
SELECT branch.branch_id FROM branch WHERE branch.mgr_id = 102
);
*결과값이 여러개일경우 '=' 대신 'IN'을 쓰는것이 좋다.
02 ON DELETE
PK를 기반으로 FK를 설정했으면 테이블끼리 연결이 되어있는 상태이다. 이때 서로 연관이 있는 키를 가진 테이블끼리의 데이터를 지우게되면 어떻게 될까?
ON DELETE 의 기능이 바로 서로 연관이 있는 키들과 테이블끼리 수정 혹은 삭제시 ON DELETE로 제약을 걸어놓은 상태로 세팅을 해준다.
테이블을 생성하였던 쿼리를 보자
CREATE TABLE branch (
branch_id INT PRIMARY KEY,
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL
);
→ 여기 쿼리에서 현재 branch/employee 테이블들이 연관이 있는 관계이다. 삭제해보자
DELETE FROM employee WHERE emp_id = 102;
SELECT * FROM employee;
Composite Attribute - 두개이상의 속성으로 구성된것을 일컫음 (국가의 도시와 번지)
Multi-valued Attribute - 하나의 개체(Entity)가 여러값을 가질 수 있는 속성을 말함. (한사람의 여러가지 취미)
Derived Attribute - 다른 속성으로부터 값이 결정되는 속성 (주민등록번호와 그 사람의 나이)
Multiple Entities - 다이어그램에 여러개의 개체를 설정 할 수 있음
Relationship Attribute - 연관이 있는 속성을 표시 (시험 점수를 원하면 시험을 쳐야함)
※ Relationship Cardinality
ERD의 목적은 Entity 유형 정의하고 Entity 유형간의 관계를 표시하기 위함
Attributes를 통해 Entity 유형을 정의했다면 Relationship으로 유형간의 관계를 표시 해야함
기본적으로 일대일(1:1) / 일대다(1:N) / 다대다(N:N)의 관계가 있음
1:1 - 두 개 Entity Type의 개체들은 서로 일대일로 대응
1:N - 하나의 개체가 다른 Entity Type의 많은 개체들과 관련되지만, 그 역은 성립하지 않음
N:N - 하나의 개체가 다른 Entity Type의 많은 개체들과 관련되며, 역이 성립
* 테이블간의 관계를 지을때 약한 개체(Weak Entity)가 발생할 수 도 있다. 약한 개체란 자신의 Key Attribute가 없는 Entity Type. 예로 '학사 관리 시스템'에서 강의번호 101는 101-1, 101-2... 이런식으로 여러개의 분반이 있을 수 있음. 이때 분반이라는 Entity는 자신의 Key Attribute는 없고 '실제 반' 개체가 있어야 존재 가능하기때문에 약한 개체라 불림
05 ERD 만들어 보기
위에서 언급 했듯이 ERD는 요구사항을 그림으로 나타낸 것이다. 요구사항은 다음과 같다.
The company is organized into branches. Each branch has a unique number, a name, and a particular employee who manages it.
The company makes it’s money by selling to clients. Each client has a name and a unique number to identify it.
The foundation of the company is it’s employees. Each employee has a name, birthday, sex, salary and a unique number.
An employee can work for one branch at a time,
and each branch will be managed by one of the employees that work there. We’ll also want to keep track of when the current manager started as manager.
An employee can act as a supervisor for other employees at the branch, an employee may also act as the supervisor for employees at other branches. An employee can have at most one supervisor.
A branch may handle a number of clients, with each client having a name and a unique number to identify it. A single client may only be handled by one branch at a time.
Employees can work with clients controlled by their branch to sell them stuff. If nescessary multiple employees can work with the same client. We’ll want to keep track of how many dollars worth of stuff each employee sells to each client they work with.
Many branches will need to work with suppliers to buy inventory. For each supplier we’ll keep track of their name and the type of product they’re selling the branch. A single supplier may supply products to multiple branches.
06 ERD 기반으로 DB 세팅
1. 가장 먼저 몇개의 테이블이 나오고 거기에 필요한 기초 컬럼이 무엇인가를 봐야 한다.
2. 테이블 개수 및 컬럼이 정해지면 테이블처럼 세팅 한다
3. 테이블끼리의 관계를 생각해 본 후 FK를 정한다. 1:1 → 1:N → N:N순으로 한다.