Using date function for SQL - mysql

I am having issues using the date functions in sql workbench.
CREATE TABLE exampletable (
exampledate DATE DEFAULT CURDATE() not null,
);
I've tried CURDATE(), NOW(), CURRENT_DATE, but it keeps saying you have an error in your sql syntax and underlines whatever date function I use. Any ideas on what my issue is?

According to MySQL doc at http://dev.mysql.com/doc/refman/5.6/en/create-table.html:
The DEFAULT clause specifies 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 or (as of MySQL
5.6.5) DATETIME column. See Section 11.3.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.

Related

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.

MySQL DATE field with default CURDATE(). NOT DATETIME

It is possible to set default value on DATE (NOT DATETIME) column in MySQL 5.7 to current date?
I try this (generated by Workbench):
ALTER TABLE `db`.`table` CHANGE COLUMN `column` `column` DATE NOT NULL DEFAULT CURDATE() ;
but not works for me.
(no data in table)
No, you cannot. The documentation is pretty clear on this:
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 TIMESTAMP and DATETIME columns. See Section 12.3.5, “Automatic
Initialization and Updating for TIMESTAMP and DATETIME”.
You can do one of the following:
Set up a column with a default value for the DATETIME. Create view that extracts the date as a separate column.
Create an insert trigger to set the date column.
There is a way you can do this if you have another column that has a for example a datetime field with a default of NOW(). See this post:

Create a column 'Date' with default value current datetime MYSQL

Following is my SQL query, it throws an error:-
CREATE TABLE IF NOT EXISTS USER_PROFILE(Id INT PRIMARY KEY AUTO_INCREMENT, date DATETIME NOT NULL DEFAULT NOW) ;
It says Invalid default value for 'date'.
I've tried synonyms for NOW() as well, namely CURRENT_TIMESTAMP, but still the same error.
How can I create a column date with default value current time?
On the documentation page, it says to assign this way
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
dt DATETIME DEFAULT CURRENT_TIMESTAMP
);
From the document
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 TIMESTAMP
and DATETIME columns
So no function is allowed in the default value hence the first query is failing.
Again from the document
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and
for at most one TIMESTAMP column per table. The following notes first
describe automatic initialization and updating for MySQL 5.6.5 and up,
then the differences for versions preceding 5.6.5.
Before 5.6.5, this is true only for TIMESTAMP
So your mysql version is less than 5.6.5 hence the 2nd query is failing too.
So you need to create the table as
CREATE TABLE IF NOT EXISTS
USER_PROFILE
(
Id INT PRIMARY KEY AUTO_INCREMENT,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ;
It might be that DATE, as a reserved word, is confusing it by the time it gets to the DEFAULT clause. Try a different name and if that works, try quoting "date".

Setting default value for DATE type column to current date without time part?

NOTE: The question is about DATE type, not Datetime nor Timestamp
How to alter column of date data type to use current date by default?
I saw a lot of examples for datetime (with time part), but not for date. I have tried:
ALTER TABLE `accounting` ALTER `accounting_date`
SET DEFAULT CURRENT_DATE;
ALTER TABLE `accounting` CHANGE `accounting_date`
`accounting_date` DATE NOT NULL DEFAULT CURRENT_DATE;
I also tried with CURDATE(), NOW(), CURRENT_DATE() ...
MySQL 8.0+:
CREATE TABLE foo (
`creation_time` DATE DEFAULT (DATE_FORMAT(NOW(), '%Y-%m-%d'))
)
I use version 8.0.26, this is working:
datecolumn date DEFAULT (CURDATE())
It does not work, if you don't use the brackets!
Probably you cannot set default value for 'date' data type in mysql. You need to change the type to timestamp or datetime.
You may have a look at this similar question.
Invalid default value for 'Date'
EDIT:
In version 5.6.5, it is possible to set a default value on a datetime column, and even make a column that will update when the row is updated. The type definition:
CREATE TABLE foo (
`creation_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
`modification_time` DATETIME ON UPDATE CURRENT_TIMESTAMP
)
Reference: http://optimize-this.blogspot.com/2012/04/datetime-default-now-finally-available.html
As noted in this question Invalid default value for 'create_date' timestamp field, this error may happen when MySQL is in strict mode (which is default behavior, I believe).
If you want to override it, just disable all these checks when creating your table:
SET SQL_MODE='ALLOW_INVALID_DATES';
The warning will be still generated, however it will allow to create the table.
It seems to work in sqlite:
"date" DATE NOT NULL DEFAULT (DATE(CURRENT_TIMESTAMP))
No, you cannot. The documentation is pretty clear on this:
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 TIMESTAMP and DATETIME columns.

Altering Mysql column default value to CURRENT_DATE gives error?

ALTER TABLE `mysystem`.`projects`
MODIFY COLUMN `project_capture_date` DATE NOT NULL DEFAULT CURRENT_DATE();
gives:
Error Code: 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 'CURRENT_DATE()'
at line 2
Current row definition:
project_capture_date, date, NO, , 0000-00-00
Just recently changed the engine to InnoDB from MyISAM.
As per the mysql document
http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
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
To solve this you may need to define the datatype as TIMESTAMP column with DEFAULT CURRENT_TIMESTAMP
DEMO
If you do not want TIMESTAMP then you have do while inserting by setting the column value as Now()