본문 바로가기

DBA

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.. 더보기
2024.12.03 5일차 수업 ■ group by   - 테이블의 행을 작은 그룹으로 나눌 수 있는 절 ex) 부서별 총액 급여를 구해주세요.select department, sum(salary)from hr.employeesgroup by department_id;▶ department_id(부서별)로 group by를 통해 부서별로 그룹을 지어준다.▶ F10을 누르면 실행계획에 HASH 알고리즘을 통해 group by가 실행됐다고 알 수 있다.▶ 9i R1 버전에서는 group by가 sort group by로 수행하기 때문에 정렬된 결과 집합으로 출력된다.▶ 9i R2 버전부터는 group by가 hash group by로 수행하기 때문에 정렬된 결과 집합으로 출력되지 않는다. (KEY-VALUE) ex) 위에 거를 정렬해서 만.. 더보기
2024.12.02 4일차 수업 1. 모든 DBMS 안에 날짜 형식의 데이터는 숫자형으로 들어가기 때문에 내부에 수치형 자료로 들어가 있다. (날짜 형식도 연산이 가능하다.) ex 1) select sysdate + 5, sysdate - 4from dual; ex 2)  오늘 시간을 문자형으로 바꿔서 초단위까지 보여주고 싶을 경우select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss.sssss')from dual; # hh만 썼을 땐 오전, 오후 구분이 안 간다. 하지만 뒤에 am이나 pm 둘 중 아무거나 써주면 현재 오전인지 오후인지 알려준다.select to_char(sysdate,'yyyy-mm-dd hh:mi:ss.sssss pm')from dual; # 현재 기원전인지 후인지 .. 더보기