DAY-21. Oracle 문자열 함수1
🔐 2022-03-30
데이터베이스
❗ 수업을 듣고, 개인이 공부한 내용을 적은 것 이기에 오류가 많을 수도 있음
2022-03-30 데이터베이스
1️⃣ 오라클 자료형
- 숫자
- number
- 문자
- char : (길이 값이 고정 / 최대 2000byte) 문자의 크기와 상관 없이 고정된 값으로 공간을 차지함 ex )char의 공간을 9byte로 하고 문자를 6byte 써도 공간이 남음
- varchar2 : (길이 값이 가변 / 최대 4000byte) 문자의 크기만큼 공간 차지함
- 날짜
- date : 년원일 나옴
- timestamp : 년원일 + 시간까지 나옴
💡char vs varchar
- NAME은 char로 MESSAGE는 varchar로 테이블 생성
2️⃣ select
조건
대소 비교
select * from 테이블명 where 대소비교 조건
select * from employee where salary >= 5000000;
AND 연산자
select * from 테이블 명 where 컬럼명 = A AND 컬럼명 = B;
select * from employee where dept_code = 'D5' and sal_level = 'S5';
OR 연산자 :
select * from 테이블 명 where 컬럼명 조건 OR 컬럼명 조건;
select * from employee where salary >= 3000000 or job_code = 'J6';
컬럼 산술 연산
컬럼에 직접적으로 수학적 공식을 활용하여 계산 가능
select salary * 12 from employee;
💡 select 문을 통해 조회되는 결과들을 resultSet이라고 한다
컬럼 별칭 설정
select 컬럼명 (as) “바꾸고 싶은 이름“ from 테이블명 ( as는 생략 가능 );
→ select emp_name as "직원명", salary*12 "연봉(원)" from employee;
임의의 문자열을 실제 데이터 값처럼 보이게끔 리터럴 추가 기능
select 컬럼명 || ‘컬럼에 붙일 이름‘ “바꾸고 싶은 이름“ from 테이블명;
→ select emp_name "직원명", salary*12 ||'원' "연봉" from employee;
중복제거
- 일반 → 중복된 값 도출
select job_code from employee;
- 중복제거 → distinct 사용
select distinct job_code from employee;
3️⃣ 연산자
- between A and B : 특정 범위에 포함되는지 A와 B사이
- like / not like : 문자 패턴 비교
- is null / is not null : null 값 비교
- in / not in : 특정한 값이 값 목록에 포함 여부
1. between
예제
1) 급여 350만원보다 많고 600만원보다 적은 사원의 이름 급여 조회
1. between 사용 x
select emp_name, salary from employee
where salary >= 3500000 and salary <= 6000000;
2. between 사용
select emp_name, salary from employee
where salary between 3500000 and 6000000;
2) employee 테이블에서 고용일이 90/01/01 ~ 01/01/01 사이인 직원의 전체 데이터를 조회
select * from employee
where hire_date between '90/01/01' and '01/01/01';
2. like
비교하려고 하는 값이 특정한 패턴을 만족시켰을 때 true 값을 반환하는 연산자
- ’%’ , ‘*’ : 와일드카드 (아무값이 대체해서 사용할 수 있는 것)
- % : 0글자이상 전xxx 같은때 사용 전~~~~~
- __ : 언더바 갯수에 맞춰서 전xx 이면 언더바 두개 전xxx 이면 언더바 세개 이런식
- 와일드카드 문자와 만약 문자열로써 사용하고픈 특수문자가 동일한 경우 -> 모두다 와일드카드 문자로 인식
- escape 옵션 사용 -> like ‘*#_’ escape ‘#’ : #뒤로 오는 문자는 와이들카드가 아닌 문자열로 인식
- #대신 다른 기호 써도 무관
예제
1) employee 테이블에서 직원명, 급여 조회 조건 : ‘전’씨 성을 가진 직원
select emp_name, salary from employee where emp_name like '전'; -- 이건 이름자체가 그냥 전 하나 인사람
select emp_name, salary from employee where emp_name like '전&'; -- 전씨만 뽑기
select emp_name, salary from employee where emp_name like '전__';
2) employee 테이블의 직원의 이메일 중에서 ‘*’ 앞자리가 3자리인 직원명과 이메일을 조회
select emp_name, email from employee where email like '___#__%' escape '#';
#은 아무 문자가 온다 이런뜻 ___ 비엇고 # 아무 문자오고 _ 다시비고 % 뒤에 아무거나 오게
#대신 다른 기호 써도 무관
3. not like
예제
1) ‘이’씨 성이 아닌 직원의 사번 ,이름, 이메일 조회
select emp_id, emp_name, email from employee where emp_name not like '이%';
2) employee에서 전화번호 처음 번호가 3자리가 010이 아닌 사원의 이름, 전화번호 조회
select emp_name, phone from employee where phone not like '010%';
3) employee에서 메일주소에 ‘s’가 들어가고, dept_code는 D9 혹은 D6이고,
고용일이 90/01/01 ~01/12/01,이면서 월급이 270만원 이상인 직원의 모든 정보 조회
select * from employee
where email like '%s%'
and (dept_code ='D9' or dept_code ='D6')
and (hire_date between '90/01/01' and '01/12/01' )
and (salary >= 2700000);
💡 괄호가 없으면 이메일에 s가 들어가고 d9 인사람 or d6인 사람으로 조건이 바뀌어버림
4. is null / is not null
예제
select * from employee where manager_id is null;
select * from employee where manager_id is not null;
5. in / not in
예제
1) employee 테이블에서 부서코드가 D9 이거나 D6 이거나 D5인사람 조회
select * from employee where dept_code ='D9' or dept_code ='D6' or dept_code ='D5';
// 같은 표현
select * from employee where dept_code in('D9','D6','D5');
2) employee 테이블에서 부서코드가 D9,D6,D5인 사람을 제외하고 출력
select * from employee where dept_code not in('D9','D6','D5');
6. order by
- select 한 컬럼에 대해 정렬을 할 때 사용
- order by + 기준컬럼명
예제
select emp_name, salary from employee order by salary ; -- 오름차순 : default값은 오름차순(asc)
select emp_name, salary from employee order by salary desc; -- 내림차순
order by + 기준 인덱스(컬럼 인덱스 -> 1부터 시작)
select emp_name, salary from employee order by 2;
Leave a comment