본문 바로가기

오라클 86기

2024.12.16 13일차 수업 # CTAS 복제 복습create table hr.emp_copytablespace usersas select * from hr.employees;desc hr.emp_copy;# 소유자 관점(만든 사람) 테이블 정보 확인select * from user_tables where table_name = 'EMP_COPY'; # DBA 관점에서 테이블 정보 확인select * from dba_tables where owner = 'HR' and table_name = 'EMP_COPY'; #  소유자 관점 컬럼 정보 확인select * from user_tab_columns where table_name = 'EMP_COPY'; # DBA 관점에서 컬럼 정보 확인 (dba)select * from dba_t.. 더보기
2024.12.12 12일차 수업 ■ 테이블 복제 (CTAS 용어 꼭 알기!)   - 테이블 구조, 행(데이터), 제약 조건 중에 NOT NULL 복제된다. ex 1)drop table hr.emp purge;create table hr.empasselect * from hr.employees;select * from hr.emp;▶ hr.employees에 있는 사원정보를 emp 테이블에 이관하는 작업이다. ♣ group by절에는 서브쿼리를 쓰지 않는다. ex 2)drop table hr.emp purge;create table hr.empasselect employee_id, last_name||' '|| first_name name, salary*12 salfrom hr.employees;select * from hr.emp; e.. 더보기
2024.12.11 11일차 수업 # 객체 권한 부여 (sys, 객체 소유자가 grant 해줄 수 있다.)grant select on hr.employees to insa;grant select on hr.departments to insa;select * from dba_tab_privs where grantee = 'INSA'; ※ SYS와 SYSTEM 계정의 차이   - 큰 차이는 없지만, SYS는 Oracle 시스템을 유지, 관리, 생성하기 위한 모든 권한을 갖는 계정이고, SYSTEM은 생성된 DB를 운영, 관리하기 위한 관리자 계정 정도로 이해하면 된다. # insa SQLselect * from user_tab_privs;select * from hr.employees;select * from hr.departments;▶ .. 더보기
2024.12.10 10일차 수업 # 집합연산자의 활용    ex 1) union 사용select e.employee_id, d.department_namefrom hr.employees e, hr.departments d -> 키값이 일치되는 값을 뽑아내고 사원테이블을 뽑는다.where e.department_id = d.department_id(+)union select e.employee_id, d.department_namefrom hr.employees e, hr.departments dwhere e.department_id(+) = d.department_id; -> 키값이 일치되는 값을 뽑아내고 부서테이블 뽑는다.  ex 2) union -> union all + not exists (바꿀 때 같이 생각하기)   # 위.. 더보기
2024.12.09 9일차 수업 복습 )   1. GROUP BY 절에는 SELECT 절에 있는 컬럼 그대로 써야 오류가 안 나온다.  ex) 요일별로 사원 인원수를 알고 싶을 경우select to_char(hire_date, 'day'), count(*)from hr.employeesgroup by to_char(hire_date, 'day') order by 1;▶order by 1을 하게 되면 첫 번째 컬럼을 오름차순 하게 되는데 출력 결과 보다시피 한글이라 ㄱㄴㄷ 순으로 출력된 걸 볼 수 있다. ex) 위에 거를 가로로 만들고 싶을 때 (pivot 함수 사용)select *from(select to_char(hire_date, 'dy') dayfrom hr.employees)pivot(count(*) for day in ('월'.. 더보기
2024.12.06 8일차 수업 문제) 부서별 최고 급여를 받는 사원들의 정보를 출력해 주세요.   1. correlate subquery 이용      내 답)select *from hr.employees ewhere salary in (select max(salary) from hr.employees where department_id = e.department_id)order by e.department_id;      교수님 답)select *from hr.employees ewhere salary = (select max(salary) from hr.employees where department_id = e.depar.. 더보기
2024.12.05 7일차 수업 문제 1) 20년 이상 근무한 사원들의 employee_id, salary, grade_level, 근무 연수, department_name, city를 출력하시오.      1. Oracle 방법select e.employee_id, e.salary, trunc(months_between(sysdate, hire_date)/12) 근무연수, j.grade_level, d.department_name, l.cityfrom hr.employees e, hr.job_grades j, hr.departments d, hr.locations lwhere months_between(sysdate, e.hire_date)/12 >= 20and e.salary between j.lowest_sal and j.high.. 더보기
2024.12.04 6일차 수업 문제 1) 80 부서에 근무하는 사원들의 last_name, job_id, department_name, city를 출력하시오.select e.last_name, e.job_id, d.department_name, l.cityfrom hr.employees e, hr.departments d, hr.locations lwhere e.department_id = d.department_idand d.location_id = l.location_idand e.department_id = 80;▶ 하지만 결과가 같다면 더 성능 좋게 셀프 튜닝으로 join 할 수도 있다. (카티션 곱 유발)▶ 문장 튜닝할 때 더 자세하게 배울 예정이다.select e.last_name, e.job_id, d.department.. 더보기