[DataBase] 3. 기본 문법

백하림's avatar
Feb 24, 2025
[DataBase] 3. 기본 문법

1. select

-- 1. 기본 select 전체 출력 select * from emp;
notion image

2. 스키마 보는 방법 - desc

-- 2. 상세보기 (스키마) desc emp;
notion image

3. 별칭 지어주기 - as

-- 3. 별칭 주기 (as 생략 가능) select empno as '사원 번호' from emp;
notion image
select ename '이름' from emp;
notion image

4. 중복 제거 - distinct

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

5. 연결 연산자 - concat

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

6. 연산자 ex) 연봉 * 12

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

7. 원하는 행 찾기 where

select * from emp where ename = 'smith';
notion image
select * from emp where hiredate = '1980-12-17';
notion image
select * from emp where hiredate = '1980/12/17';
notion image
select * from emp where sal >= 2000;
notion image

8. is null, is not null

select * from emp where comm is null;
notion image
select * from emp where comm is not null;
notion image

9. and - 두 가지 조건 충족 할 때

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

10. or - 둘 중 하나라도 충족할 때

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

11. in - or의 역할

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

12. between and- 두 범위 사이에 있는 값

select * from emp where sal between 800 and 3000;
notion image

13. regexp - 정규 표현식

-- 10. 정규 표현식 (어떤 문자열에서 특수한 규칙을 찾아야 할 때 사용) select * from professor where name regexp '조|형';
💡
  • | : OR(또는) 연산자
  • "조" 또는 "형"이 포함된 값 찾기
notion image
SELECT * FROM professor WHERE email REGEXP '^[^@]+@[^@]+\.net$';
💡
  • ^ : 문자열의 시작을 의미.
  • [^@]+ : '@' 문자가 아닌 문자들이 1개 이상 나오는 부분. 즉, 이메일의 아이디 부분.
  • @ : 반드시 '@' 문자가 포함되어야 함.
  • [^@]+ : 다시 '@' 문자가 아닌 문자들이 1개 이상 나오는 부분. 즉, 이메일의 도메인 부분(일부).
  • \.net : 문자 그대로 .net 이어야 함.
  • $ : 문자열의 끝을 의미.
notion image

패스워드 검증 쿼리문

SELECT 'Password1!' REGEXP '^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,}$' AS is_valid from dual;
💡
  • REGEXP : MySQL에서 정규식 패턴 일치 여부를 확인하는 연산자
  • (?=.*[A-Z]) : 대문자 최소 1개 포함
  • (?=.*[a-z]) : 소문자 최소 1개 포함
  • (?=.*[0-9]) : 숫자 최소 1개 포함
  • (?=.*[!@#$%^&*()_+={}\[\]:;<>,.?/~]) : **특수문자 최소 1개 포함** (\W를 사용하지 않는 이유: MySQL의 \W`는 공백도 포함하기 때문)
  • .{8,} : 최소 8자 이상
notion image
Share article

harimmon