데이터 베이스 강의.

 

시작메뉴 - 서비스 프로그램

 

OracleDB19Home1TNSListener -> 원격 서비스

OracleServiceORCL -> 데이터베이스 서비스

이 둘은 중요하다. 시작시 백그라운스 서비스로 두 개는 '자동'으로 설정

 

팀장의 호스트. IP주소로 새로 만듦. (team_spring)

 

데이터 베이스 교제 p20부터 시작

DBMS의 역할이란?

 

나머지는 실습으로 공부!

혼자서 충분히 공부할 수 있다!

주석을 달아가며 공부하자.

 

## 테이블의 데이터 타입

# NUMBER(6,0) : 전체 자릿수는 6자리이고, 소수점은 0의 자리까지 허용한다. == 소수점을 표시하지 않음.

# NUMBER(8,2) : 전체 자릿수는 8자리이고, 소수점은 2의 자리까지 허용.

Variable : 변할 수 있는

# VARCHAR2 (20 BYTE) : 문자열의 길이가 변할 수 있는데, 최대 20(알파벳)문자까지

숫자, 알파벳, 특수기호 -> 1 byte

한글 -> 3byte

 

## NULLABLE

# NO : 해당 컬럼은 반드시 값이 들어가야 한다! 널이 되면 안된다.

# Yes : 해당 컬럼은 NULL이 들어가도 상관없다.

 

## Date : 날짜를 정할 수 있다.

 

 

4장

-------------------------------------
-- 04-3 SQL문의 뼈대, SELECT절과 FROM절
-------------------------------------
-- 사원 테이블의 전체 컬럼 가져오기
select * from employees;
-- 사원 테이블에서 사원번호, 봉급 가져오기
select employee_id, salary from employees;

-------------------------------------
-- 04-4 중복 데이터를 삭제하는 DISTINCT
-------------------------------------
-- 사원들이 근무하는 부서의 번호 가져오기(중복 제거해서)
select distinct department_id from employees;
--  
select distinct job_id, department_id from employees;
-------------------------------------
-- 04-5 한눈에 보기 좋게 별칭 설정하기
-------------------------------------
-- 사원의 월급과 연봉을 가져오기
select salary as 월급, salary*12 as 연봉 from employees;  -- as를 생략해도 실행은 되지만, 가급적 사용해주자.
-------------------------------------
-- 04-6 원하는 순서로 출력 데이터를 정렬하는 ORDER BY
-------------------------------------
-- 월급을 가장 많이 받아가는 사원 순(내림차순)으로 사원들을 정렬해서 가져오기
select employee_id, first_name, salary from employees
order by salary desc;
-- 사원들이 근무하는 부서의 번호를 중복을 제거하여 올림차순으로 가져오기
select distinct department_id from employees
order by department_id asc;

 

5장

-----------------------------------------
-- 05-1 필요한 데이터만 쏙! 출력하는 WHERE절
-----------------------------------------
-- 조건없이 사원의 전체 행 가져오기
select * from employees;

-- 부서 번호가 30인 사원들의 행을 가져오기
select * from employees
where department_id = 30;

-- 직무가 AD_VP인 사원들의 행을 가져오기
select * from employees
where job_id = 'AD_VP';

-- 2007.05.21 이 날에 입사한 사원의 행을 가져오기
 select * from employees
 where hire_date = '2007-05-21';  -- '2007 05 21'도 가능 '2007.05.21' 도 가능 '2007/05/21'도 가능하다.
 
-- 2005년도에 입사한 사원의 행을 가져오기 (방법 1)
select * from employees
where hire_date >= '2005-01-01' and hire_date <= '2005-12-31';

-- (방법 2) (연산자의 개수를 줄여 이것이 더 빠르다.)
select * from employees
where hire_date between '2005-01-01' and '2005-12-31';

