The DEFAULT clause specifies 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 columnCREATE TABLE users( joindate DATE , lastvisit TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , lastactivity TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , lastpost TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ) ;
试过了,还是不行。出错信息为:Incorrect table definition:there can be only TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
代码如下: CREATE TABLE users( uid int auto_increment NOT NULL, groupexpiry int NOT NULL DEFAULT '0', extgroupids char(60) NOT NULL DEFAULT '', regip char(15) NOT NULL DEFAULT '', joindate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, lastip char(15) NOT NULL DEFAULT '', lastvisit timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, lastactivity timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, lastpost timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, lastpostid int NOT NULL DEFAULT '0', CONSTRAINT PK_dnt_members PRIMARY KEY CLUSTERED ( uid ASC ) ) 帮忙看看
CREATE TABLE users( uid INT AUTO_INCREMENT NOT NULL, groupexpiry INT NOT NULL DEFAULT '0', extgroupids CHAR(60) NOT NULL DEFAULT '', regip CHAR(15) NOT NULL DEFAULT '', joindate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, lastip CHAR(15) NOT NULL DEFAULT '', lastvisit TIMESTAMP NOT NULL , lastactivity TIMESTAMP NOT NULL , lastpost TIMESTAMP NOT NULL , lastpostid INT NOT NULL DEFAULT '0', CONSTRAINT `PK_dnt_members` PRIMARY KEY(`uid`) )
The DEFAULT clause specifies 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 columnCREATE TABLE users( joindate DATE ,
lastvisit TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
lastactivity TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
lastpost TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ) ;
CREATE TABLE users(
uid int auto_increment NOT NULL,
groupexpiry int NOT NULL DEFAULT '0',
extgroupids char(60) NOT NULL DEFAULT '',
regip char(15) NOT NULL DEFAULT '',
joindate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
lastip char(15) NOT NULL DEFAULT '',
lastvisit timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
lastactivity timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
lastpost timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
lastpostid int NOT NULL DEFAULT '0',
CONSTRAINT PK_dnt_members PRIMARY KEY CLUSTERED
(
uid ASC
)
)
帮忙看看
这样可以吗??在一个表里,只能存在一个CURRENT_TIMESTAMP 吧??
没有办法,只允许一个字段设置这个CURRENT_TIMESTAMP的默认值。可以使用触发器实现类似的功能。
CREATE TABLE users(
uid INT AUTO_INCREMENT NOT NULL,
groupexpiry INT NOT NULL DEFAULT '0',
extgroupids CHAR(60) NOT NULL DEFAULT '',
regip CHAR(15) NOT NULL DEFAULT '',
joindate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
lastip CHAR(15) NOT NULL DEFAULT '',
lastvisit TIMESTAMP NOT NULL ,
lastactivity TIMESTAMP NOT NULL ,
lastpost TIMESTAMP NOT NULL ,
lastpostid INT NOT NULL DEFAULT '0',
CONSTRAINT `PK_dnt_members` PRIMARY KEY(`uid`)
)