Real MariaDB (2017년)
데이터 조작 0 0 37,765

by 구루비스터디 MariaDB DML REPLACE [2019.08.11]


3.5 데이터 조작

3.5.1 INSERT


# 사용할 데이터베이스 변경
MariaDB [test]> use mysql;
MariaDB [mysql]>

# 테이블 생성
MariaDB [test]> create table tab_test(fd1 int, fd2 varchar(50), primary key(fd1)) engine = innodb;
MariaDB [test]> insert into tab_test values (1, 'matt');
MariaDB [test]> insert into tab_test values (2, 'toto');

# 데이터 입력 및 조회
MariaDB [test]> insert into tab_test values (3, 'Lee') on duplicate key update fd2='Lee';
MariaDB [test]> select * from tab_test;
+-----+------+
| fd1 | fd2  |
+-----+------+
|   1 | matt |
|   2 | toto |
|   3 | Lee  |
+-----+------+
3 rows in set (0.00 sec)

# 동일 데이터가 이미 있을 경우 fd2를 Seonguck으로 업데이트 한다.
MariaDB [test]> insert into tab_test values (3, 'Seonguck') on duplicate key update fd2='Seonguck';
Query OK, 2 rows affected (0.00 sec)

MariaDB [test]> select * from tab_test;
+-----+----------+
| fd1 | fd2      |
+-----+----------+
|   1 | matt     |
|   2 | toto     |
|   3 | Seonguck |
+-----+----------+
3 rows in set (0.00 sec)


3.5.2 SELECT


MariaDB [test]> select * from tab_test;
+-----+----------+
| fd1 | fd2      |
+-----+----------+
|   1 | matt     |
|   2 | toto     |
|   3 | Seonguck |
+-----+----------+
3 rows in set (0.00 sec)

# 세로 형태로 보여준다.
MariaDB [test]> select * from tab_test\G
*************************** 1. row ***************************
fd1: 1
fd2: matt
*************************** 2. row ***************************
fd1: 2
fd2: toto
*************************** 3. row ***************************
fd1: 3
fd2: Seonguck
3 rows in set (0.00 sec)


3.5.3 UPDATE


MariaDB [test]> update tab_test set fd2='123' where fd1 = 3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [test]> select * from tab_test;
+-----+------+
| fd1 | fd2  |
+-----+------+
|   1 | matt |
|   2 | toto |
|   3 | 123  |
+-----+------+
3 rows in set (0.00 sec)


3.5.4 REPLACE

  • 동일한 데이터가 있으면 update 처리 되고, 없으면 insert 처리된다.
  • Insert into ... on duplicate key update와 유사한 기능이나, REPLACE 명령어는 동일한 값이 있는 경우 기존 레코드를 삭제 후 INSERT하는 방식이다.
  • replace는 부하가 높으므로 가급적 Insert into ... on duplicate key update를 사용하자

MariaDB [test]> replace tab_test set fd1=1, fd2='aaa';
Query OK, 2 rows affected (0.00 sec)

MariaDB [test]> select * from tab_test;
+-----+------+
| fd1 | fd2  |
+-----+------+
|   1 | aaa  |
|   2 | toto |
|   3 | 123  |
+-----+------+
3 rows in set (0.00 sec)

MariaDB [test]> replace tab_test set fd1=4, fd2='bbbb';
Query OK, 1 row affected (0.01 sec)

MariaDB [test]> select * from tab_test;
+-----+------+
| fd1 | fd2  |
+-----+------+
|   1 | aaa  |
|   2 | toto |
|   3 | 123  |
|   4 | bbbb |
+-----+------+
4 rows in set (0.00 sec)

# replace절에서는 where절은 사용하지 못한다.
MariaDB [test]> replace tab_test set fd1=4, fd2='bbbb' where fd1=4;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where fd1=4' at line 1


3.5.5 DELETE


MariaDB [test]> delete from tab_test where fd1=1;
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select * from tab_test;
+-----+------+
| fd1 | fd2  |
+-----+------+
|   2 | toto |
|   3 | 123  |
|   4 | bbbb |
+-----+------+
3 rows in set (0.00 sec)

"구루비 데이터베이스 스터디모임" 에서 2017년에 "Real MariaDB" 도서를 스터디하면서 정리한 내용 입니다.

- 강좌 URL : http://www.gurubee.net/lecture/4185

- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^

- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입