-----------------------------------------
-- 05-2 여러개 조건식을 사용하는 AND, OR 연산자
-----------------------------------------
-- 월급이 15000 이하인 부서 30번과 50번 사원의 정보 가져오기 (방법 1)
select employee_id, salary, department_id from employees
where salary <= 15000 and (department_id = 30 or department_id = 50);
-- (방법 2)
select employee_id, salary, department_id from employees
where salary <= 15000 and department_id in(30,50);

-----------------------------------------
-- 05-3 연산자의 종류와 활용 방법 알아보기
-----------------------------------------
-- 부서 번호가 30이면서 직무가 ST_MAN이 아닌 사원의 행 가져오기
-- 방법 1
select employee_id, job_id, department_id from employees 
where department_id = 50 and job_id != 'ST_MAN';
-- 방법 2
select employee_id, job_id, department_id from employees
where department_id = 50 and job_id <> 'ST_MAN';  -- <>기호는 not과 같다.
-- 방법 3
select employee_id, job_id, department_id from employees
where department_id = 50 and not job_id = 'ST_MAN';  -- not 연산자 사용하기

-- 관리자(상사)가 없는(null) 사원의 행 가져오기
select employee_id, manager_id from employees
where manager_id is null;

-- 관리자(상사)가 있는 사원의 행 가져오기
select employee_id, manager_id from employees
where manager_id is not null;  -- not manager_id is null도 가능하다.

-- 직무 아이디가 'ST_MAN' 이거나, 'FI_MGR'인 사원의 행 가져오기
-- 방법 1
select employee_id, job_id from employees
where job_id = 'ST_MAN' or job_id = 'FI_MGR';

-- 방법 2
select employee_id, job_id from employees
where job_id in('ST_MAN', 'FI_MGR');

-- 봉급이 14000 이상 16000 이하인 사원의 행 가져오기
-- 방법 1
select * from employees 
where salary >= 14000 and salary <= 16000;

-- 방법 2
select * from employees 
where salary between 14000 and 16000;

-- 봉급이 14000 이상 16000 이하가 아닌 사원의 행 가져오기
-- 방법 1
select * from employees 
where not (salary >= 14000 and salary <= 16000);

-- 방법 2
select * from employees 
where not salary between 14000 and 16000;

-- 방법 3
select * from employees 
where salary not between 14000 and 16000;

-- first_name에서 첫글자가 'Sh'인 사원의 행 가져오기
select first_name from employees
where first_name like 'Sh%';    // like : ~와 같은, % : 어떠한 문자든지. (중요!!)

-- first_name에서 ell이 포함된 사원의 행 가져오기
select first_name from employees
where first_name like '%ell%';

-- first_name에서 ell이 포함안된 사원의 행 가져오기
-- 방법 1
select first_name from employees
where first_name not like '%ell%';
-- 방법 2
select first_name from employees
where not first_name like '%ell%';

 

6장

-----------------------------------------
-- 06-2 문자 데이터를 가공하는 문자 함수
-----------------------------------------
-- 문자 수 얻기
select first_name, length(first_name) from employees; -- length()는 함수 호출 -- 행마다 실행하는 함수: '단일 행 함수'라고 부른다.
-- 우리가 함수를 만들어서 사용하는 것보다 DB에서 제공해주는 함수를 많이 사용한다.
-- 도큐먼트에서 찾아 사용할 수 있다.
-- https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Functions.html#GUID-AC0E8A99-5097-4147-8295-C88EAC5AA362

-- 문자 잘라내기해서 가져오기
select last_name, substr(last_name, 1, 3), substr(last_name,2) from employees; -- 인덱스는 1부터 시작한다.

-- 특정 문자를 다른 문자로 바꾸기
select employee_id, first_name, job_id, replace(job_id, 'PROG', '개발')
from employees
where job_id = 'IT_PROG';

-- 좌/우 빈 공간 채우기 / reft padding, right padding **
select email, lpad(email, 10), rpad(email, 10),
        lpad(email, 10, '*'), rpad(email, 10, '-'),
        rpad(substr(email,1,5), 10, '*')
from employees;

