MySQL default DATETIME field with an offset - mysql

When CREATE TABLE , how to set MySQL DATETIME field with an default offset??
In oracle, I can do something like:
PWDEXPIRETIME" DATE DEFAULT SYSDATE+30 NOT NULL ENABLE
But it gives me an error when do this in MYSQL:
PWDEXPIRETIME` DATETIME DEFAULT CURRENT_TIMESTAMP+30 NOT NULL

If you are using MySQL versions earlier than 8+, then the most you can do is to specify CURRENT_TIMESTAMP, with no offset, as the default value.
In MySQL 8+, you may use INTERVAL syntax to get what you want:
CREATE TABLE yourTable (
PWDEXPIRETIME DATETIME DEFAULT (CURRENT_TIMESTAMP + INTERVAL 30 MINUTE),
...
)

Related

MySQL (or Mariadb) Expression with DATEDIFF and DATETIME() OR NOW()

What is wrong here?
CREATE TABLE `actionAngebot` (
`createdAt` DATETIME NOT NULL,
`expiryDat` SMALLINT DEFAULT UNSIGNED AS (DATEDIFF(`createdAt`, DATETIME())) STORED
)
ENGINE=MyISAM;
SQL Error (1064): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '() )) STORED ) ENGINE=MyISAM' at line 3 *
please do not propose me this :
CREATE TABLE `promos` (
`createdAt` DATETIME NOT NULL,
`createdTil` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`expiryDateOfReward` SMALLINT(10) AS ((to_days(`createdAt`) - to_days(`createdTil`))) VIRTUAL
)
ENGINE=MyISAM;
I want to make it with 2 columns.
The problem with your current query is that DATETIME is a data type, not a function, so you can't call it as one. DATETIME is not valid in that position either, you would need to use NOW() or CURDATE() dependent on whether you wanted a difference to the date and time or just date.
But
You can't use expressions for generated columns which include functions based on system date and time, so you cannot do what you want (see the manual). You could consider creating a view instead:
CREATE VIEW aabot AS
SELECT *, DATEDIFF(`createdAt`, NOW()) AS expiryDat
FROM actionAngebot

Unable to set current timestamp as default

I am trying to set CURRENT_TIMESTAMP as default value for my DATE type in phpmyadmin. I first tried to set DATE type, and then to DATETIME type, but i'm getting this error:
#1067 - Invalid default value for 'registered_at'
So, how can I set current timestamp as default value ?
You need to use MySQL Server 5.6.5 or later to do this. You also need to use the TIMESTAMP or DATETIME data types. It won't work with DATE.
The example in the manual shows both TIMESTAMP and DATETIME:
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
dt DATETIME DEFAULT CURRENT_TIMESTAMP
);
If you have an earlier version of MySQL Server, you need to use the TIMESTAMP data type only. It won't work with DATETIME.
Read https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html for more information.

What is the reason why MySQL is telling me "Invalid default value for 'postdate'"?

I have 2 Windows servers running MySQL, one of them running version 5.1 and the other running version 5.7. I am trying trying to copy a database from the MySQL 5.7 over to the 5.1 on the other server and believe it's the difference in versions (new syntax in 5.7?) that is causing this error, but I could be wrong.
After Exporting through phpMyAdmin the database I in the 5.7 version and trying to Import in the 5.1 version I'm getting the error
MySQL said: Documentation #1067 - Invalid default value for
'postdate'
on the command
CREATE TABLE IF NOT EXISTS `jobs` (
`id` mediumint( 9 ) NOT NULL ,
`title` varchar( 200 ) DEFAULT NULL ,
`descr` varchar( 5000 ) DEFAULT NULL ,
`postdate` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB AUTO_INCREMENT =5 DEFAULT CHARSET = utf8mb4;
Any idea why?
In 5.1, the default value has to be a constant value (e.g. NULL is acceptable) except for the timestamp type where current_timestamp is allowed. I.e., for a date or datetime you cannot use current_date, now or current_timestamp.
So you either stick to the datetime type for your postdate column and you have to give up current_timestamp as a default value (you can maybe set up a trigger for the purpose, see examples here), or - depending on your requirements - consider using timestamp (which has a different range of values).
The corresponding section of the manual says:
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.

MySQL datetime default time with interval

Is it possible to add to a default time with NOW(), 10 minutes?
I've tried something like that:
CREATE TABLE `table1` (
`date` DATETIME NOT NULL DEFAULT DATE_ADD(NOW(), INTERVAL 10 MINUTE)
);
However, it doesn't work.
I dont think you can do this.
The MySQL Documentation states that:
The DEFAULT value clause in a data type specification indicates a
default value for a column. 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
You could however use an insert-trigger to accomplish this. Set the default for the 'date' column to null, and use
CREATE TRIGGER settime
BEFORE INSERT on table1
FOR EACH ROW BEGIN
IF new.`date` is null THEN
SET new.`date` = DATE_ADD(NOW(), INTERVAL 10 MINUTE);
END IF;
END;
As of MySQL 8.0.13, you can now achieve this by wrapping the default expression in parentheses:
CREATE TABLE IF NOT EXISTS `datetime_interval_example`
(
...
`later` DATETIME NOT NULL DEFAULT (CURRENT_TIMESTAMP + INTERVAL 10 MINUTE),
...
);
The link posted by #DavidSteele in their answer demonstrates this as well.

How to set default value to sysdate in MySql?

I am going to create a table with a column storing Created_date which is of datetime datatype. And my aim is to set its default value as sysdate().
I tried
CREATE TABLE tbl_table (
created_date datetime DEFAULT sysdate())
This gives me error saying not a valid default statement. I used similar logic in Oracle.
Please help me to resolve this.
Thanks in advance.
Try
CREATE TABLE tbl_table ( created_date TIMESTAMP DEFAULT NOW())
But: NOW is different from sysdate and TIMESTAMP is different from datetime, keep this in mind.
Normaly you only can use constants for default-values. TIMESTAMP is the only column-type which supports a function like NOW(). See here for further information on the MySQL Bugtracker.
CREATE TABLE tbl_table(
created_datetime DATETIME DEFAULT CURRENT_TIMESTAMP,
modified_datetime DATETIME ON UPDATE CURRENT_TIMESTAMP
)
Should do the trick.