Luver Duck 2022. 8. 24. 22:38

조회 (select)

분석 함수

- 단일 행 함수와 집계 함수의 합성 형태

- 조회(select)에서 테이블의 행에 대해 특정 그룹별로 집계값을 산출할 때 사용

분석함수(매개변수) over(분석기준)

- 분석 함수

RANK   COUNT  
SUM   LAG  
AVG   LEAD  
MAX   CUM_DISK  
MIN   DENSE_RANK  
FIRST   PERCENT_RANK  
LAST   ROW_NUMBER  

 

분석기준 partition by 컬럼명

- 분석 함수로 계산할 행(컬럼)의 그룹을 지정

분석함수(매개변수) over(partition by 컬럼명)

ex) 상품 분류(product_type)별 가격 순위 출력

select product.*, rank() over(partition by product_type order by product_price desc) from product;

 

그룹

- 조회(select)에서 특정 컬럼별 집계 함수의 결과를 얻을 수 있다

select 컬럼명, 집계함수(매개변수) from 테이블명 group by 컬럼명;

 

ex) 상품 분류(product_type)별 갯수 출력

select product_type, count(type) from product group by product_type;

 

집합 연산

- 합집합 연산자 : union

- 교집합 연산자 : intersection

 

ex) 시험(exam) 테이블

 

(Q 1) 합집합(union) : 피카츄 또는 라이츄가 본 시험 출력 

select distinct subject from exam where student = '피카츄' 
union 
select distinct subject from exam where student = '라이츄';

 

(Q 2) 교집합(intersection) : 피카츄와 라이츄 둘 다 본 시험 

select distinct subject from exam where student = '피카츄' 
intersect 
select distinct subject from exam where student = '라이츄';

 

(Q 3) 차집합(minus) : 피카츄만 또는 라이츄만 본 시험

select distinct subject from exam where student = '피카츄' 
minus 
select distinct subject from exam where student = '라이츄';

 

수정(update)

- 기존에 등록한 데이터를 원하는 값으로 변경 (commit / rollback 필요)

- 전체를 변경하는 경우보다 원하는 항목만 변경하는 경우가 더 많다

update 테이블명 set 컬럼명 = 바꿀값 where 조건;

 

ex) 상품 테이블(product)에서 스크류바의 가격을 2000원으로 인상

update product set product_price = 2000 where product_name = '스크류바';

 

삭제(delete)

- 기존에 등록한 데이터를 삭제 (commit / rollback 필요)

- 대부분 Primary Key를 이용한 단일 삭제로 처리한다

delete 테이블명 where 기본키컬럼명 = 숫자;

 

ex) 상품 테이블(product)에서 제조년도(product_made)가 2020년 상반기인 상품 정보를 삭제

delete product where extract(year from product_made) = 2020 and extract(month from product_made) between 1 and 6;

 

뷰(view)

- 테이블과 흡사한 객체로 가상 테이블 (혹은 논리 테이블)이라 한다

- 실제 데이터를 저장하고있지는 않지만 DML(insert, update, delete, select)이 가능하다

 

뷰 생성 권한 부여 (관리자 계정)

grant create view to 사용자명;

 

뷰 생성

create [or replace] view 뷰이름 as select문;