...
Section | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
...
INSERT ON DUPLICATE KEY UPDATE
마크베이스는, 흔히 알려진 UPSERT 기능과 유사한 구문을 지원한다.
기본 키가 지정된 Lookup/Volatile 테이블에 값을 입력할 때 사용할 수 있는 특수 구문으로, 기본 키 값이 중복되는 데이터가 이미 테이블에 존재하는 경우에는 기존 데이터의 값이 변경된다.
물론, 키 값이 중복되는 데이터가 존재하지 않는 경우에는 새로운 데이터로 삽입된다.
이 구문을 사용하기 위해서, 휘발성 테이블에 기본 키가 지정되어 있어야 한다.
삽입되는 데이터의 컬럼 값과 갱신되는 데이터의 컬럼 값을 다르게 하고자 하는 경우, 또는 삽입되는 데이터의 컬럼 값이 아닌 다른 컬럼 값을 갱신하고자 하는 경우에는 SET 절을 추가로 입력할 수 있다.
...
SET 절에는 '컬럼=값'으로 구성되며, 각각을 콤마로 구분해야 한다.
...
Machbase supports syntax similar to the commonly known UPSERT function.
A special syntax that can be used when entering a value into a Lookup/Volatile table with a primary key specified. If the table already contains data with a duplicate primary key value, the value of the existing data is changed.
Of course, when there is no duplicated key value data, it is inserted as new data.
To use this syntax, the primary key must be specified in the volatile table.
If the column value of the inserted data is different from the column value of the updated data, or if it is desired to update a column value other than the column value of the inserted data, the SET clause can be further input.
The SET clause consists of 'column = value', each separated by a comma.
You must not change the default key value in the SET clause.
INSERT SELECT
...
insert_select_stmt:
...
Code Block |
---|
insert_select_stmt ::= 'INSERT INTO' table_name ( '(' insert_column_list ')' )? select_stmt |
특정 table에 대해서 SELECT 문의 수행 결과를 삽입하는 문장이다. 기본적으로는 다른 DBMS와 유사하지만 다음의 차이점이 있다.
...
_ARRIVAL_TIME 컬럼 값은 select 및 INSERT 컬럼 리스트에서 지정되지 않으면 INSERT SELECT 문이 수행되는 시점의 시간 값으로 입력된다.
...
VARCHAR 타입의 컬럼에 대해서 삽입되는 입력값이 컬럼의 최대 길이보다 큰 경우, 오류를 발생시키지 않고 해당 컬럼의 최대 길이만큼 잘라서 입력된다.
...
형 변환이 가능한 경우(숫자형->숫자형)에는 입력되는 컬럼 값에 맞게 삽입된다.
...
수행 도중 오류가 발생한 경우 ROLLBACK되지 않는다.
...
This statement inserts the result of the SELECT statement on a specific table. In basic, it is similar to other DBMSs, but with the following differences.
The _ARRIVAL_TIME column value is entered as the time value at the time the INSERT SELECT statement is executed unless specified in the select and INSERT column lists.
If the input value to be inserted for a VARCHAR type column is greater than the maximum length of the column, the maximum length of the corresponding column is entered without error.
If type conversion is possible (numeric -> numeric), it is inserted according to the input column value.
ROLLBACK does not occur if an error occurs during execution.
If you insert a value in the _ARRIVAL_TIME column, the new value will not be entered if it has a time before the existing value.
Code Block | ||||
---|---|---|---|---|
| ||||
create table t1 (i1 integer, i2 varchar(60), i3 varchar(5)); Created successfully. insert into t1 values (1, 'a', 'ddd' ); 1 row(s) inserted. insert into t1 values (2, 'kkkkkkkkkkkkkkkkkkkkk', 'c'); 1 row(s) inserted. insert into t1 select * from t1; 2 row(s) inserted. create table t2 (i1 integer, i2 varchar(60), i3 varchar(5)); insert into t2 (_arrival_time, i1, i2, i3) select _arrival_time, * from t1; 4 row(s) inserted. |
UPDATE
...
Info |
---|
This function is available from 5.5 부터 제공되는 기능입니다. |
update_stmt:
update_expr_list:
...
Code Block |
---|
update_stmt ::= 'UPDATE' table_name ( 'METADATA' )? 'SET' update_expr_list 'WHERE' primary_key_column '=' value update_expr_list ::= update_expr ( ',' update_expr)* update_expr ::= column '=' value |
INSERT ON DUPLICATE KEY UPDATE 를 통한 UPSERT 가 아닌, UPDATE 구문도 제공된다.역시, 기본 키 (Primary Key) 가 지정된 Lookup/Volatile 테이블에 값을 입력할 때 사용할 수 있다. WHERE 절에는 기본 키의 일치 조건식을 작성해야 한다UPDATE syntax is also provided, rather than UPSERT via a KEY UPDATE.
You can also use the Primary Key to enter values into the specified Lookup/Volatile table. In the WHERE clause, you must create a matching predicate for the primary key.
UPDATE METADATA
Only for the TAGDATA 테이블에 한해서, 메타데이터를 업데이트 하고자 할 때 사용한다table, when you want to update the metadata.
Code Block | ||||
---|---|---|---|---|
| ||||
UPDATE TAG METADATA SET ... |
Note |
---|
TAGDATA 테이블의 메타데이터는 The metadata of the TAGDATA table can not be entered/modified through INSERT ON DUPLICATE KEY UPDATE 를 통해 입력/수정할 수 없다. |
DELETE
...
delete_stmt:
time_unit:
Code Block |
---|
delete_stmt ::= 'DELETE FROM' table_name ( 'OLDEST' number 'ROWS' | 'EXCEPT' number ( 'ROWS' | time_unit ) | 'BEFORE' datetime_expression )? 'NO WAIT'? time_unit ::= 'DURATION' number time_unit ( ( 'BEFORE' | 'AFTER' ) number time_unit )? |
마크베이스에서의 DELETE BEFORE 구문은 로그 테이블, Tag 테이블, Rollup table에 대해서 수행 가능하다. 중간의 임의 위치에 있는 데이터를 삭제할 수 없으며, 임의의 위치부터 연속적으로 마지막(가장 오래된 로그) 레코드까지 지울 수 있도록 구현되었다.
이는 로그 데이터의 특성을 살린 정책으로서 한번 입력되면 수정이 없고, 공간 확보를 위해 파일을 삭제하는 행위를 DB 형식으로 표현한 것이다.
DURATION, OLDEST, EXCEPT 구문은 TAG 및 Rollup 테이블에 대해서는 사용할 수 없다The DELETE statement in Machbase can be performed on the log table. In addition, it is not possible to delete data in an arbitrary position in the middle, and it is possible to erase consecutively from the arbitrary position to the last (oldest log) record.
This is a policy that takes advantage of the characteristics of log data. It is a DB format representation of the act of deleting a file in order to secure space when it is entered once.
The syntax of DURATION, OLDEST, and EXCEPT cannot be used for TAG and Rollup tables.
Code Block | ||||
---|---|---|---|---|
| ||||
-- 모두Delete 삭제하라all. DELETE FROM devices; -- 가장Delete 오래된oldest 마지막last N건을N 삭제하라rows. DELETE FROM devices OLDEST N ROWS; -- Delete 최근all N건을except 제외하고recent 모두N 삭제하라rows. DELETE FROM devices EXCEPT N ROWS; -- 지금부터 N일치를 남기고 모두 삭제하라Delete all except N matches from now on. DELETE FROM devices EXCEPT N DAY; -- Delete 2014년all 6월data 1일from 이전의before 데이터를June 모두1, 삭제하라2014. DELETE FROM devices BEFORE TO_DATE('2014-06-01', 'YYYY-MM-DD'); -- Delete tag data from 데이터의before 시간June 기준1, 삭제2014. DELETE FROM tag BEFORE TO_DATE('2014-06-01', 'YYYY-MM-DD'); -- Delete tag rollup data 데이터의from before 시간June 기준1, 삭제2014. DELETE FROM tag ROLLUP BEFORE TO_DATE('2014-06-01', 'YYYY-MM-DD'); |
...
Code Block | ||||
---|---|---|---|---|
| ||||
create volatile table t1 (i1 int primary key, i2 int); Created successfully. insert into t1 values (2,2); 1 row(s) inserted. delete from t1 where i1 = 2; 1 row(s) deleted. |
휘발성 테이블에 대해서만 수행 가능한 구문으로, WHERE 절에 작성된 조건에 일치하는 레코드만 삭제할 수 있다.
기본 키가 지정된 휘발성 테이블에 대해서만 수행 가능하다.
WHERE 절에는 (기본 키 컬럼) = (값) 의 조건만 허용되며, 다른 조건과 함께 작성할 수 없다.
기본 키 컬럼이 아닌 다른 컬럼을 조건에 사용할 수 없다You can only delete records that match the conditions created in the WHERE clause, which can only be performed on volatile tables.
The primary key can only be performed on the specified volatile table.
The WHERE clause allows only conditions of (primary key column) = (value), and can not be created with other conditions.
You can not use a column other than the primary key column in the condition.
delete_from_tag_where_stmt:
...
Code Block | ||||
---|---|---|---|---|
| ||||
delete_from_tag_where_stmt ::= 'DELETE FROM' table_name 'WHERE' tag_name '=' value ( and tag_time '<' datetime_expression )? |
Tag 테이블은 아래와 같이 2가지 방식의 삭제쿼리가, 추가적으로 지원된다.
...
The Tag table additionally supports two types of deletion queries as follows.
- Delete by tag name
- Delete by tag name and tag time
Code Block | ||
---|---|---|
| ||
-- Delete by tag name 기준 삭제 DELETE FROM tag where tag_name = 'my_tag_2021' -- Delete by tag name 와and tag time 기준 삭제 DELETE FROM tag where tag_name = 'my_tag_2021' and tag_time < TO_DATE('2021-07-01', 'YYYY-MM-DD'); |
- 삭제 쿼리가 실행된 후에, 삭제된 row가 저장공간에서 물리적으로 삭제되기 까지 걸리는 시간은, DBMS의 동작상황에 따라서 다를 수 있다The time it takes for the deleted row to be physically deleted from the storage space after the deletion query is executed may vary depending on the situation of the DBMS.
LOAD DATA INFILE
...
load_data_infile_stmt:
...
Code Block | ||||
---|---|---|---|---|
| ||||
load_data_infile_stmt: 'LOAD DATA INFILE' file_name 'INTO TABLE' table_name ( 'TABLESPACE' tbs_name )? ( 'AUTO' ( 'BULKLOAD' | 'HEADUSE' | 'HEADUSE_ESCAPE' ) )? ( ( 'FIELDS' | 'COLUMNS' ) ( 'TERMINATED BY' char )? ( 'ENCLOSED BY' char )? )? ( 'TRIM' ( 'ON' | 'OFF' ) )? ( 'IGNORE' number ( 'LINES' | 'ROWS' ) )? ( 'MAX_LINE_LENGTH' number )? ( 'ENCODED BY' coding_name )? ( 'ON ERROR' ( 'STOP' | 'IGNORE' ) )? |
CSV 포맷의 데이터 파일을 서버에서 직접 읽어서, 옵션에 따라 서버에서 직접 테이블 및 컬럼들을 생성하여 이를 입력하는 기능이다.
각 옵션에 대해서 설명하면 다음과 같다.
...
옵션
...
format data files are read directly from the server, and tables and columns are created directly from the server according to the options and input them.
Each option is explained as follows
Options | Description |
---|---|
AUTO mode_string mode_string = (BULKLOAD | HEADUSE | HEADUSE_ESCAPE) |
해당 테이블을 생성하고 컬럼 타입(자동 생성시 varchar type) 및 컬럼명을 자동으로 생성한다.
BULKLOAD: 데이터 한 개의 row를 하나의 컬럼으로 입력한다. 컬럼으로 구분할 수 없는 데이터에 대해서 사용한다.
Creates the corresponding table and automatically generates the column type (varchar type for automatic creation) and the column name. |
option, but appends a '_' character to the front and back of the column name to avoid errors that can occur if the column name is the same as the reserved word in the DB. If a special character exists in the column name, changes it to '_'. |
(FIELDS|COLUMNS) TERMINATED BY 'term_char' ESCAPED BY 'escape_char' |
Specifies the delimiter (term_char) |
and escape character (escape_char) |
for parsing the data line. For common CSV files, the delimiter is , and the escape character is '. |
ENCODED BY coding_name coding_name = { UTF8(default) | MS949 | KSC5601 | EUCJP | SHIFTJIS | BIG5 | GB231280 } |
Specifies the encoding options for the data file. The default value is UTF-8. |
TRIM (ON | OFF) |
Removes or maintains the empty space of the column. The default is ON. |
IGNORE number (LINES | ROWS) |
Ignores data for a specified number of lines or lines. It is used to ignore header of CSV format file or ignore VCF header. |
MAX_LINE_LENGTH |
Specifies the maximum length of one line. The default value is 512K. If the data is larger, you can specify a larger value. |
ON ERROR (STOP | IGNORE) |
입력 도중 에러가 발생할 경우 수행할 동작을 지정한다. STOP인 경우 입력을 중단하고 IGNORE인 경우 에러가 발생한 라인을 건너뛰고 계속 입력한다.
Specifies the action to take when an error occurs during input. If it is STOP, input is stopped. If it is IGNORE, the line where error occurred is skipped and input is continued. |
Code Block | ||||
---|---|---|---|---|
| ||||
-- Use default field delimiter(,) field encloser (") 를to 사용하여input 데이터를 입력한다data. LOAD DATA INFILE '/tmp/aaa.csv' INTO TABLE Sample_data ; -- Create 하나의NEWTABLE with 컬럼을one 갖는column NEWTABLE을and 생성해서enter 한one 라인을line 한as 컬럼으로one 입력한다column. LOAD DATA INFILE '/tmp/bbb.csv' INTO TABLE NEWTABLE AUTO BULKLOAD; -- Create NEWTABLE csv의using 첫번째first 라인을line 컬럼of 정보로csv 이용하여as NEWTABLE을column 생성하고information, and 이를input 그it 테이블에into 입력한다table. LOAD DATA INFILE '/tmp/bbb.csv' INTO TABLE NEWTABLE AUTO HEADUSE; -- 첫번째 라인은 무시하고 필드 구분자는 First line is ignored and field delimiter is ; and enclosing 문자는character 'is 로specified 지정해서by 입력한다'. LOAD DATA INFILE '/tmp/ccc.csv' INTO TABLE Sample_data FIELDS TERMINATED BY ';' ENCLOSED BY '\'' IGNORE 1 LINES ON ERROR IGNORE; |
Tip |
---|
AUTO 옵션을 사용하지 않는 경우 테이블의 모든 컬럼은 VARCHAR 또는 TEXT 타입으로 생성해야 한다If the AUTO option is not used, all columns of the table must be created as VARCHAR or TEXT types. |