반응형
set @@global.sql_mode = "ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION";
set @@global.sql_mode =
"ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
python에서 mariadb에 인서트를 하는중에 아래와 같은 에러가 출력되었다.
pymysql.err.DataError: (1406, "Data too long for column 'name' at row 1")
넣을려는 값이 더 커서 그런가보다 했지만, 다른 쪽에서는 길었음에도 불구하고 데이터가 잘려서 인서트 되는 경우를 보게 되었다.
이상하게 생각되어 찾아보던 중 이런 설정이 있었다.
select @@global.sql_mode; |
+--------------------------------------------------------------------------------------------------------------------+ | @@global.sql_mode | +--------------------------------------------------------------------------------------------------------------------+ | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | +--------------------------------------------------------------------------------------------------------------------+ |
strict_trans_tables 의 값이 있다면 데이터가 안들어가고 에러를 내뱉고, 없다면 자동으로 알아서 데이터를 잘라서 넣어주는 것이였다.
이것을 설정을 바꾼다면 insert가 가능하다.
set @@global.sql_mode = "ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"; |
설정이 되었다고 바로 적용되는 것은 아니고 해당세션을 새로 접속하면 적용된 것을 볼 수 있다.
아래는 테스트한 결과 입니다.
mysql> use prod Database changed mysql> create table strict_table_test -> (name varchar(10)) -> ; Query OK, 0 rows affected (0.01 sec) -- 10자리 입력 mysql> insert into strict_table_test values('1111111111'); Query OK, 1 row affected (0.01 sec) -- 11자리 입력 mysql> insert into strict_table_test values('11111111145'); ERROR 1406 (22001): Data too long for column 'name' at row 1 mysql> select @@global.sql_mode; +-----------------------------------------------------------------------------------------------------------------------+ | @@global.sql_mode | +-----------------------------------------------------------------------------------------------------------------------+ | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | +-----------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> set @@global.sql_mode = -> "ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye user@macos mysql % mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 8.0.22 Homebrew Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use prod Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed -- 11자리 입력 mysql> insert into strict_table_test values('11111111145'); Query OK, 1 row affected, 1 warning (0.01 sec) mysql> select * from strict_table_test; +------------+ | name | +------------+ | 1111111111 | | 1111111114 | => 마지막 1자리를 제외하고 인서트 된 것을 확인할 수 있다. +------------+ 2 rows in set (0.00 sec) |
반응형
'MySQL' 카테고리의 다른 글
스토리지 엔진 (0) | 2022.10.21 |
---|---|
my.cnf (0) | 2022.09.28 |
transaction이 완벽하지 않은듯하다? (0) | 2022.09.28 |
시스템 환경설정 확인 방법의 차이? (0) | 2022.09.28 |
파일에 쓰기 (0) | 2022.07.28 |