Database-Operator

 

Oracle 연산자

Between

--사원의 급여가 2000$이상이고 4000$이하인 모든 사원의 정보를 출력
select*
from emp
where sal >=2000 and sal <=4000;
--연산자 : 컬럼명 between A and B(=을 포함한다.) >>and를 연결
select*
from emp
where sal between 2000 and 4000; 
--사원의 급여가 2000$초과이고 4000$미만인 모든 사원의 정보를 출력
--을 할 때에는 =이 포함되지 않으므로 between을 사용하면 안된다.
select*
from emp
where sal >2000 and sal <4000 ; 

--부서 번호가 10 또는 20번 또는 30번인 사원의 사번, 이름, 급여, 부서번호를 출력하세요
select empno,ename,sal,deptno
from emp
where deptno = 10 or deptno = 20 or deptno = 30;

In

-- IN연산자 : in(조건,조건,조건)  >> 또는을 연결
select empno,ename,sal,deptno
from emp
where deptno in(10,20,30); --컬럼은 한개만 사용가능하다.

select empno,ename,sal,deptno
from emp
where deptno != 10 and  deptno !=20;

NOT IN

-- NOT IN 연산자 : not in(조건,조건,조건) >> and 부정값 , and 부정값 >>  in의 반대
select empno,ename,sal,deptno
from emp
where deptno not in(10,20);

Null

--POINT : 값이 없다(데이터 없다) >> null

create table member(
 userid varchar2(20) not null,
 name varchar2(20) not null,
 hobby varchar2(20) --생략하면 default로 null이 붙는다. null값을 허용한다. >> 필수 입력사항이 아니다.
);

select*from member;
insert into member(USERID,HOBBY) values('hong','농구');
--ORA-01400: cannot insert NULL into ("SYSTEM"."MEMBER"."NAME") >> name에 값을 안넣으면 안된다.
insert into member(USERID,name) values('hong','홍길동'); --hobby 값을 넣지 않으면 null값이다.
insert into member(USERID,name,hobby) values('hong','홍길동','야구');
commit;

--수당을 받지 않는 모든 사원의 정보를 출력하세요.
select*
from emp
where comm = null; --은 존재하지 않는다.

select*
from emp
where comm is null; -- null은 is로 받아야한다.

--수당을 받는 모든 사원의 정보를 출력하세요
select*
from emp
where comm is not null;  

--사원테이블에서 사번, 이름, 급여, 수당, 총급여를 출력하세요.
--총급여는(급여 + 수당)
select EMPNO,ENAME,SAL,COMM,sal+comm as "총급여"
from emp;

nvl

--POINT null
--null과의 모든 연산은 결과가 null이다.
--위의 문제를 해결하기 위해서 null을 처리할 수 있는 함수를 만든다.
--nvl() , nvl2() 

--ms-sql >> convert()
--my-sql >> IFNULL()
select EMPNO,ENAME,SAL,COMM,sal+nvl(comm,0) as "총급여"
from emp;

select 1000 + null from dual;

select 1000 + nvl(null,0) from dual;

select comm,nvl(comm,0) from emp;

--사원의 급여가 1000$이상이고 수당을 받지않는 사원의 사번, 이름, 직종, 급여, 수당을 출력하세요
select empno,ename,job,sal,comm
from emp
where sal >= 1000 and comm is null;

--DQL(data quary language) : SELECT
--DDL(data difinitial language) : CREATE, ALTER, DROP : 객체생성, 수정, 삭제
create table board(
 boardid number not null, --필수 입력
 title varchar2(20) not null, --영문자,특문, 공백 20자, 한글 10자
 content varchar2(2000) not null,
 hp varchar2(20)
 );
 
 desc board;
 --insert, update, delete 작업시에는 (rollback, commit)반드시 수행
 insert into board(boardid, title, content)
 values(100,'오라클','참쉽네');


select*from board;
--실반영 : commit
--취소 : rollback
commit;

 insert into board(boardid, title, content)
 values(200,'자바','그립다');

--잘못 되었는데 
rollback;

 insert into board(boardid, title, content,hp)
 values(200,'자바','그립다','010-3211-4456');

