Mysql default CURRENT_TIMESTAMP & on update CURRENT_TIMESTAMP区别

public @interface Column {

    /**
     * (Optional) The name of the column. Defaults to 
     * the property or field name.
     */
    String name() default "";

    /**
     * (Optional) Whether the column is a unique key.  This is a 
     * shortcut for the <code>UniqueConstraint</code> annotation at the table 
     * level and is useful for when the unique key constraint 
     * corresponds to only a single column. This constraint applies 
     * in addition to any constraint entailed by primary key mapping and 
     * to constraints specified at the table level.
     */
    boolean unique() default false;

    /**
     * (Optional) Whether the database column is nullable.
     */
    boolean nullable() default true;

    /**
     * (Optional) Whether the column is included in SQL INSERT 
     * statements generated by the persistence provider.
     */
    boolean insertable() default true;

    /**
     * (Optional) Whether the column is included in SQL UPDATE 
     * statements generated by the persistence provider.
     */
    boolean updatable() default true;

    /**
     * (Optional) The SQL fragment that is used when 
     * generating the DDL for the column.
     * <p> Defaults to the generated SQL to create a
     * column of the inferred type.
     */
    String columnDefinition() default "";

    /**
     * (Optional) The name of the table that contains the column. 
     * If absent the column is assumed to be in the primary table.
     */
    String table() default "";

    /**
     * (Optional) The column length. (Applies only if a
     * string-valued column is used.)
     */
    int length() default 255;

    /**
     * (Optional) The precision for a decimal (exact numeric) 
     * column. (Applies only if a decimal column is used.)
     * Value must be set by developer if used when generating 
     * the DDL for the column.
     */
    int precision() default 0;

    /**
     * (Optional) The scale for a decimal (exact numeric) column. 
     * (Applies only if a decimal column is used.)
     */
    int scale() default 0;
}

两条sql语句

creat_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',

即:
数据创建时间用 CURRENT_TIMESTAMP ---insert此条数据的时间(之后就不变了)
数据更新时间用 UPDATE CURRENT_TIMESTAMP---update此条数据的时间(数据字段有变更时,会更新为当前时间)

DEFAULT CURRENT_TIMESTAMP 

insert的时候,在用户不给定值的情况下[ jpa @column(insertable=false) ],使用默认值;

注意:即使用户给定的值是null,还是会使用用户插入的值。

insertable为false的意思,就是生成insert语句的时候不包含这个字段,因此值未知了就使用默认值。

ON UPDATE CURRENT_TIMESTAMP

update的使用,在用户不给定值的情况下[ jpa @column(updateable=false) ],使用默认值;

 注意:即使用户给定的值是null,还是会使用用户给定的值。

updateable为false的意思,就是生成update语句的时候不包含这个字段,因此值未知了就使用默认值。

MySQL时区问题

show variables like '%time_zone';

这里有两个time_zone:

system_time_zone: JVM 插入java.util.Date()时默认的time_zone.

time_zone: 又称为MySQL session time_zone,数据库使用timestamp等函数时默认的time_zone. 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值