HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to set addtime(current_timestamp, '15:00:00') as MySQL timestamp column default value?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
timestampcolumncurrent_timestampvaluemysqldefaulthowaddtimeset

Problem

I want to set addtime(current_timestamp, '15:00:00') as the default value of one MySQL table column. I tried to do this with the following command but failed:

mysql> alter table mytable change c1 c2 timestamp null default addtime(current_timestamp, '15:00:00');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'addtime(current_timestamp, '15:00:00')' at line 1


How to do this? Thanks.

Solution

MySQL does not support functions in column default values. CURRENT_TIMESTAMP is the closest thing you'll find to an exception:


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 column.

http://dev.mysql.com/doc/refman/5.5/en/create-table.html#id850368

Context

StackExchange Database Administrators Q#5331, answer score: 2

Revisions (0)

No revisions yet.