[Oracle]00

2020. 9. 17. 17:04DB/Oracle

SQL

DB에 관련된
- DQL : Data Query Language  - 조회 명령 (따로 뺄 정도로 용량이 많다,문법적 구성요소가 많다)
- DML : Data Manipulation Language: 입력/수정/삭제/조회


객체와 관련된 

- DDL : Data Definition Language : 객체의 생성 / 수정 /삭제 - create(생성하다)
- DCL : Data Control Language : 객체의 권한을 부여하거나 뺏는 명령 - grant / revoke
- TCL : Transaction Control Language : 트랜잭션(?) 제어하는 명령

 


select table_name from user_tables;
-- describe : 묘사하다.
desc job;


-- select : 조회하다.
-- 선택한 부분만 조회
select EMP_ID,EMP_NAME,EMP_NO from employee; --quary 문, 문법

-- 전체 조회
select * from employee;

-- employee table 에서 '유재식'이라는 사람의 모든 정보를 조회해라.
-- 문자열 출력 은 더블 쿼테이션 이 아닌 싱글 쿼테이션을 사용 한다.
-- 한글자이든 여러 글자이던 싱글 쿼테이션 을 사용한다.
select * from employee where emp_name = '유재식';

-- employee table 에서 '유재식'이라는 사람의 컬럼값(선택한) 정보만 조회해라. 
-- 컬럼 값은 대,소문자 거르지 않는다.
-- where 이 들어가면 반복문 중 if가 들어갔다라고 생각하면 된다.
select EMP_ID,EMP_NAME,EMP_NO from employee where emp_name = '유재식';

-- 모든 직원의 이름과 급여만 출력해보세요.
select emp_name,salary from employee;

-- JAVA와 같이 연산자 사용 방법은 같다.
select * from employee where salary = '6000000';
select * from employee where salary < '6000000';
select * from employee where salary <= '6000000';
select * from employee where salary > '6000000';
select * from employee where salary >= '6000000';

-- && 대신 and 를 사용하고 &&의 기능과 같이 둘다 true 가 되어야 최종 true가 출력된다.
select * from employee where emp_id=203 and emp_name = '송은희';


select jop_name from employee;


-- 1. job table 에서 job_name 만 출력하세요.
select job_name from job;
-- 2. department 테이블의 내용 전체를 출력하세요.
desc department;
select * from department;

-- 3. Employee 테이블에서 고용일,이름 ,월급만 출력하세요.
desc employee;
select * from employee;
select emp_name,hire_date,salary from employee;

--5. employee table salary 2500000 이상 사람의 이름과 급여 등급(Sal_level)을 출력하세요.
select emp_name, salary from employee where salary >= 2500000;

--6. employee table salary 3500000 이상이면서 J3 직급 코드에 속한사람의 이름과 전화번호만 출력하세요.
select emp_name, phone from employee where salary >= 3500000 and job_code = 'J3';


-- 이렇게 연산을 할 수 있다.
select emp_name , salary *12 from employee;

-- 이렇게 출력하면 salary *12라는 컬럼명이 연봉 이라고 변경이 되며,
-- as 에 '원' 을 입력하고 "단위" 를 입력 하면 뒤에 컬럼값으로 단위 그 각 객체에 원으로 입력된다.
select emp_name "직원명" , salary *12 "연봉",'원' "단위" from employee;

-- || 문자열을 이어줄 수 있다.
select emp_name "직원명" , salary * 12 || '원' "연봉" from employee;

-- date 타입은 출력될때 연/월/일 로 저장되어 출력된다.
select emp_name "직원명" , salary * 12 || '원' "연봉",hire_date "날짜"from employee;

-- sysdate 는 현재 날짜가 찍혀나온다.
select emp_name "직원명" , salary * 12 || '원' "연봉",hire_date "입사날짜", sysdate "현재날짜" from employee;

-- 근속일수를 확인 할 수 있다.  / 현재 날짜 - 입사 날짜
-- floor 은 버림 함수라고 생각하면 된다 (소수점을 버리는 기능하는 함수이다.)
select emp_name "직원명" , salary * 12 || '원' "연봉", floor(sysdate - hire_date)||'일' "근속일수"from employee;

select sysdate from dual; -- 날짜를 한번 확인 하고 싶을때에 dual을 사용한다.
select * from dual;

--------------------------------------------------------------------------------------------------------------
-- 1. employee Table 에서 이름, 연봉, 총수령액 을 출력하세요.

select * from employee;

select emp_name "이름", salary *12 || '원' "연봉" ,  (salary *12) +(salary* bonus)||'원' "총수령액"from employee;

select emp_name "이름", salary *12 || '원' "연봉" ,  bonus || '%' "보너스" ,(salary *12) +(salary* nvl(bonus,0))||'원' "총수령액",((salary *12) +(salary* nvl(bonus,0)))* 0.97 ||'원'"세후금액" from employee; 

-- 2. 직원이름과 근무일 수를 출력하세요.
select emp_name||'님'"직원명",floor(sysdate - hire_date) ||'일'"근속일수" from employee;

-- 3. employee table에서 20년 이상의 근속자 이름 ,월급 , 보너스를 출력하세요.
-- 7300 일
select emp_name||'님' "성함", salary ||'원' "월급" , bonus ||'%' "보너스" from employee
where(sysdate - hire_date)/365 >= 20;

-- distinct 중복 방지 //대상을 하나만 넣어야 한다.
select distinct job_code, emp_name from employee;  -- X
select distinct job_code from employee; -- O
-- 급여가 300만원 이상 400만원 이하인 직원의 이름 부서 급여를 출럭하세요. 

-- Exam 01
select emp_name, dept_code, salary from employee where salary >= 3000000 and salary <= 4000000;
-- 급여가 300만원 이상 400만원 이하인 직원의 이름 부서 급여를 출럭하세요.

-- Exam 02
select emp_name, dept_code, salary from employee where salary between 3000000 and 4000000;

-- between - and 사용법 // between 어디부터 and 어디까지 // 어디라는 부분에는 수치값을 받아 들일 수 있다.
select emp_name, hire_date from employee where hire_date between '05/01/01' and '10/12/31';