mysql中有一个“注册时间”字段,datetime类型,约束为not null。
我希望再设一个默认值为当前时间。
网上找了很多,都说要转成timestamp类型。
但timestamp类型好象只要修改了该条字段的内容就会自动更改为当前时间(是不是我的理解有错)。
那注册时间肯定不能发生改变呀,请问怎么办?
第一次来,望好心人解答

解决方案 »

  1.   

    是你理解错了mysql> create table testtime(id int,rowdate timestamp default now());
    Query OK, 0 rows affected (0.11 sec)mysql> insert into testtime(id) values(1);
    Query OK, 1 row affected (0.13 sec)mysql> select * from testtime;
    +------+---------------------+
    | id   | rowdate             |
    +------+---------------------+
    |    1 | 2011-05-22 05:20:22 |
    +------+---------------------+
    1 row in set (0.00 sec)mysql> update testtime set id=2;
    Query OK, 1 row affected (0.13 sec)
    Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from testtime;
    +------+---------------------+
    | id   | rowdate             |
    +------+---------------------+
    |    2 | 2011-05-22 05:20:22 |
    +------+---------------------+
    1 row in set (0.00 sec)
      

  2.   

    只要你不加  ON UPDATE CURRENT_TIMESTAMP 就不会在更新的时候改变了。有类似问题的时候,建议楼主首先应该尝试看一下MYSQL官方手册。
      

  3.   

    首先,感谢楼上两位的回答
    我才刚刚开始接触mysql,在理解上很容易出错
    我知道你们的回答很有用,但那么长的一段,实在让我看得有点晕
    这样说吧
    注册时间 timestamp not null default current_timestamp,
    这样写注册时间对吗?在修改和取值时会不会有什么问题?
    回答这个就行,不对的话还请指教
    最好再告诉我用PHP怎么取出这个时间的语句,呵呵.......
      

  4.   

    很简单得动手试一下mysql> create table test4(id int,a timestamp not null default current_timestamp)
    ;
    Query OK, 0 rows affected (0.19 sec)mysql> insert into test4 (id) values(1);
    Query OK, 1 row affected (0.03 sec)mysql> select * from test4;
    +------+---------------------+
    | id   | a                   |
    +------+---------------------+
    |    1 | 2011-05-22 18:02:17 |
    +------+---------------------+
    1 row in set (0.00 sec)mysql> update test4 set id=id+1;
    Query OK, 1 row affected (0.11 sec)
    Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from test4;
    +------+---------------------+
    | id   | a                   |
    +------+---------------------+
    |    2 | 2011-05-22 18:02:17 |
    +------+---------------------+
    1 row in set (0.00 sec)
      

  5.   

    直接在程序里 写死就可以  增加的时候
    productArea.setLastUpdateTime(new Date());
    就是 系统默认时间~~~~~~~~~