2

i'm new to databases. I have just created my first database, so far so good. Now I looking to add time in to this.

I have certain table, when it's displayed I need it to show the table info + the date that info was added. Also possible the difference in between (5day 5hours ago) or something of the sort.

Do i need to add another column to the data table where the time will be stored? If so is there a way to auto increment that to current date? How do I go about doing that?

Not really sure where to start can someone please point me in the right direction perhaps a tutorial somewhere? Cant seem to find anything solid on this topic

Thanks a bunch!:)

2

2 Answers 2

2

For each record in your DB, you want to know when it has been created?

You have to add a column of type "timestamp". You can then set the default to "current_timestamp". Note also that you can use the "on update current_timestamp". Instead of saving current time on insertion, it will do so each time your record is updated. If you need the 2 dates (created_at and updated_at), you'll need 2 columns. EDIT : well you can't :p

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

3 Comments

Don't forget the underscore in CURRENT_TIMESTAMP. It's required. :)
You also are restricted from to only one timestamp fields with one of the special default values. One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.
Seriously?! Ooops I didn't know this one. Now I understand why Symfony etc don't use it :s @MarcusAdams, corrected too
0

There is a datetime as well as a timestamp field. If you use a timestamp field you can have the field automatically populate with the timestamp of when the row was inserted.

http://dev.mysql.com/doc/refman/5.0/en/datetime.html

http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html

I suggest trying out http://sqlzoo.net/ for a gentle introduction to SQL.

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.