---
title: "datetime 设置默认值"
author: "wangsong76"
date: "2011-10-14T10:58:54Z"
tags: []
categories: ["数据库"]
slug: "datetime"
draft: false
lastmod: "2011-10-14T10:58:54Z"
id: 1195733
url: "https://wangsong76.iteye.com/blog/1195733"
---

```sql

create table test
(
   id                   bigint not null,
   entry_time           datetime default now(),
   primary key (id)
);

```

结果：
1067 - Invalid default value for 'entry_time'
原因：
The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column. 

<http://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html>
根据上面的解释，下面这个是对的：

```sql

create table test
(
   id                   bigint not null,
   entry_time           timestamp default CURRENT_TIMESTAMP,
   primary key (id)
);

```