ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Database]DML(MySQL)
    Web/Database 2021. 4. 11. 16:50

    *DML(Data Manipulation Language) : 데이터 조작어. 데이터베이스 객체에서 데이터를 CRUD(Create Read Update Delete : insert select update delete)한다.

     

    (1) 데이터 삽입(INSERT)

    -- 모든 열에 데이터를 삽입하는 경우
    insert into table_name
    values (val1, val2, ...);
    
    -- 특정 열에 데이터를 삽입하는 경우(권장)
    insert into table_name (col_name1, col_name2, ...)
    values (val1, val2, ...);
    
    -- 여러 데이터(행)를 한 번에 삽입하는 경우
    insert into table_name (col_name1, col_name2, ...)
    values (val1, val2, ...),
           (val1, val2, ...);
           
    -- Ex
    insert into student (studentname, studentid, studentpwd)
    values ("kim", "asas", "1234"),
           ("lee", "asdf", "1234");

     

    (2) 데이터 변경(UPDATE)

    -- UPDATE(where 절을 생략하면 모든 데이터가 바뀌니 주의!).
    update table_name
    set col_name1 = val1, ...
    where conditions;
    
    -- Ex
    update student
    set studentname = "park", studentid = "aaaa"
    where studentno = 2;

     

    (3) 데이터 삭제(DELETE)

    -- DELETE(UPDATE와 마찬가지로 where절을 삭제하면 모든 데이터가 삭제되니 주의!).
    delete from table_name
    where conditions;
    
    -- Ex
    delete from student
    where studentno = 3;

     

    (4) 데이터 조회(READ)

    -> 테이블 조회(기본)

    -- 전체 테이블 조회
    select *
    from table_name;
    
    -- Ex
    select *
    from student;
    -- 특정 열을 선택하여 조회
    select col_name, col_name2, ...
    from table_name;
    
    -- Ex
    select studentNo, studentName
    from student;
    
    -- 중복되는 행을 제외하고 조회(DISTINCT 명령어)
    select distinct col_name
    from studnt;

     

    -> 별칭 : 열에 사용자 지정 이름을 새로 붙인다(AS).

    -- 문법
    select col_name as "alias"
    from table_name;
    
    -- Ex
    select studentNo as "학번"
    from student;

     

    -> 조건을 만족하는 테이블 내용 조회 : WHERE 절

    -- Ex : 나이가 21살 이상인 학생 조회
    select *
    from student
    where age >= 21;
    
    -- 조건에 AND, OR, NOT을 활용할 수 있다.
    -- AND Ex : 나이가 21살이고, 이름이 kim인 학생 조회
    select *
    from student
    where age = 21 and studentName = "kim";
    
    -- OR Ex : 나이가 21살 이거나, 이름이 kim인 학생 조회
    select *
    from student
    where age = 21 or studentName = "kim";
    
    -- NOT Ex : 나이가 21살이 아닌 학생 조회
    select *
    from student
    where not (age = 21);
    
    -- IN, BETWEEN을 이용한 범위 조건
    -- IN Ex : 나이가 21, 22, 23살인 학생 조회
    select *
    from student
    where age in (21, 22, 23);
    
    -- BETWEEN Ex : 나이가 21, 22, 23살인 학생 조회
    select *
    from student
    where age BETWEEN 21 AND 23;
    
    -- NULL 값에 대한 비교 : NULL 값은 '-' 연산자가 아닌 'is' 연산자를 사용해야 한다.
    select *
    from student
    where studentNo is null;

    ※ LIKE 명령어와 wild card(%, _) : 특정 문자를 포함한 대상을 검색할 수 있다.

    -- % : 어떤 문자가 몇 개가 와도 상관 없다.
    -- 이름이 "김"으로 시작하는 모든 학생 조회
    select *
    from student
    where studentName like "김%";
    
    -- 이름에 "김"을 포함하는 모든 학생 조회
    select *
    from student
    where studentName like "%김%";
    
    -- 이름이 "김"으로 끝나는 모든 학생 조회
    select *
    from student
    where studentName like "%김";
    
    -- _ : 해당 문자의 개수 만큼 해당 위치에 어떤 문자가 와도 상관 없다.
    -- 이름이 "김"으로 시작하고 3글자인 모든 학생 조회
    select *
    from student
    where studentName like "김__";
    
    -- 이름 중간에 "김"을 포함하고 3글자인 모든 학생 조회
    select *
    from student
    where studentName like "_김_";

     

    -> 출력행 정렬 : ORDER BY 절

    -- 테이블을 오름차순(ASC : 디폴트), 내림차순(DESC)으로 정렬한다.
    -- ASC Ex : 학생의 나이순 오름차순으로 정렬
    select *
    from student
    order by age;
    
    -- DESC Ex : 학생의 나이순 내림차순으로 정렬
    select *
    from student
    order by age desc;

     

    *MySQL API

    -> 앞서 제시한 데이터 타입, 혹은 기존에 익숙한 프로그래밍 언어와 마찬가지로 MySQL의 자세한 문법이나 사용을 보고 싶으면 API문서를 참고하는 습관을 들이도록 하자.

    dev.mysql.com/doc/refman/8.0/en/sql-statements.html

     

    MySQL :: MySQL 8.0 Reference Manual :: 13 SQL Statements

     

    dev.mysql.com

    'Web > Database' 카테고리의 다른 글

    [Database]서브 쿼리(MySQL)  (0) 2021.04.12
    [Database]조인과 집합연산(MySQL)  (0) 2021.04.11
    [Database]내장 함수, 집계 함수와 GROUP BY절(MySQL)  (0) 2021.04.11
    [Database]DDL(MySQL)  (0) 2021.04.11
    [Database]SQL 개요  (0) 2021.04.11

    댓글

Designed by Tistory.