1. 용어 정리
- select (프로젝션 도구)
- from (하드디스크의 테이블을 메모리로 퍼올리는 것)
- where (행 고르기 - 하드디스크에서 연산됨)
- projection (특정한 열만 선택하는 것)
- table (전체 껍데기)
- column (칼럼 제목)
- row = record (행)
- cursor (→)
- fullscan (전체 데이터를 다 봄)
- constraint (칼럼에 제약을 줌)
- unique (유일하다)
- index → random access / 다이렉트한 접근 가능. 유일하면 index가 유리
- schema (DB내에 어떤 구조로 데이터가 저장되는가를 나타내는 데이터베이스 구조)


2. Select 문법 기본
select
select*
from emp;

상세 보기
varchar의 ( )숫자는 최대 저장할 수 있는 문자 수를 의미
varchar(20) → 20자의 문자열 저장 가능
varchar(9) → 9자의 문자열 저장 가능
desc emp;

별칭 주기
별칭을 영어로 적을 경우 소문자로 적고 띄어쓰기는 _ 로 함
select empno as '사원번호'
from emp;

-- as 생략가능함
select ename '이름'
from emp;

중복 제거
--distinct_서로 다른 집합을 만든다
select distinct job
from emp;

연결 연산자
select concat(ename,'의 직업은 ',job) "직원소개"
from emp;

select ename "이름", concat(sal*12 + ifnull(comm, 0), "$") "연봉"
from emp;

연산자
select ename, sal*12
from emp;

원하는 행 찾기
select *
from emp
where ename = 'SMITH';

select *
from emp
where hiredate = '1980-12-17';

select *
from emp
where hiredate = '1980/12/17';

select *
from emp
where sal > 2000;

select *
from emp
where comm is null;

select *
from emp
where comm is not null;

where
select *
from emp
where sal = 800 and deptno = 20;

select *
from emp
where sal = 800 or sal = 1600;

select *
from emp
where sal in (800,1600);

select *
from emp
where sal between 800 and 3000;

Like 연산자
select *
from emp
where ename like 'S%';

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

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

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

정규 표현식(Regular Expression)
-- 어떤 문자열에서 특수한 규칙을 찾아야할 때
select *
from professor
where name regexp '조|형'; -- or 를 표현하며 x 또는 y 문자가 존재함을 의미

SELECT *
FROM professor
WHERE email REGEXP '^[^@]+@[^@]+\.net$';

SELECT 'Password1!' REGEXP '^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,}$' AS is_valid
from dual; -- 생략가능

SELECT 'Password!' REGEXP '^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,}$' AS is_valid
from dual; -- 생략가능

Share article