Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Auto-Increment by DateTime

Is there a way in my sql to auto increment by the date and time?

so say I have a table

                         mytable
 =====================================================
 = Comment_ID = First_Name =  Comment  =  DateTime   =
 =====================================================
 =     1     =      Bob   =  FooBar = 11-01-14 11:00 =
 =     2     =     Jack   =  Blah   = 11-01-14 12:29 =
 =     3     =     John   =  jonny  = 12-01-14 07:09 =
 =====================================================

Is there away to make the date auto-increment?

like image 651
RixsonL Avatar asked Nov 15 '25 18:11

RixsonL


1 Answers

Run this in your MySQL:

ALTER TABLE `mytable` 
CHANGE COLUMN `DateTime` `DateTime` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ;

This sets your default value to CURRENT_TIMESTAMP.

like image 76
Tzar Avatar answered Nov 18 '25 11:11

Tzar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!