1

In the server, I have MySQL version:

5.1.61

It has a table called test with 10 columns and 10K rows. Now I have decided to add a new column

ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

New column ts is added. but the problem is for existing rows this new column(ts)'s value is 00:000:000 Not current time.

PS: When I add new rows or update any existing row then the ts is updated with current time stamp.

why is default current_timestamp/now() not working for existing rows?

Edited: I can run a simple update SQL to update existing rows to the current time. But I am new in the database and I am trying to know if it is possible to update existing rows with a default value.

0

1 Answer 1

2

The DEFAULT clause in a MySQL table only concerns what happens when new records get added to the table. There is no legacy behavior where the DBMS goes back to already existing records and applies some default value. In this case, if you want the already existing records to bear the current timestamp, you may do a blanket update:

UPDATE yourTable
SET ts = CURRENT_TIMESTAMP;

After this point, when you add new records and do not specify a value for the ts column, it will be assigned CURRENT_TIMESTAMP.

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

4 Comments

he is asking why the CURRENT_TIMESTAMP is not working to the existing rows while adding a new column
@Ajay Um...my answer explains what is happening and why it is happening. Did you bother to read it?
ooh my mistake I apologize..:)
@TimBiegeleisen thanks for your answer. I was really trying to know if it is possible or not. I thought I made mistakes.

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.