Versions Compared

Key

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

데이터 입력

휘발성 테이블의 데이터 입력은 다음과 같다.

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


 

데이터 갱신

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


 

삽입할 데이터 값으로 갱신

INSERT 구문에서 삽입할 데이터를 지정했지만, 삽입 데이터의 기본 키 값과 일치하는 다른 데이터가 존재하는 경우에는 INSERT 구문이 실패하게 되고 해당 데이터는 삽입되지 않는다. 삽입 데이터의 기본 키 값과 일치하는 다른 데이터가 존재하는 경우에, 삽입이 아닌 해당 데이터를 갱신하고자 하는 경우에는 ON DUPLICATE KEY UPDATE 절을 추가할 수 있다.

  • 기본 키 중복 데이터가 존재하지 않는 경우, 삽입할 데이터 내용이 그대로 삽입된다.

  • 기본 키 중복 데이터가 존재하는 경우, 삽입할 데이터 내용으로 기존의 데이터가 갱신된다.

이 기능을 사용하기 위한 제약 조건은 다음과 같다.

  • 휘발성 테이블에 기본 키가 지정되어 있어야 한다.

  • 삽입하고자 하는 값에, 기본 키 값이 반드시 포함되어야 한다.

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

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

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


 

갱신할 데이터 값을 지정

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

  • 기본 키 중복 데이터가 존재하지 않는 경우, 삽입 데이터 내용이 그대로 삽입된다.

  • 기본 키 중복 데이터가 존재하는 경우, SET 절에 명시된 갱신 데이터만으로 기존의 데이터가 갱신된다.

  • 기본 키 값을 갱신할 데이터 값으로 지정할 수 없다.

  • SET 절에서 명시되지 않은 컬럼들의 값은 갱신되지 않는다.

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

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

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


 

데이터 삭제

휘발성 테이블은 조건 절(WHERE 절)에서 기본 키 값 조건을 이용해 이용해 데이터를 삭제할 수 있다.

  • 휘발성 테이블에 기본 키 컬럼이 지정되어 있어야 한다.

  • (기본 키 컬럼) = (값)의 조건만 허용하며, 다른 조건과 함께 사용할 수 없다.

  • 기본 키 컬럼이 아닌 다른 컬럼을 사용할 수 없다.

iFlux> create volatile table vtable (id integer primary key, name varchar(20));
Created successfully.
iFlux> insert into vtable values(1, 'west device');
1 row(s) inserted.
iFlux> insert into vtable values(2, 'east device');
1 row(s) inserted.
iFlux> insert into vtable values(3, 'north device');
1 row(s) inserted.
iFlux> insert into vtable values(4, 'south device');
1 row(s) inserted.
iFlux> select * from vtable;
ID          NAME                  
-------------------------------------
1           west device           
2           east device           
3           north device          
4           south device          
[4] row(s) inserted.
iFlux> delete from vtable where id = 2;
[1] row(s) deleted.
iFlux> select * from vtable;
ID          NAME                  
-------------------------------------
1           west device           
3           north device          
4           south device          
[3] row(s) selected.


 

데이터 조회

데이터 조회는 다른 테이블 유형과 마찬가지로, 아래와 같이 수행할 수 있다.

iFlux> create volatile table vtable (id integer primary key, name varchar(20));
Created successfully.
iFlux> insert into vtable values(1, 'west device');
1 row(s) inserted.
iFlux> insert into vtable values(2, 'east device');
1 row(s) inserted.
iFlux> insert into vtable values(3, 'north device');
1 row(s) inserted.
iFlux> insert into vtable values(4, 'south device');
1 row(s) inserted.
iFlux> select * from vtable;
ID          NAME                  
-------------------------------------
1           west device           
2           east device           
3           north device          
4           south device          
[4] row(s) selected.
iFlux> select * from vtable where id = 1;
ID          NAME                  
-------------------------------------
1           west device           
[1] row(s) selected.
iFlux> select * from vtable where name like 'west%';
ID          NAME                  
-------------------------------------
1           west device           
[1] row(s) selected.