MySQL 中,时间默认值问题
以下两个语句:register_timedatatimenotnulldefaultNOW()register_timetimestampnotnulldefaultNO...
以下两个语句:
register_time datatime not null default NOW()
register_time timestamp not null default NOW()
在MYSQL中,为什么第一个语句有语法错误,第二个却可以呢? 展开
register_time datatime not null default NOW()
register_time timestamp not null default NOW()
在MYSQL中,为什么第一个语句有语法错误,第二个却可以呢? 展开
2个回答
2013-04-14
展开全部
MySQL 中,默认值无法使用函数
也就是你无法 设置某一列,默认值是 NOW () 这样的处理
假如需要 某列的默认值为 当前数据库时间,那么可以使用 TIMESTAMP 数据类型。插入的时候,忽略该列 即可。
dt TIMESTAMP
等价于
dt TIMESTAMP default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
create table testB (
id int PRIMARY KEY,
val varchar(10),
dt TIMESTAMP
);
INSERT INTO testB(id, val) VALUES(1, 'A');
INSERT INTO testB(id, val) VALUES(2, 'B');
SELECT
*
FROM
testB;
+----+------+---------------------+
| id | val | dt |
+----+------+---------------------+
| 1 | A | 2013-03-21 14:24:20 |
| 2 | B | 2013-03-21 14:24:21 |
+----+------+---------------------+
2 rows in set (0.00 sec)
UPDATE
testB
SET
val = 'C'
WHERE
id = 1;
SELECT
*
FROM
testB;
+----+------+---------------------+
| id | val | dt |
+----+------+---------------------+
| 1 | C | 2013-03-21 14:28:02 |
| 2 | B | 2013-03-21 14:24:21 |
+----+------+---------------------+
2 rows in set (0.00 sec)
假如仅仅需要 插入时记录时间, 更新的时候不需要, 那么需要修改表定义方式:
TIMESTAMP not null default CURRENT_TIMESTAMP
create table testC (
id int PRIMARY KEY,
val varchar(10),
dt TIMESTAMP not null default CURRENT_TIMESTAMP
);
INSERT INTO testC(id, val) VALUES(1, 'A');
INSERT INTO testC(id, val) VALUES(2, 'B');
SELECT
*
FROM
testC;
+----+------+---------------------+
| id | val | dt |
+----+------+---------------------+
| 1 | A | 2013-03-21 14:35:12 |
| 2 | B | 2013-03-21 14:35:13 |
+----+------+---------------------+
2 rows in set (0.00 sec)
UPDATE
testC
SET
val = 'CC'
WHERE
id = 1;
SELECT
*
FROM
testC;
+----+------+---------------------+
| id | val | dt |
+----+------+---------------------+
| 1 | CC | 2013-03-21 14:35:12 |
| 2 | B | 2013-03-21 14:35:13 |
+----+------+---------------------+
2 rows in set (0.00 sec)
也就是你无法 设置某一列,默认值是 NOW () 这样的处理
假如需要 某列的默认值为 当前数据库时间,那么可以使用 TIMESTAMP 数据类型。插入的时候,忽略该列 即可。
dt TIMESTAMP
等价于
dt TIMESTAMP default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
create table testB (
id int PRIMARY KEY,
val varchar(10),
dt TIMESTAMP
);
INSERT INTO testB(id, val) VALUES(1, 'A');
INSERT INTO testB(id, val) VALUES(2, 'B');
SELECT
*
FROM
testB;
+----+------+---------------------+
| id | val | dt |
+----+------+---------------------+
| 1 | A | 2013-03-21 14:24:20 |
| 2 | B | 2013-03-21 14:24:21 |
+----+------+---------------------+
2 rows in set (0.00 sec)
UPDATE
testB
SET
val = 'C'
WHERE
id = 1;
SELECT
*
FROM
testB;
+----+------+---------------------+
| id | val | dt |
+----+------+---------------------+
| 1 | C | 2013-03-21 14:28:02 |
| 2 | B | 2013-03-21 14:24:21 |
+----+------+---------------------+
2 rows in set (0.00 sec)
假如仅仅需要 插入时记录时间, 更新的时候不需要, 那么需要修改表定义方式:
TIMESTAMP not null default CURRENT_TIMESTAMP
create table testC (
id int PRIMARY KEY,
val varchar(10),
dt TIMESTAMP not null default CURRENT_TIMESTAMP
);
INSERT INTO testC(id, val) VALUES(1, 'A');
INSERT INTO testC(id, val) VALUES(2, 'B');
SELECT
*
FROM
testC;
+----+------+---------------------+
| id | val | dt |
+----+------+---------------------+
| 1 | A | 2013-03-21 14:35:12 |
| 2 | B | 2013-03-21 14:35:13 |
+----+------+---------------------+
2 rows in set (0.00 sec)
UPDATE
testC
SET
val = 'CC'
WHERE
id = 1;
SELECT
*
FROM
testC;
+----+------+---------------------+
| id | val | dt |
+----+------+---------------------+
| 1 | CC | 2013-03-21 14:35:12 |
| 2 | B | 2013-03-21 14:35:13 |
+----+------+---------------------+
2 rows in set (0.00 sec)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询