Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: ERROR 1364 (HY000): Field ‘xxx’ doesn’t have a default value
【TiDB Environment】Dev
【TiDB Version】v6.1.0
【Problem Encountered】
Due to the inability to use the Create Table select command, I used show create table to get the table definition, then manually created the table. When inserting data, I encountered ERROR 1364 (HY000): Field ‘xxx’ doesn’t have a default value.
【Reproduction Path】Can be reproduced at any time
【Problem Phenomenon and Impact】
-
First, create the original table
create table testrb(id bigint(20) auto_random primary key, name varchar(2) default null); -
Insert record
MySQL [raydb]> insert into testrb(name) values(‘tr’);
Query OK, 1 row affected (0.031 sec)
MySQL [raydb]> select * from testrb;
±--------------------±-----+
| id | name |
±--------------------±-----+
| 2017612633061982209 | tr |
±--------------------±-----+
1 row in set (0.003 sec)
-
Use show create table to get the definition
MySQL [raydb]> show create table testrb;
±-------±------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------+
| Table | Create Table |
±-------±------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------+
| testrb | CREATE TABLEtestrb
(
id
bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,
name
varchar(2) DEFAULT NULL,
PRIMARY KEY (id
) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
±-------±------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------+
1 row in set (0.005 sec) -
Then only change the table name, copy the definition, and manually execute
MySQL [raydb]> CREATE TABLEtestrb2
(
→id
bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,
→name
varchar(2) DEFAULT NULL,
→ PRIMARY KEY (id
) /*T![clustered_index] CLUSTERED */
→ ) ;
Query OK, 0 rows affected (0.122 sec) -
Execute the same insert with only the table name different
MySQL [raydb]> insert into testrb2(name) values(‘tr’);
ERROR 1364 (HY000): Field ‘id’ doesn’t have a default value -
Of course, specifying the ID value is fine, but it loses the meaning of auto_random
MySQL [raydb]> insert into testrb2(id, name) values(‘1221321’, ‘tr’);
Query OK, 1 row affected (0.011 sec)
MySQL [raydb]> select * from testrb2;
±--------±-----+
| id | name |
±--------±-----+
| 1221321 | tr |
±--------±-----+
1 row in set (0.004 sec)
If the show create table output is not equivalent to manual creation, then it loses its meaning. Is there any issue with the AUTO_RANDOM situation?
【Attachments】
- TiUP Cluster Display Information
- TiUP Cluster Edit Config Information
- TiDB- Overview Monitoring