The way MySQL implements the TIMESTAMP data type, it is actually storing the epoch time in the database. So you could just use a TIMESTAMP column with a default of CURRENT_TIMESTAMP and apply the UNIX_TIMESTAMP() to it if you want to display it as an int:

CREATE TABLE foo(
  created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

insert into foo values (current_Date()),(now());

select unix_timestamp(created) from foo;
+-------------------------+
| unix_timestamp(created) |
+-------------------------+
|              1300248000 |
|              1300306959 |
+-------------------------+
2 rows in set (0.00 sec)

However, if you really want the datatype of the column to be INT, you can use R. Bemrose's suggestion and set it via trigger:

CREATE TABLE foo(
  created INT NULL
);

delimiter $$

create trigger tr_b_ins_foo before insert on foo for each row
begin
  if (new.created is null)
  then
    set new.created = unix_timestamp();
  end if;
end $$

delimiter ;


insert into foo values (unix_timestamp(current_Date())), (null);

select created from foo;
+------------+
| created    |
+------------+
| 1300248000 |
| 1300306995 |
+------------+
2 rows in set (0.00 sec)
Answer from Ike Walker on Stack Overflow
Top answer
1 of 6
67

The way MySQL implements the TIMESTAMP data type, it is actually storing the epoch time in the database. So you could just use a TIMESTAMP column with a default of CURRENT_TIMESTAMP and apply the UNIX_TIMESTAMP() to it if you want to display it as an int:

CREATE TABLE foo(
  created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

insert into foo values (current_Date()),(now());

select unix_timestamp(created) from foo;
+-------------------------+
| unix_timestamp(created) |
+-------------------------+
|              1300248000 |
|              1300306959 |
+-------------------------+
2 rows in set (0.00 sec)

However, if you really want the datatype of the column to be INT, you can use R. Bemrose's suggestion and set it via trigger:

CREATE TABLE foo(
  created INT NULL
);

delimiter $$

create trigger tr_b_ins_foo before insert on foo for each row
begin
  if (new.created is null)
  then
    set new.created = unix_timestamp();
  end if;
end $$

delimiter ;


insert into foo values (unix_timestamp(current_Date())), (null);

select created from foo;
+------------+
| created    |
+------------+
| 1300248000 |
| 1300306995 |
+------------+
2 rows in set (0.00 sec)
2 of 6
20

From the documentation:

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 a TIMESTAMP column.

🌐
w3resource
w3resource.com › mysql › date-and-time-functions › mysql-unix_timestamp-function.php
MySQL UNIX_TIMESTAMP() function - w3resource
2 weeks ago - The following statement will return the unix timestamp in seconds as an unsigned integer since '1970-01-01 00:00:00' UTC for the specified datetime 1970-01-01 12:00:00. ... mysql> SELECT UNIX_TIMESTAMP('1970-01-01 12:00:00'); +-------------...
Discussions

Cannot use default with `ON UPDATE UNIX_TIMESTAMP()`
Then use unix_timestamp() if you need the timestamp integer: ... There was an error while loading. Please reload this page. Something went wrong. There was an error while loading. Please reload this page. ... You can use ON UPDATE, but I believe that DDL will throw the same error in plain MySQL 8. More on github.com
🌐 github.com
1
1
unix timestamp - How to set default value of a mysql column as unix_timestamp? - Stack Overflow
I was thinking at something like this: `post_modified` int(11) NOT NULL DEFAULT UNIX_TIMESTAMP (NOW ()) ON UPDATE UNIX_TIMESTAMP (NOW ()), But this code it is not working. More on stackoverflow.com
🌐 stackoverflow.com
April 4, 2017
MYSQL Auto Create Unix Timestamp
Create this new column and for ... to unix and apply to this new column as well? ... Create the column. Then run an update query like: ... Results = 20k or more Rows and index doesnt speed it up. you have now posted three different threads about this · the optimizer isn't going to use an index because the number of rows that satisfy the condition is too high -- it's cheaper to jsut do a table scan ... I have tried converting to timestamp but MYSQL doesnt allow ... More on reddit.com
🌐 r/mysql
12
1
September 7, 2022
mysql - How to store unix timestamp as int with default and on update? - Stack Overflow
I have been updating my MySQL tables with the following: ALTER TABLE logs ADD COLUMN updateTimeStamp timestamp DEFAULT current_timestamp() ON UPDATE current_timestamp; This stores the timestamp in... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Narkive
mysql.mysql.narkive.com › 07CdHcbp › using-unix-timestamp-as-default-field-value
Using UNIX_TIMESTAMP() as default field value ?
Permalink Currently I am using UNIX_TIMESTAMP() in my INSERTS, just the idea came to me that maybe I could use a MySQL function as a default for a field. Too bad MySQL doesn't allow it though...... Thanks for your reply though =) Oh and yeah, I knew the int(10) was for the display length ;p -- Keith Bussey Mana Internet Solutions, Inc.
🌐
MySQL
dev.mysql.com › doc › en › date-and-time-functions.html
MySQL :: MySQL 8.4 Reference Manual :: 14.7 Date and Time Functions
The valid range of argument values is the same as for the TIMESTAMP data type: '1970-01-01 00:00:01.000000' UTC to '2038-01-19 03:14:07.999999' UTC for 32-bit platforms; for MySQL running on 64-bit platforms, the valid range of argument values for UNIX_TIMESTAMP() is '1970-01-01 00:00:01.000000' ...
🌐
Reddit
reddit.com › r/mysql › mysql auto create unix timestamp
r/mysql on Reddit: MYSQL Auto Create Unix Timestamp
September 7, 2022 - I figure if I cant use index on these large data sources then make datetime something more readable like Unix_time ... You have to get the parentheses and data types right. CREATE TABLE t (ts TIMESTAMP DEFAULT UNIX_TIMESTAMP()); is wrong for two different reasons.
Find elsewhere
🌐
Epoch Converter
epochconverter.com › epoch converter › programming a-z › using unix timestamps in mysql mini-course
Using Unix Timestamps in MySQL Mini-Course - Epoch Converter
INSERT INTO mytable VALUES(1,'pagename',UNIX_TIMESTAMP(now())) ... There's one big problem with MySQL: MySQL cannot convert negative epoch timestamps (dates before 1-1-1970). This creates problems with for example birthdates.
🌐
MySQL
dev.mysql.com › doc › refman › 8.0 › en › timestamp-initialization.html
MySQL :: MySQL 8.0 Reference Manual :: 13.2.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME
May 15, 2022 - To specify automatic properties, use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses in column definitions. The order of the clauses does not matter. If both are present in a column definition, either can occur first. Any of the synonyms for CURRENT_TIMESTAMP have the same meaning as CURRENT_TIMESTAMP.
🌐
NUST
download.nust.na › pub6 › mysql › doc › refman › 5.4 › en › timestamp.html
MySQL :: MySQL 5.4 Reference Manual :: 10.3.1.1 TIMESTAMP Properties
In any system, hand coded, phpmyadmin, mysql query browser or otherwise : `created` timestamp NOT NULL default '0000-00-00 00:00:00', `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update NOW(), although, phpmyadmin exports this out without the NOW, replacing it with CURRENT_TIMESTAMP ...
🌐
Reddit
reddit.com › r/mysql › correct column type for a unix timestamp with microseconds
r/mysql on Reddit: Correct Column Type For a Unix Timestamp with Microseconds
August 2, 2021 -

Hi,

Currently I have a column of type char that stores unix time with microseconds as follows:

 `last_update_utime` char(18) COLLATE utf8mb4_unicode_ci DEFAULT NULL 
  COMMENT 'Unix Time of Last Update with mseconds'

I update that column as below;

UPDATE `transaction_tbl` 

SET    `transaction_token` = '$token',
       `last_update_utime` = UNIX_TIMESTAMP(LOCALTIMESTAMP(6))

WHERE  `user_id` = '$userID'

I use the field last_update_utime to address concurrency.
What is the correct column type for unix timestamps with microseconds ?

i have seen posts on the web stating int(11) or bigint(20)
but the issue of microseconds is not addressed ?
Is keeping the column type to 'char' sufficient or better to use something like decimal(18,6) ?

Pls advise.

🌐
Scaler
scaler.com › home › topics › mysql unix_timestamp() function
MySQL UNIX_TIMESTAMP() Function - Scaler Topics
July 17, 2023 - MySQL UNIX_TIMESTAMP() returns a Unix timestamp in seconds since '1970-01-01 00:00:00' UTC as an unsigned integer if no arguments are passed with UNIX_TIMESTAMP().
🌐
DataCamp
datacamp.com › doc › mysql › mysql-unix-timestamp
MySQL UNIX_TIMESTAMP() Function: Usage & Examples
The `UNIX_TIMESTAMP()` function in MySQL returns the current Unix timestamp, which is the number of seconds that have elapsed since '1970-01-01 00:00:00' UTC.
🌐
MariaDB
mariadb.com › kb › en › unix_timestamp
UNIX_TIMESTAMP | Server | MariaDB Documentation
If called with no argument, returns a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC) as an unsigned integer. If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since 1970-01-01 00:00:00 ...
🌐
MySQL
dev.mysql.com › doc › refman › 5.7 › en › timestamp-initialization.html
MySQL :: MySQL 5.7 Reference Manual :: 11.2.6 Automatic Initialization and Updating for TIMESTAMP and DATETIME
June 11, 2019 - To specify automatic properties, use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses in column definitions. The order of the clauses does not matter. If both are present in a column definition, either can occur first. Any of the synonyms for CURRENT_TIMESTAMP have the same meaning as CURRENT_TIMESTAMP.
🌐
MySQL
dev.mysql.com › doc › refman › 8.0 › en › date-and-time-functions.html
MySQL :: MySQL 8.0 Reference Manual :: 14.7 Date and Time Functions
January 18, 2022 - For MySQL 8.0.28 and later running on 64-bit platforms, the valid range of argument values for UNIX_TIMESTAMP() is '1970-01-01 00:00:01.000000' UTC to '3001-01-19 03:14:07.999999' UTC (corresponding to 32536771199.999999 seconds). Regardless of MySQL version or platform architecture, if you ...