-- 문자열 결합
-- 방법 1
select first_name || '-' || last_name as name
from employees;
-- 방법 2
select concat(first_name,'-') || last_name as name
from employees;
-- 방법 3
select concat(concat(first_name,'-'), last_name)as name
from employees;

--------------------------------------------------
-- 06-3 숫자 데이터를 연산하고 수치를 조정하는 숫자 함수
--------------------------------------------------
select commission_pct, 
        ceil(commission_pct), floor(commission_pct),  -- ceil과 floor은 바로 아래 정수..
        round(commission_pct), -- 소수 이하를 반올림
        round(commission_pct, 1) -- 소수 첫째자리까지 반올림 (둘째 자리에서 연산이 들어가는 것)
from employees
where commission_pct is not null;

-----------------------------------
-- 06-4 날짜 데이터를 다루는 날짜 함수
-----------------------------------
-- SYSDATE 함수 많이 씀. 알아두자!! (중요!!)
select sysdate
from dual;  -- sysdate는 '3+5'와 같이 테이블에서 가져와서 연산하는 것이 아니다. 따라서 dual이란 가상테이블을 붙여주어야 한다.
-- 예시
select 3+5
from dual;

select sysdate as today,
        sysdate-1 as yesterday,  -- sysdaate는 빼기가 가능하다. 기본적으로 day를 뺀다.
        sysdate+1 as tomorrow,
        add_months(sysdate, 3),  -- 3달을 더해준다. day까지 고려해준다.
        add_months(sysdate, -3)
from dual;

-- 날짜간 연산
-- 모든 사원의 근무일수를 가져오시오
select employee_id, hire_date, round(sysdate - hire_date) as ilsoo   -- 날짜 - 날짜는 일 수를 나타낸다.
from employees
order by ilsoo desc;

-- 모든 사원의 근무월수를 가져오시오
select employee_id, hire_date, round(months_between(sysdate, hire_date)) as monthnums -- months_between()함수는 월수를 계산해준다.
from employees
order by monthnums desc;

-- 돌아오는 월요일은?
select next_day(sysdate, '화')
from dual;

-- 이번달 말일은?
select last_day(sysdate)
from dual;

-----------------------------------
-- 06-5 자료형을 변환하는 형 변환 함수
-----------------------------------
-- to_number, to_date, to_char는 알아놓자.
select to_number('3.14') + 5,
        concat(to_char(5.15), ' asdf'),
        to_char(sysdate, 'YYYY"년" MM"월" DD"일"'),
        to_char(sysdate, 'YYYY / MM / DD'),
        to_char(sysdate, 'DL'),
        to_char(sysdate, 'DS'),
        to_date('2025.04.28'),
        to_date('2025/04/28')
from dual;

-- 올해 12월 25일까지 남아있는 일수는?
select round(to_date('2024/12/25')-sysdate) as chrismas_Dday
from dual;

----------------------
-- 06-6 NULL 처리 함수
----------------------
-- 사원의 월급과 연봉을 가져오기
select employee_id, salary, salary*12 + salary*commission_pct as ysal1,
         salary*12 + salary*null as ysal2,    -- null연산의 결과는 null이다.
         salary*12 + salary*nvl(commission_pct, 0) as ysal3     -- nvl(value1, value2)함수 : 만약 value1의 값이 null이라면 value2값으로 나타내준다. (중요!)
from employees;

여기까지가 교재 185페이지까지..

자주 쓰는 함수들을 익히자.

 

참고 사이트

: https://arainablog.tistory.com/415

 

내일은 1차 미니 프로젝트를 위하여 311페이지부터 나간다.

'JAVA' 카테고리의 다른 글

42일차 2024-04-25  (1) 2024.04.25
41일차 2024-04-24  (0) 2024.04.24
36일차 2024-04-17  (0) 2024.04.17
34일차 2024-4-15  (0) 2024.04.15
33일차 2024 - 4 - 12  (0) 2024.04.12

+ Recent posts