Oracle day 07

유요한·2024년 7월 1일
0

DataBase(MySQL & Oracle)

목록 보기
15/17

이미 있는 테이블과 동일한 테이블 만들기

create table newbook as select * from book

book 테이블의 레코드를 모두 조회하여 동일한 컬럼과 레코드를 갖는 테이블 생성

create table newbook as select * from book where 1=2;

절대로 만족하지 않는 조건을 주면 레코드는 추가하지 않고 구조만 복사할 수 있다.

update안에 서브쿼리

update customer 
set address = (select address from customer where name='김연아') 
where name = '박세리';

delete안에 서브쿼리

delete orders 
where custId = (select custId from customer where name = '박지성');

수정

  • update : 레코드 수정
  • alter : 테이블 구조 수정

삭제

  • delete : 레코드를 삭제
  • drop : 테이블 삭제

함수

숫자 함수

  • abs() : 절대값을 알려주는 함수
  • ceil() : 올림수를 취해 주는 함수
  • floor() : 버림수를 취해 주는 함수
  • round() : 반올림을 취해 주는 함수

글자 함수

  • lower() : 소문자로 바꿔줌
  • upper() : 대문자로 바꿔줌
  • trim() : 양쪽 공백을 제거
  • rtrim() : 오른쪽 공백을 제거
  • ltrim() : 왼쪽 공백을 제거
  • length() : 글자의 길이를 알려줌
  • lengthb() : 글자를 표현하는데 필요한 바이트 길이
  • replace() : 문자열을 변경 💡 replace(’데이터’, ‘원래글자’, ‘바꿀글자’)
  • substr() : 문자열의 일부분을 잘라주는 함수 💡 substr(’데이터’, 시작위치, 개수);
  • instr() : 해당 문자열이 있는 맨처음 나오는 위치를 알려줍니다.
  • lpad() : 왼쪽 공백을 특정문자로 채움 💡 lpad(문자열, 자리수, 채울문자)
  • rpad() : 오른쪽 공백을 특정문자로 채움

코드

select workerId, substr(jumin,1,8) jumin, substr(email,1, instr(email, '@') -1) id 
from worker where jumin is not null;

select lpad('hello', 10, '*') from dual;

select 
    w.workerId 사원번호, 
    w.name 사원이름,
    m.name 관리자명,
    lpad(round((w.salary + NVL(w.comm, 0) * 12 * 2.0), -1), 10, '0') 상여금,
    rpad(substr(w.jumin,1,8), 14, '*') 주민번호,
    substr(w.email, 1, instr(w.email, '@') -1) 아이디,
    w.hiredate 입사일
from worker w
join worker m on w.mgr = m.id
where substr(w.jumin,8,1) in (1,3) and w.hiredate < '2021/01/01';

날짜

sysdate

오늘 날짜와 시간을 알려줌

함수

  • to_date(문자, 형식) : 문자를 날짜로 변경
  • to_char(날짜, 형식) : 날짜를 문자로 변경

형식에 올 수 있는 것

  • yyyy : 연도 4자리
  • yy : 연도 2자리
  • mm : 월
  • dd : 일
  • hh : 시
  • mi : 분
  • ss : 초

    코드

    select to_char(sysdate, 'yy') from dual;
    select to_char(sysdate, 'yyyy/mm/dd/hh/mi/ss') from dual;
    
    // 나이가 27살 이상인 모든 직원의 정보
    select * from worker
    where workerId in (
        select workerId 
        from (
            select 
                workerId,
                to_char(sysdate, 'yyyy') 
                - case
                when substr(jumin, 8, 1) in ('1', '2') then  concat('19',substr(jumin, 1, 2))
                when substr(jumin, 8, 1) in ('3', '4') then  concat('20',substr(jumin, 1, 2))
                end
                as age
            from worker where jumin is not null
            )
        where  age >= 27
    );
profile
발전하기 위한 공부

0개의 댓글