select boardid,nvl(hp,'EMPTY') as "hp" from board;
--nvl() 함수는 숫자, 날짜, 문자 모두에 적용가능하다.

Like

--문자열 검색
--주소검색  :  '역삼' 검색하면 역삼이라는 단어가 있는 문장이 다 나온다.
--문자열 패턴 검색(like 연산자)
--like 연산자 (와일드카드 문자 (% : 모든 것 , _ : 한문자))결합...

select*
from emp
where ename like '%A%'; --A가 들어간 것 다 찾아라

select*
from emp
where ename like 'A%'; --대소문자 구별,A로 시작하는 것 다 찾기

select*
from emp
where ename like '%S'; -- S로 끝나는 것 다 찾기

select*
from emp
where ename like '%LL%';

select*
from emp
where ename like '%A%A%'; -- A가 붙어있던 떨어져 있던 다 뽑는다. A가 2개

select ename
from emp
where ename like '_A%'; -- 두번째 시작하는 알파벳이 A이고 뒤에는 상관없다.

select ename
from emp
where ename like '___A%'; -- _는 찾고자 하는 단어에서 알파벳의 순서를 찾는 데 사용된다.

--오라클 과제(정규표현식 : ex) regexp_like))
select*from emp where REGEXP_LIKE(ename,'[A-C]');
-- 표준이다. (java, oracle, script 에서 사용 방법이 동일 하다.)

order by

--데이터 정렬하기

--order by 컬럼명 : 문자, 숫자, 날짜
--오름차순 : asc : 낮은 순 (default값)
--내림차순 : desc : 높은 순

select*from emp order by sal; -- 급여가 낮은 순으로 asc
select*from emp order by sal asc;
select*from emp order by sal desc; -- 급여 많이 받는 사람이 1등

select ename from emp order by ename asc; --문자열도 정렬 가능

--입사일이 가장 늦은 순으로 정렬해서 사번, 이름, 급여, 입사일 출력
select EMPNO,ENAME,SAL,HIREDATE
from emp
order by hiredate desc;

/*
실행 순서
select       3
from         1
where       2
order by    4   select한 결과를 가지고 정렬
*/
--입사일이 늦은 순으로 직업이 manager인 사람의 사번,이름,급여,직종,입사일 을 출력하세요
select empno,ename,sal,job,hiredate
from emp
where job = 'MANAGER'
order by hiredate desc;

--문제
select deptno,job
from emp
order by deptno desc , job asc; ---grouping distinct와 같다.

union , union all

--연산자
            --합집합(union) : 테이블과 테이블의 데이터를 합치는 것 (중복값은 배제)
            --합집합(union all) : 중복값 허용
            
            create table UTA(name varchar2(20));
            insert into uta(name) values('AAA');
            insert into uta(name) values('BBB');
            insert into uta(name) values('CCC');
            insert into uta(name) values('DDD');
            commit;
            select *from uta;
            
            create table UT(name varchar2(20));
            insert into ut(name) values('AAA');
            insert into ut(name) values('BBB');
            insert into ut(name) values('CCC');
            commit;
            select*from ut
            
            select*from ut
            union
            select*from uta; --중복 데이터 배제
            
            select*from ut
            union all
            select*from uta; --중복 데이터 허용
            
            --union 규칙
              --1. 대응되는 컬럼의 타입이 동일
              select empno, ename from emp
              union
              select dname,deptno from dept; --X
              
              select empno, ename from emp
              union
              select deptno,dname from dept; -- type을 맞추어야 합쳐진다.
              
                              --실무 > subquery 사용해서 union한 테[이블 가상 테이블 처럼 사용한다.
                              select empno , ename
                              from(
                                 select empno, ename from emp
                                 union
                                 select deptno,dname from dept 
                              ) order by empno desc;
              
              --2. 대응되는 컬럼의 개수가 맞아야 한다.(null로 방의 갯수를 맞추면 된다.)
              select empno,ename,job,sal from emp
              union
              select deptno,dname,loc from dept; --X 방의 갯수가 다르다.
            
              select empno,ename,job,sal from emp
              union
              select deptno,dname,loc,null from dept;