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

서회정's avatar
Feb 24, 2025
[데이터베이스] 70. SELECT 기본

 

1. 용어 정리

  • select (projection 도구)
  • from (하드 디스크의 테이블을 메모리로 퍼올리는것)
  • where (행 고르기 - 하드디스크에서 연산됨)
  • projection (특정한 열만 선택하는 것)
  • table (전체 껍데기)
  • column (컬럼 제목)
  • row = record (행)
  • cursor
  • fullscan
  • constraint (column에 제약을 줄 수 있음)
  • unique (유일하다 - index)
  • index → random access
  • schema (테이블 구조) -desc
notion image
 

2. Select 문법 기본

📌
Select를 사용하여 데이터를 읽을 수 있다.
-- 1. 기본 select select * from emp; -- 2. 상세보기 desc emp; -- 3. 별칭주기 select empno as '사원번호' from emp; -- as는 생략 가능하다 select ename '이름' from emp; -- 4. 중복제거 {1,2,3,4} -> 서로 다른 집합을 만들어라. select distinct job from emp; -- 5. 연결연산자 select concat(ename, '의', job) "직원소개" from emp; -- 6. 연산자 select ename, sal*12 from emp; select ename, concat(sal*12 + ifnull(comm, 0),"$") "연봉" from emp; -- 7. 원하는 행 찾기 -- ename(이름) 찾기 select * from emp where ename='smith'; -- hiredate(입사일) 찾기, date 유형1 select * from emp where hiredate = '1980-12-17'; -- hiredate(입사일) 찾기, date 유형2 select * from emp where hiredate = '1980/12/17'; -- sal(연봉) 값이 2000 이상인 사람 찾기 select * from emp where sal > 2000; -- comm 값이 null인 사람 찾기 select * from emp where comm is null; -- comm 값이 null이 아닌 사람 찾기 select * from emp where comm is not null; -- 8. 복잡한 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 >= 800 and sal <= 3000; -- ~이상 ~이하 축약 select * from emp where sal between 800 and 3000; -- 9. LIKE 연산자 %,_ -- s로 시작하는 모든 것 찾기 select ename like 's%' from emp; -- m이 포함된 모든 항목 찾기 select ename from emp where ename like '%m%'; -- 2번째에 m이 있는 항목 찾기 (언더바 갯수) select ename from emp where ename like '_m%'; -- 4번째에 t가 있는 항목 찾기 (언더바 갯수) select ename from emp where ename like '___t%'; -- 10. 정규 표현식 - 어떤 문자열에서 특수한 규칙을 찾아야할 때 사용 -- 조로 시작하고 형으로 끝나는 이름 찾기 select * from professor where name regexp '조|형'; -- ~@~.net 형식의 이메일 주소를 가진 교수들 찾기 SELECT * FROM professor WHERE email REGEXP '^[^@]+@[^@]+\.net$'; -- 대문자+소문자+특수문자+숫자가 포함된 패스워드 검증 SELECT 'Password1!' REGEXP '^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,}$' AS is_valid from dual;
 
Share article

clubnerdy