Create default default field

I need to create a timestamp field for a table whose rows should expire after a certain time. If I use the following:

`timestamp` TIMESTAMP DEFAULT NOW(), 

It shows time in a humanoid format, it would be much easier if I could have it in an era so that I could calculate in a matter of seconds. Is there a way to create a field that will display the current time when a row is created by default in the era? Thanks!

+7
sql mysql
source share
1 answer

You can use the UNIX_TIMESTAMP() function in your SELECT , as shown in the following example:

 CREATE TABLE test_tb (`timestamp` TIMESTAMP DEFAULT NOW()); INSERT INTO test_tb VALUES (DEFAULT); SELECT UNIX_TIMESTAMP(`timestamp`) epoch, `timestamp` FROM test_tb; +------------+---------------------+ | epoch | timestamp | +------------+---------------------+ | 1281834891 | 2010-08-15 03:14:51 | +------------+---------------------+ 1 row in set (0.00 sec) 
+6
source share

All Articles