1

I have a table

-------------
| id | date |
-------------

Now i need to add column "week", so is it possible to add column like

alter table `a` add column `week` INT(6) not null default DATE_FORMAT(`date`, '%Y%v')

?

2
  • 1
    Why not try it? Then if you have an error or issue then ask the question. Commented Mar 8, 2018 at 14:50
  • 2
    You must use Trigger for this kind of functionality dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html Commented Mar 8, 2018 at 14:52

1 Answer 1

2

The comment above about using a trigger, is the way to go on this but doesn't elaborate on why you can't do it. So, from the documentation ...

The DEFAULT value clause in a data type specification indicates 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 TIMESTAMP and DATETIME columns.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.