如何在同一个表中设置多个字段的默认值为当前时间
如下语句:
CREATE TABLE users(
joindate date NOT NULL   DEFAULT now(),
lastvisit timestamp NOT NULL   DEFAULT now(),
lastactivity timestamp NOT NULL   DEFAULT now(),
lastpost timestamp NOT NULL DEFAULT now()
)
该语句在mysql中无法执行

解决方案 »

  1.   


    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   ) ;
      

  2.   

    试过了,还是不行。出错信息为:Incorrect table definition:there can be only TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
      

  3.   

    代码如下:
    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

    )
    帮忙看看
      

  4.   


    这样可以吗??在一个表里,只能存在一个CURRENT_TIMESTAMP 吧??
      

  5.   


    没有办法,只允许一个字段设置这个CURRENT_TIMESTAMP的默认值。可以使用触发器实现类似的功能。 
      

  6.   


               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`)  
    )
      

  7.   

    to :ACMAIN_CHM你在1楼给的例子,能正常运行吗??