Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT

简介: Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT

有时我们执行数据库的sql语句时,可能会遇到下面的错误

错误代码: 1293

Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause


错误代码: 1067

Invalid default value for 'createtime’


1.我们想要执行的sql语句:


/*==============================================================*/
/* Table: user                                                  */
/*==============================================================*/
CREATE TABLE USER
(
   user_id              INT NOT NULL AUTO_INCREMENT COMMENT '医生id',
   username             VARCHAR(100) COMMENT '用户名',
   PASSWORD             VARCHAR(100) COMMENT '密码',
   realname             VARCHAR(100) COMMENT '真实姓名',
   telephone            VARCHAR(20) COMMENT '电话号码',
   birthday             DATETIME COMMENT '生日',
   dept_id              INT COMMENT 'id',
   user_type            INT COMMENT '医生类型',
   active               INT DEFAULT 1 COMMENT '是否有效,1 有效,0 失效',
   createtime           DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
   PRIMARY KEY (user_id)
);


2.但是由于mysql的版本不同,可能会出现下面的错误:

错误代码: 1293
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

或者是:

错误代码: 1067
Invalid default value for 'createtime'

通俗的理解就是由于mysql的版本问题我们的那个createtime字段自动生成时间报错了。


3.解决办法:


把createtime字段的类型改为TIMESTAMP,但是其他时间(如birthday,并不需要自动生成的时间)还继续用DATETIME。


修改后可以正确执行的sql语句:

/*==============================================================*/
/* Table: user                                                  */
/*==============================================================*/
CREATE TABLE USER
(
   user_id              INT NOT NULL AUTO_INCREMENT COMMENT '医生id',
   username             VARCHAR(100) COMMENT '用户名',
   PASSWORD             VARCHAR(100) COMMENT '密码',
   realname             VARCHAR(100) COMMENT '真实姓名',
   telephone            VARCHAR(20) COMMENT '电话号码',
   birthday             DATETIME COMMENT '生日',
   dept_id              INT COMMENT 'id',
   user_type            INT COMMENT '医生类型',
   active               INT DEFAULT 1 COMMENT '是否有效,1 有效,0 失效',
   createtime           TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
   PRIMARY KEY (user_id)
);

这样就可以正常执行了,OK问题解决。


相关文章
使用result类一定要添加@Data注解“timestamp“:“2023-09-07T07:57:57.497+00:00““status“: 406,No acceptable repre
使用result类一定要添加@Data注解“timestamp“:“2023-09-07T07:57:57.497+00:00““status“: 406,No acceptable repre
|
关系型数据库 MySQL 数据库
View ‘information_schema.SCHEMATA‘ references invalid table(s) or column(s) or function(s) or define
View ‘information_schema.SCHEMATA‘ references invalid table(s) or column(s) or function(s) or define
224 0
|
数据库
Data truncation: Incorrect date value: ‘2022-11-28T16:00:00.000Z‘ for column ‘start_date‘ at row 1
Data truncation: Incorrect date value: ‘2022-11-28T16:00:00.000Z‘ for column ‘start_date‘ at row 1
263 0
|
数据库
Field ‘id‘ doesn‘t have a default value
Field ‘id‘ doesn‘t have a default value
167 0
|
数据库
Incorrect table definition; there can be only one auto column and it must be defined as a key
Incorrect table definition; there can be only one auto column and it must be defined as a key
177 0
Incorrect table definition; there can be only one auto column and it must be defined as a key
|
SQL 关系型数据库 MySQL
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
731 0
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
|
关系型数据库 MySQL
报错:Incorrect datetime value: '0000-00-00 00:00:00' for column 'login_time' at row 1
报错:Incorrect datetime value: '0000-00-00 00:00:00' for column 'login_time' at row 1
658 0
|
SQL 关系型数据库 MySQL
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
1173 0
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
|
数据库
timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
1572 0
|
关系型数据库 MySQL
1293 - Incorrect table definition; there can be only oneTIMESTAMP column with CURRENT_TIMESTAMP
一个表中出现多个timestamp并设置其中一个为current_timestamp的时候经常会遇到 1293 - Incorrect table definition; there can be only oneTIME...
2475 0