[mysql/maria] upsert (insert /update)
upsert 라는 기능이 있습니다. (있으면 update 없으면 insert)
버전에 따라서 지원을 안할 경우 트리거를 이용하여 할수도 있으나
버전이 된다면 아래와 같은 명령어로 사용할 수 있습니다.
(test해보지 않아 문법이 틀릴 수 있습니다. 자세한 사항은 Docs를 참조해주세요.)
(버전에 따라 명령어가 실행되지 않을 수 있습니다. 적용되는 버전은 Docs를 참조해주세요.)
t1
----------------
a | b | c |
----------------
1 | 2 | 3 |
a unique key
insert / update
INSERT INTO t1 (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
or
UPDATE t1 SET c=c+1 WHERE a=1;
oracle nosql
UPSERT INTO t1 (a,b,c) VALUES (1,2,3); -- 4로 바꿀 순 없다.
mysql / maria
INSERT INTO t1 (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
oracle
MERGE INTO t1
USING dual
ON t1.a=1
WHEN MATCHED THEN
UPDATE SET t1.c = t1.c+1
WHEN NOT MATCHED THEN INSERT (t1.a, t1.b, t1.c)
VALUES (1,2,3)
https://mariadb.com/kb/en/insert-on-duplicate-key-update/
INSERT ON DUPLICATE KEY UPDATE
INSERT if no duplicate key is found, otherwise UPDATE
mariadb.com
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
MySQL :: MySQL 8.0 Reference Manual :: 13.2.6.2 INSERT ... ON DUPLICATE KEY UPDATE Statement
13.2.6.2 INSERT ... ON DUPLICATE KEY UPDATE Statement If you specify an ON DUPLICATE KEY UPDATE clause and a row to be inserted would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row occurs. For example, if column a is de
dev.mysql.com
Getting Started with SQL for Oracle NoSQL Database
docs.oracle.com
https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF01606
MERGE
Prerequisites You must have the INSERT and UPDATE object privileges on the target table and the SELECT object privilege on the source table. To specify the DELETE clause of the merge_update_clause, you must also have the DELETE object privilege on the targ
docs.oracle.com