Notice
Recent Posts
Recent Comments
Link
«   2026/03   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

brograming

[Oracle]1 ~ 3장_Day14 본문

Kosta

[Oracle]1 ~ 3장_Day14

brograming 2023. 3. 6. 17:41

RDBMS

오라클, ms-sql, mysql ...

관계형 데이터 베이스 : 데이터를 표로 표현

하나의 테이블을 엔터티 > vo

세로줄 : 컬럼. 이름, 타입, 크기

가로줄 : 로우. 한 객체를 표현

 

SQL> conn /as sysdba; 
SQL> ALTER user hr account unlock; 
SQL> ALTER user hr IDENTIFIED BY hr;

SQL> conn hr/hr;

SQL> select table_name from tables;

SQL> select table_name from tabs;

 

 

--1.7
DESCRIBE employees;

--1.8
select employee_id, last_name, job_id, hire_date "STARTDATE"
from employees;

--1.9
select distinct job_id
from employees;

--1.10
select employee_id "Emp#", last_name "Employee", job_id "Job", hire_date "Hire Date"
from employees;

--1.11
select last_name || ', ' || job_id "Employee and Title"
from employees;

--1.12 ***
select *
from employees;

--2.1
select last_name, salary
from employees
where salary >=12000;

--2.2
select last_name, department_id
from employees
where employee_id = 176;

--2.3
select last_name, salary
from employees
where salary not between 5000 and 12000;

--2.4
select last_name, job_id, hire_date
from employees
where hire_date between '08/02/20' and '08/05/01'
order by hire_date;

--2.5
select last_name, department_id
from employees
where department_id in(20, 50)
order by last_name;

--2.6
select last_name "Employee", salary "Monthly Salary"
from employees
where salary between 5000 and 12000
and (department_id = 50 or department_id = 20);

--2.7
select last_name, hire_date
from employees
where hire_date like '04%';

--2.8
select last_name,job_id
from employees
where manager_id is null;

--2.9
select last_name, salary, commission_pct
from employees
where commission_pct is not null
order by commission_pct desc;

--2.10
select last_name
from employees
where last_name like '__a%';

--2.11
select last_name
from employees
where last_name like '%a%' 
and last_name like '%e%';

--2.12
select last_name, job_id, salary
from employees
where job_id in ('ST_CLERK', 'SA_REP') and salary not in (2500, 3500, 7000);

--2.13
select last_name "Employee", salary "Monthly Salary", commission_pct
from employees
where commission_pct = 0.2;


--3.1
select sysdate "Date"
from dual;

--3.2
select employee_id, last_name, salary, trunc(salary*0.15+salary) "New Salary"
from employees;

--3.3
select employee_id, last_name, salary, trunc(salary*0.15+salary) "New Salary", trunc(salary*0.15+salary - salary) "Incease"
from employees;

--3.5
select initcap(last_name)"Name",length(last_name)"Length"
from employees
where last_name like 'A%' or last_name like 'J%' or last_name like 'M%'
order by last_name;

INITCAP 첫번째 문자는 대문자 나머지는 소문자

--3.6
select last_name,round(months_between(sysdate,hire_date)) "MONTHS_WORKED"
from employees
order by MONTHS_WORKED;

--3.7
select last_name || ' earns' || to_char(salary,'$99,999.00') || ' monthly but wants' || to_char(salary*3, '$99,999.00')"Dream Salaries"
from employees;

--3.8
select last_name, lpad(salary,15,'$')"SALARY"
from employees;

--3.9
select last_name, hire_date, to_char(next_day(add_months(hire_date,6),'월요일'), 'fmday, dd "of" month, yyyy') "REVIEW"
from employees;

--3.10 *
select last_name, hire_date, to_char(hire_date, 'day') "DAY"
from employees
order by to_char(hire_date-1,'D');

▶ 날짜 포맷 중 'D'는 요일을 숫자로 변환 1~7의 값을 갖음.

     1 = sunday, 2 = monday ... 이므로 -1을 줘서 monday를 1로 만듬. sunday는 7이 됨.



--3.11
select last_name, nvl(to_char(commission_pct),'No Commission') "COMM"
from employees;
NVL (expr1, expr2) expr1이 null이 아니면 그대로 출력, null이면 expr2 출력

 


--3.12
select rpad(last_name, length(last_name)+trunc(salary/1000),'*')"EMPLOYEE_AND_THEIR_SALARIES"
from employees
order by salary desc;

TRUNC(45.923,2) 45.92, TRUNC(45.923) 45

     두번째 인수가 2면 값을 소수점 둘째 자리까지 남기고 버리며, 두번째 인수가 0이거나 없으면 값의 일의 자리까지 남기고 버린다.

 

 

                                         ↓ : 띄어쓰기 공백. 공백자리 까지 포함해서 salary/1000+1이 됨.

selelct last_name || rpad( '  ', salary/1000+1, '*' ) "EMPLOYEE_AND_THEIR_SALARIES"

from employees
order by salary desc;

RPAD (salary, 10, '*' ) 24000*****, LPAD(salary, 10, '*' ) *****24000


--3.13
select job_id, decode(job_id, 'AD_PRES', 'A',
                                               'ST_MAN', 'B',
                                               'IT_PROG', 'C',
                                               'SA_REP', 'D',
                                               'ST_CLERK', 'E',
                                                0) "G"
from employees;             

--3.14
select job_id,
        case job_id when 'AD_PRES' then 'A'
                            when 'ST_MAN' then 'B'
                             when 'IT_PROG' then 'C'
                            when 'SA_REP' then 'D'
                             when 'ST_CLERK' then 'E'
                             else '0' end "G"
 from employees;

'Kosta' 카테고리의 다른 글

[Oracle] 8강_Day16  (0) 2023.03.08
[Oracle] 4 ~ 6장_Day15  (0) 2023.03.07
[JAVA] 예외처리, 입출력 스트림_Day12  (0) 2023.03.02
[JAVA] 컬렉션(Map)_Day11  (0) 2023.02.28
[JAVA] 반복자_Day11  (0) 2023.02.28