inblog logo
|
sxias
    데이터베이스SELECT

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

    문정준's avatar
    문정준
    Feb 24, 2025
    [데이터베이스] 2. SELECT 기본
    Contents
    1. 용어 정리2. SELECT 기본3. 테이블 속성 (Schema)4. 별칭 주기5. 중복 제거6. 연결 연산자7. 연산자8. 원하는 값 찾기9. 복잡한 where
     

    1. 용어 정리

    💡
    • SELECT : projection tool
    • FROM : HDD의 table → RAM으로 Load
    • WHERE : selection (행 고르기 - 하드디스크에서 연산됨)
    • table :
    • column : 속성
    • row = record : 테이블의 한 행 = 여러 속성 값들의 모임
    • cursor : 행에서 값들을 읽는 개체
    • projection : 선택해서 출력
    • full scan : 시간복잡도 O(n) - n번 찾아야 함
    • constraint : 제약 조건 (컬럼)
    • unique : 유일
    • index → random access (index)
    • schema : 테이블 구조
     
    notion image

    2. SELECT 기본

    • scott의 emp 테이블 확인
    use scott; select * from emp;
    • 검정색 화살표 : cursor ( 0행부터 시작 )
    notion image
     
    • ename, job만 골라서 확인
    select ename, job from emp;
    notion image
     
    • empno가 7369인 데이터만 골라서 확인
    select * from emp where empno = 7369;
    notion image
     

    3. 테이블 속성 (Schema)

    • desc (describe) : 테이블 속성 표시
    desc emp;
    • Field : 필드(컬럼) 이름
    • Type (자료형)
      • int : 숫자
      • varchar(n) : n글자/바이트 String
      • date : 날짜
      • decimal(a,b) : double ( 소수점 아래 b자리의 실수 )
    • null : 빈 값이 있는지 확인
    • key : 찾을 수 있는 기준 ( 기본 키 )
    • default : 기본 값 ( 없으면 null )
    notion image
     
     

    4. 별칭 주기

    • as : select로 선택한 projection을 다른 이름으로 보여주기
      • select ~~ as ‘abc’
    -- 3. 별칭 주기 select empno as '사원번호' from emp;
    notion image
     
    • as는 생략 가능
      • where을 사용해서 찾을 때, 별칭으로 찾으면 오류 발생
        • 별칭은 projection이 일어나면서 변경됨
    select ename '이름' from emp;
    notion image
     

    5. 중복 제거

    • select distinct : 중복되지 않는 요소로만 select
    -- 4. 중복 제거 {1,2,3,4} -> 서로 다른 집합 select distinct job from emp;
    notion image
     

    6. 연결 연산자

    • concat() : 데이터를 문자열과 함께 묶어줌
    • 컬럼명은 별칭으로 처리
    -- 5. 연결 연산자 select concat(ename,'의 직업은 ',job) as 소개 from emp;
    notion image
     

    7. 연산자

    • ifnull : 컬럼의 null 값을 다른 값으로 대체
      • null이 포함된 값을 더하면 그 값도 null이 됨
    notion image
    -- 6. 연산자 select ename, concat(sal*12 + ifnull(comm, 0), '$') '연봉' from emp;
    notion image
     

    8. 원하는 값 찾기

    • where : 원하는 값을 조건으로 대입
    -- 7. 원하는 값 찾기 select * from emp where ename = 'smith';
    notion image
     
    • 날짜의 경우 ‘-’ 와 ‘/’ 둘 다 구분
    select * from emp where hiredate = '1980-12-17';
    select * from emp where hiredate = '1980/12/17';
    • 결과는 위와 동일
     
    • 특정 값 이상, 이하, 초과, 미만인 값 : 비교연산자
    select * from emp where sal > 2000;
    notion image
     
     
    • is null : 특정 속성이 null인 값
    select * from emp where comm is null;
    notion image
     
    • is not null : 특정 속성이 null이 아닌 값
    select * from emp where comm is not null;
    notion image
     

    9. 복잡한 where

    • 동시 조건 : and
    -- 8. 복잡한 where select * from emp where sal = 800 and deptno = 20;
    notion image
     
    • 선택 조건 : or
    select * from emp where sal = 800 or sal = 1600;
    notion image
     
    • 똑같은 컬럼을 선택하는 조건의 경우는 in 연산자로 묶을 수 있음
    select * from emp where sal in (800, 1600);
     
    • between : 특정 경계 사이의 값
      • 경계의 값도 포함
    select * from emp where sal between 800 and 3000;
    notion image
     
    Share article
    Contents
    1. 용어 정리2. SELECT 기본3. 테이블 속성 (Schema)4. 별칭 주기5. 중복 제거6. 연결 연산자7. 연산자8. 원하는 값 찾기9. 복잡한 where

    Sxias ㆍ a32176740@gmail.com

    RSS·Powered by Inblog