반응형 [DBMS]34 [Oracle]집합연산 # 집합연산 - 서로다른 집합 사이의 합집합 교집합 차집합등을 구하는 과정 -연산을 위해서는 2개의 서로다른 결과 집합이 필요 -두집합의 컬럼이 같아한다 --합집합 ( union all) --중복을 따지지 않고 합집합 처리 ex) select subject from exam where student ='피카츄' union all select subject from exam where student ='파이리'; --합집합 (union) --중복을 제거하고 합집합 처리 select subject from exam where student ='피카츄' union select subject from exam where student ='파이리'; --교집합(Intersect) :파이리와 피카츄가 둘.. 2022. 5. 30. [Oracle]기본키 Primary key Primary key (기본키 , 고유키) =테이블 컬럼을 대표하는 역활 =테이블에 1개만 설정해야한다. ex) --ex:학생 정보를 저장(학년 , 반 , 번호 , 이름) --1.학번이라는 고유 항목을 만들어서 기본키로 관리 --2 학년+ 반 + 번호를 조합해서 기본키로 관리 (복합키 =composite key) drop table student; create table student( grade number(1), class number(1), no number(2), name varchar2(21), primary key(grade, class , no) ); insert into student(grade ,class , no , name) values ( 1,1,1,'피카츄'); insert i.. 2022. 5. 30. [Oracle]그룹쿼리 Gruop by select * from product; select distinct type from product;--타입을 중복제거해서 출력(추가계산이 불가능) select type from product group by type;--타입별로 묶어서 타입명을 출력(추가계산이 가능) select type, count(*) from product group by type;--타입별 개수 select type, sum(price) from product group by type;--타입별 합계 select type, count(*) from product group by type order by count(*) desc;--타입별 개수를 많은 순서로 정렬 select type, count(*) "개수" from prod.. 2022. 5. 30. [Oracle]TopN Queey 탑N쿼리 Top N query -데이터를 원하느 개수만큼 끊어서 조회하는 구문 작성 방식 사례 : Top3 , top 100 ,페이지네이션(pagenation) -rownum 이 필요 :결과집합의 데이터에 순서대로 부여되는 행번호(무조건 1부터 시작) --product 테이블에 rownum을 추가하여 조회 --문제점 : 항목이 먼저 해석되기 떄문에 rownum 이 부여된 상태로 정렬이 되어 의미가 없어진다. select rownum ,product.* from product; select rownum ,product.* from product order by price asc; --1차 해결 : 서브쿼리를 이용하여 선 정렬 수행후 rownum부여 select rownum ,TMP.* from( sele.. 2022. 5. 30. 이전 1 ··· 3 4 5 6 7 8 9 다음 반응형