[데이터 베이스] 2. SELECT 기본

귤's avatar
Feb 25, 2025
[데이터 베이스] 2. SELECT 기본

1. 용어 정리

  • Select : Projection(프로젝션) 도구
  • From : 하드디스크의 테이블을 메모리로 퍼 올리는 거 (Where이 있으면 Where 데이터만 찾아서 걔만 퍼 올린다.)
  • Where : 행 골라내기 (행 고르기 - 하드디스크에서 연산됨)
  • Projection : 끄집어 내는 거 (선택) / 특정한 열만 선택 하는 것
  • Table : 전체 껍데기
  • Column : 컬럼 제목
  • Row = Record : 행
  • cursor : 가르키는 거 = 손가락
  • Full Scan : 전체 탐색
  • Constraint : 컬럼에 제약(조건)을 줄 수 있음
  • Unique : 유일하다
  • Index → Random Access : 다양하게 채울 수 있다.
  • schema : 테이블 구조 - desc
  • data base : 테이블의 집합

notion image

2. Select 문법 기본

1) 기본 select

💡
select
-- 1. 기본 select select * from emp;
notion image

2) 상세 보기

💡
desc
-- 2. 상세 보기 desc emp;
notion image

3) 별칭 주기

💡
as
-- 3. 별칭 주기 select empno as '사원 번호' from emp;
notion image

4) 중복 제거

💡
distinct
-- 4. 중복 제거 {1,2,3,4} -> 서로 다른 집합을 만들어라 = distinct select distinct job from emp;
notion image

5) 연결 연산자

💡
concat()
-- 5. 연결 연산자 select concat(ename,'의 직업은',job) "직원소개" from emp;
notion image

6) 연산자

💡
concat(), ifnull ()
-- 6. 연산자 select ename, sal*12 from emp; select ename, concat(sal*12 + ifnull(comm, 0), "$") "연봉" from emp;
notion image
notion image

7) 원하는 행 찾기

💡
where
-- 7. 원하는 행 찾기 + 날짜 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;
notion image
notion image
notion image
notion image
notion image
notion image

8) 복잡한 where

💡
where
and
in ()
between and
-- 8. 복잡한 where select * from emp where sal = 800; // sal이 800인 값 select * from emp where sal = 800 and deptno =20; // sal이 800이고 deptno가 20인 값 select * from emp where sal = 800 or sal = 1600; // sal이 800 또는 1600인 값 select * from emp where sal in (800, 1600); // sal이 800 또는 1600인 값 (sal을 두번 반복 하지 않아도 됨) select * from emp where sal between 800 and 3000; // sal이 8003000 사이인 값
notion image
notion image
notion image
notion image
notion image

9) Like 연산자

notion image
notion image
like '%, _' ~와 같다 라는 의미
💡
Like는 fullscan을 한다.
  • %모든 문자를 의미
  • _한 글자라는 의미
  • S가 들어간 모든 문자를 찾아라 (첫번째 자리가 무조건 S인 것만 찾아진다.)
select ename from emp where ename like 'S%';
notion image
  • M이 포함된 걸 다 찾아라 (자리 상관없이 M을 포함한 모든 문자들이 찾아진다.)
select ename from emp where ename like '%M%';
notion image
 
  • 두번째 자리가 ‘M’인 모든 문자를 찾아라
select ename from emp where ename like '_M%';
notion image
 
  • 네번째 글자가 T인 문자를 찾아라
select ename from emp where ename like '___T%';
notion image

10) 정규 표현식

  • 어떤 문자열에서 특수한 규칙을 찾아야할 때 사용!!
💡
  • regexp : regular Expression을 줄여서 부른다.
  • 특정한 형태나 규칙을 가진 문자열을 찾기 위해형태나 규칙을 나타내는 패턴을 정의하는 식
  • 특정한 규칙을 가진 문자열의 집합을 표현하기 위해 쓰이는 형식 언어
select * from professor where name regexp '조|형';
notion image
SELECT * FROM professor WHERE email REGEXP '^[^@]+@[^@]+\.net$';
notion image
SELECT 'Password1!' REGEXP '^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,}$' AS is_valid from dual;
notion image
💡
from dual; : table에서 퍼 올릴게 없을 때 쓰는 것 (지금은 생략 가능한 것으로 보임)
 
Share article

gyul