728x90
반응형
테이블 유형
- 영구 테이블(permanent table) : create table 문으로 생성
- 파생 테이블(derived table) : 하위 쿼리에서 반환하고 메모리에 보관된 행
- 임시 테이블(temporary table) : 메모리에 저장된 휘발성 데이터
- 가상 테이블(virtual table) : create view 문으로 생성
- 각 테이블 유형은 쿼리의 from 절에 포함될 수 있다.
파생 테이블
- 서브쿼리(subquery)는 다른 쿼리에 포함된 쿼리다.
- 서브쿼리는 괄호로 묶여 있으며 select 문의 여러 부분에서 찾을 수 있다.
- from 절 내에서의 서브쿼리는 from 절에 명시된 다른 테이블과 상호작용할 수 있는 파생 테이블을 생성하는 역할을 한다.
select concat(customer.last_name, concat(', ', customer.first_name)) full_name
from
(select first_name, last_name, email
from customer
where first_name = 'JESSIE'
)customer;
임시 테이블
- 성이 J로 시작하는 배우를 임시로 저장하는 방법
create temporary table actors_j
(actor_id smallint(5),
first_name varchar(45),
last_name varchar(45),
);
insert into_actors_j;
select actor_id, first_name, last_name
from actor
where last_name like 'J%';
- 7개의 행은 메모적으로 저장되면 세션이 종료되면 사라진다.
가상 테이블(뷰)
- 뷰는 데이터 딕셔너리에 저장된 쿼리다.
- 테이블처럼 동작하지만 뷰에 저장된 데이터가 존재하지 않는다.
- 이 때문에 가상 테이블라고 부른다.
-- employee 테이블 쿼리해 4개의 열을 포함하는 뷰 정의
create view cust_vw AS
select customer_id, first_name, last_name, active from customer;
select first_name, last_name
from cust_vw
where active = 0;
테이블 연결
- 단순 from 절 정의와 두 번째로 다른 점은 from 절에 둘 이상의 테이블이 있으면 그 테이블을 연결하는데 필요한 조건도 포함해야 한다는 의무사항이다.
- 여러 테이블을 조인하는 ANSI 승인 방법이며 다양한 데이터베이스 서버에서 가장 이식성이 뛰어난 방법이기도 하다.
select customer.first_name, customer.last_name,
time(rental.rental_date) rental_time
from customer
INNER JOIN rental
ON customer.customer_id = rental.customer_id
where date(rental.rental_date) = '2005-06-14';
테이블 별칭 정의
- 단일 쿼리에서 여러 테이블 조인할 경우 select, where group by, have 및 order by 절에서 열을 참조할 때 참조 테이블을 식별할 방법이 필요하다.
- from 절 외부에서 테이블을 참조할 때는 다음과 같은 방법 사용 가능
- employee.emp_id와 같이 전체 테이블 이름을 사용
- 각 테이블의 별칭을 할당하고 쿼리 전체에서 해당 별칭을 사용
select c.first_name, c.last_name,
time(r.rental_date) as rental_time
from customer c
inner join rental r
on c.customer_id = r.customer_id
where date(r.rental_date) = '2005-06-14';
728x90
반응형
최근댓글