Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagesql
Mach> create volatile table vtable (id integer, name varchar(20));
Created successfully.
Mach> insert into vtable values(1, 'west device');
1 row(s) inserted.
Mach> insert into vtable values(2, 'east device');
1 row(s) inserted.
Mach> insert into vtable values(3, 'north device');
1 row(s) inserted.
Mach> insert into vtable values(4, 'south device');
1 row(s) inserted.
Mach>

 

데이터 갱신

휘발성 테이블의 데이터 입력 시, ON DUPLICATE KEY UPDATE 절을 사용해 중복된 기본 키 값을 가진 데이터의 갱신을 할 수 있다.

...

Code Block
languagesql
Mach> create volatile table vtable (id integer primary key, direction varchar(10), refcnt integer);
Created successfully.
Mach> insert into vtable values(1, 'west', 0);
1 row(s) inserted.
Mach> insert into vtable values(2, 'east', 0);
1 row(s) inserted.
Mach> select * from vtable;
ID          DIRECTION   REFCNT      
----------------------------------------
1           west       0           
2           east        0           
[2] row(s) selected.

Mach> insert into vtable values(1, 'south', 0);
[ERR-01418 : The key already exists in the unique index.]
Mach> insert into vtable values(1, 'south', 0) on duplicate key update;
1 row(s) inserted.

Mach> select * from vtable;
ID          DIRECTION   REFCNT      
----------------------------------------
1           south        0           
2           east        0           
[2] row(s) selected.
Mach>




갱신할 데이터 값을 지정

위와 비슷하지만, 삽입할 데이터 값과 다른 컬럼 값으로 갱신해야 하는 경우에는 ON DUPLICATE KEY UPDATE SET 절을 통해 지정할 수 있다. SET 절 아래에 갱신할 데이터 값을 지정할 수 있다.

...

Code Block
languagesql
Mach> create volatile table vtable (id integer primary key, direction varchar(10), refcnt integer);
Created successfully.
Mach> insert into vtable values(1, 'west', 0);
1 row(s) inserted.
Mach> insert into vtable values(2, 'east', 0);
1 row(s) inserted.
Mach> select * from vtable;
ID          DIRECTION   REFCNT      
----------------------------------------
1           west        0           
2           east        0           
[2] row(s) selected.

Mach> insert into vtable values(1, 'west', 0) on duplicate key update set refcnt = 1;

1 row(s) inserted.
Mach> select * from vtable;
ID          DIRECTION   REFCNT      
----------------------------------------
1           west        1           
2           east        0           
[2] row(s) selected.
Mach>