개발 공부/mysql

[Mysql] mysql 중복된 행 있을 시 무시 하고 다음 행 INSERT 하는법

solutionMan 2024. 7. 17. 11:39
반응형

쿼리문

INSERT IGNORE INTO [TABLE] (COLUMN1, COLUMN2, ...)VALUES (VALUE1, VALUE2, ...)

 

MySQL :: MySQL 8.4 Reference Manual :: 15.2.7 INSERT Statement

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [PARTITION (partition_name [, partition_name] ...)] [(col_name [, col_name] ...)] { {VALUES | VALUE} (value_list) [, (value_list)] ... } [AS row_alias[(col_alias [, col_alias] ...)]]

dev.mysql.com

 

원문

 

  • If you use the IGNORE modifier, ignorable errors that occur while executing the INSERT statement are ignored. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row is discarded and no error occurs. Ignored errors generate warnings instead.Data conversions that would trigger errors abort the statement if IGNORE is not specified. With IGNORE, invalid values are adjusted to the closest values and inserted; warnings are produced but the statement does not abort. You can determine with the mysql_info() C API function how many rows were actually inserted into the table.You can use REPLACE instead of INSERT to overwrite old rows. REPLACE is the counterpart to INSERT IGNORE in the treatment of new rows that contain unique key values that duplicate old rows: The new rows replace the old rows rather than being discarded. See Section 15.2.12, “REPLACE Statement”.
  • For more information, see The Effect of IGNORE on Statement Execution.
  • IGNORE has a similar effect on inserts into partitioned tables where no partition matching a given value is found. Without IGNORE, such INSERT statements are aborted with an error. When INSERT IGNORE is used, the insert operation fails silently for rows containing the unmatched value, but inserts rows that are matched. For an example, see Section 26.2.2, “LIST Partitioning”.

 

번역

만약 IGNORE 모디파이어를 사용하면, INSERT 문을 실행하는 동안 발생하는 무시할 수 있는 오류들이 무시됩니다.

예를 들어, IGNORE 없이 중복된 UNIQUE 인덱스나 PRIMARY KEY 값을 가진 행을 삽입하려고 하면 중복 키 오류가 발생하여 문이 중단됩니다.

 

IGNORE를 사용하면, 해당 행은 버려지고 오류는 발생하지 않습니다. 무시된 오류는 경고를 생성합니다.

 

IGNORE는 주어진 값에 매칭되는 파티션이 없는 파티션된 테이블에 삽입할 때도 비슷한 효과를 가집니다. IGNORE 없이, 이러한 INSERT 문은 오류로 중단됩니다. INSERT IGNORE를 사용할 때는, 매칭되지 않는 값을 가진 행에 대해서는 삽입 작업이 조용히 실패하지만, 매칭된 행은 삽입됩니다.

 

예시는 Section 26.2.2, "LIST Partitioning"을 참조하세요.

 

데이터 변환에서 오류가 발생하면, IGNORE가 지정되지 않은 경우 문이 중단됩니다. IGNORE를 사용하면, 잘못된 값은 가장 가까운 값으로 조정되어 삽입되고, 경고가 생성되지만 문은 중단되지 않습니다. 몇 개의 행이 실제로 테이블에 삽입되었는지는 mysql_info() C API 함수를 통해 확인할 수 있습니다.

자세한 내용은 "The Effect of IGNORE on Statement Execution"을 참조하세요.

 

SELECT ROW_COUNT();

MYSQL에서 INSERT, DELETE, UPDATE 쿼리를 통해 수행된 row 수를 RETURN

SELECT 후 사용하면 '-1'을 리턴함

 

 

INSERT IGNORE 대신 REPLACE를 사용하여 기존 행을 덮어쓸 수 있습니다.

REPLACE는 새로운 행이 기존 행과 중복되는 고유 키 값을 포함하는 경우 새로운 행이 버려지는 대신 기존 행을 대체한다는 점에서 INSERT IGNORE의 대응물입니다.

자세한 내용은 Section 15.2.12, "REPLACE Statement"를 참조하세요.

 

찾은 이유

PRI 에 대한 DUP발생 때문에 다른 데이터들이 안들어가는 경우를 제외 하는법이 없을 까 찾아보다

역시 있었다는것을 알고 정리

반응형