MySQL 建表自动添加当前系统时间
MySQL在建表的同时怎样才能写入一个函数,当添加数据或修改数据的同时,自动将当前系统时间添加到数据库对应字段的记录中?...
MySQL 在建表的同时 怎样才能写入一个函数,当添加数据或修改数据的同时,自动将当前系统时间添加到数据库对应字段的记录中?
展开
展开全部
mysql> create table timetest(
-> id int not null,
-> modtime timestamp default current_timestamp on update current_timestamp,
-> primary key(id)
-> )engine=innodb,default charset=utf8;
Query OK, 0 rows affected (0.06 sec)
以下是测试,包括建表时记录时间,插入时记录时间,更新时记录时间:
mysql> insert into timetest(id) values (1),(2),(3);
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from timetest;
+----+---------------------+
| id | modtime |
+----+---------------------+
| 1 | 2011-09-21 09:56:24 |
| 2 | 2011-09-21 09:56:24 |
| 3 | 2011-09-21 09:56:24 |
+----+---------------------+
3 rows in set (0.00 sec)
mysql> update timetest set id=10 where id=1;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from timetest;
+----+---------------------+
| id | modtime |
+----+---------------------+
| 2 | 2011-09-21 09:56:24 |
| 3 | 2011-09-21 09:56:24 |
| 10 | 2011-09-21 09:57:15 |
+----+---------------------+
3 rows in set (0.00 sec)
mysql> update timetest set id=4 where id=3;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from timetest;
+----+---------------------+
| id | modtime |
+----+---------------------+
| 2 | 2011-09-21 09:56:24 |
| 4 | 2011-09-21 09:58:10 |
| 10 | 2011-09-21 09:57:15 |
+----+---------------------+
3 rows in set (0.00 sec)
mysql>
-> id int not null,
-> modtime timestamp default current_timestamp on update current_timestamp,
-> primary key(id)
-> )engine=innodb,default charset=utf8;
Query OK, 0 rows affected (0.06 sec)
以下是测试,包括建表时记录时间,插入时记录时间,更新时记录时间:
mysql> insert into timetest(id) values (1),(2),(3);
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from timetest;
+----+---------------------+
| id | modtime |
+----+---------------------+
| 1 | 2011-09-21 09:56:24 |
| 2 | 2011-09-21 09:56:24 |
| 3 | 2011-09-21 09:56:24 |
+----+---------------------+
3 rows in set (0.00 sec)
mysql> update timetest set id=10 where id=1;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from timetest;
+----+---------------------+
| id | modtime |
+----+---------------------+
| 2 | 2011-09-21 09:56:24 |
| 3 | 2011-09-21 09:56:24 |
| 10 | 2011-09-21 09:57:15 |
+----+---------------------+
3 rows in set (0.00 sec)
mysql> update timetest set id=4 where id=3;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from timetest;
+----+---------------------+
| id | modtime |
+----+---------------------+
| 2 | 2011-09-21 09:56:24 |
| 4 | 2011-09-21 09:58:10 |
| 10 | 2011-09-21 09:57:15 |
+----+---------------------+
3 rows in set (0.00 sec)
mysql>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
CREATE TABLE `test`.`tabletest` (
`id` INT NOT NULL ,
`d` TIMESTAMP NOT NULL DEFAULT current_timestamp ,
PRIMARY KEY (`id`) );
insert into test.tabletest (idtest.tabletest)values(1);
SELECT * FROM `test`.`tabletest`;
1 2011-09-20 20:49:51
`id` INT NOT NULL ,
`d` TIMESTAMP NOT NULL DEFAULT current_timestamp ,
PRIMARY KEY (`id`) );
insert into test.tabletest (idtest.tabletest)values(1);
SELECT * FROM `test`.`tabletest`;
1 2011-09-20 20:49:51
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
默认为当前时间可以,不过只有TIMESTAMP类型可以alter
table
tablename
modify
field
timestamp
not
null
default
current_timestamp;
table
tablename
modify
field
timestamp
not
null
default
current_timestamp;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
大致应该是:
把该字段的默认值设为 当前系统时间 的 函数
把该字段的默认值设为 当前系统时间 的 函数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询