将varchar值插入到int自动递增列中MySQL

huangapple go评论80阅读模式
英文:

MySQL insert varchar value into int auto increment column

问题

以下是您要翻译的内容:

  1. 我在项目的暂存服务器上注意到了奇怪的MySQL行为。给定:
  2. ```sql
  3. create table test_table
  4. (
  5. id int auto_increment,
  6. name varchar(64) not null,
  7. constraint test_table_pk
  8. primary key (id)
  9. );
  10. insert into test_table (id, name)
  11. VALUES (3, 'value 3');
  12. insert into test_table (id, name)
  13. VALUES (5, 'value 5');
  14. insert into test_table (id, name)
  15. VALUES ('value 6 id', 'value 6');
  16. select *
  17. from test_table;

结果:

  1. mysql> select * from test_table;
  2. +----+---------+
  3. | id | name |
  4. +----+---------+
  5. | 3 | value 3 |
  6. | 5 | value 5 |
  7. | 6 | value 6 |
  8. +----+---------+
  9. 2 rows in set (0.01 sec)

前两个插入语句按预期正常工作。第三个插入语句背后的魔法是什么?它能够工作,似乎将不正确类型的值替换为自动递增值。
我正在尝试创建DB服务器的Docker版本。但我找不到启用此行为的配置选项。Docker DB实例拒绝了第三个插入语句并显示错误消息:

  1. Incorrect Integer value: 'value 6 id' for column 'id'
  1. mysql> SHOW VARIABLES LIKE 'version';
  2. +---------------+-----------+
  3. | Variable_name | Value |
  4. +---------------+-----------+
  5. | version | 5.7.27-30 |
  6. +---------------+-----------+
  7. 1 row in set (0.01 sec)

更新

在暂存服务器上:

  1. show variables like '%sql_mode%';

返回:

  1. IGNORE_SPACE,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

我的docker-compose.yaml:

  1. version: "3.8"
  2. services:
  3. db:
  4. image: percona/percona-server:5.7.27
  5. volumes:
  6. -db-data:/var/lib/mysql
  7. env_file:
  8. - ./db.env
  9. command:
  10. --sql_mode=IGNORE_SPACE,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  11. ports:
  12. - "3306:3306"
  13. flyway:
  14. image: flyway/flyway:7.15.0
  15. command: migrate
  16. volumes:
  17. - ./../../sql/migrations:/flyway/sql
  18. - ./flyway/conf:/flyway/conf
  19. depends_on:
  20. -db
  21. volumes:
  22. db-data:

有什么想法吗?
谢谢

  1. <details>
  2. <summary>英文:</summary>
  3. I noticed strange MySQL behaviour on projects staging server. Given:

create table test_table
(
id int auto_increment,
name varchar(64) not null,
constraint test_table_pk
primary key (id)
);

insert into test_table (id, name)
VALUES (3, 'value 3');
insert into test_table (id, name)
VALUES (5, 'value 5');
insert into test_table (id, name)
VALUES ('value 6 id', 'value 6');

select *
from test_table;

  1. Results:

mysql> select * from test_table;
+----+---------+
| id | name |
+----+---------+
| 3 | value 3 |
| 5 | value 5 |
| 6 | value 6 |
+----+---------+
2 rows in set (0.01 sec)

  1. First two insert statements works fine as expected. What&#39;s the magic behind the third one? It works and it seems incorrect type value is replaced with auto increment value.
  2. I&#39;m trying to create docker version of the DB server. But I can&#39;t find what&#39;s config option to enable this behaviour. The third insert statement is rejected by docker DB instance with error message:

Incorrect Integer value: 'value 6 id' for column 'id'

mysql> SHOW VARIABLES LIKE 'version';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| version | 5.7.27-30 |
+---------------+-----------+
1 row in set (0.01 sec)

  1. UPDATE
  2. On Staging server:

show variables like '%sql_mode%';

  1. Returns:

IGNORE_SPACE,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

  1. My docker-compose.yaml

version: "3.8"
services:
db:
image: percona/percona-server:5.7.27
volumes:
-db-data:/var/lib/mysql
env_file:
- ./db.env
command:
--sql_mode=IGNORE_SPACE,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
ports:
- "3306:3306"
flyway:
image: flyway/flyway:7.15.0
command: migrate
volumes:
- ./../../sql/migrations:/flyway/sql
- ./flyway/conf:/flyway/conf
depends_on:
-db
volumes:
db-data:

  1. Any ideas?
  2. Thank you
  3. </details>
  4. # 答案1
  5. **得分**: 2
  6. 你已启用了`STRICT_TRANS_TABLES`,这会导致无效数据值被拒绝,应该从列表中移除:
  7. SET sql_mode=&#39;IGNORE_SPACE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION&#39;;
  8. [示例在此处][1]
  9. [1]: https://dbfiddle.uk/zWjNJuM9
  10. <details>
  11. <summary>英文:</summary>
  12. You have enabled `STRICT_TRANS_TABLES` which causes invalid data values to be rejected, this should be removed from the list:
  13. SET sql_mode=&#39;IGNORE_SPACE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION&#39;;
  14. [Demo here][1]
  15. [1]: https://dbfiddle.uk/zWjNJuM9
  16. </details>
  17. # 答案2
  18. **得分**: 1
  19. 这与sql_mode有关。您可以检查两个数据库实例的sql_mode参数以找出其中的秘密。
  20. `show variables like '%sql_mode%';`
  21. <details>
  22. <summary>英文:</summary>
  23. This has something to do with sql_mode. You can check the sql_mode parameters of two database instances to find out the secrets.
  24. `show variables like &#39;%sql_mode%&#39;;`
  25. </details>

huangapple
  • 本文由 发表于 2023年5月17日 21:59:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定