79

Pretty straight forward question here, I think this should work but it doesn't. Why doesn't it?

CREATE TABLE INVOICE(
   INVOICEDATE DATE NOT NULL DEFAULT CURRENT_DATE
)
inControl
  • 2,215
  • 4
  • 24
  • 39
  • 10
    _This Question is being outdated. Newer versions allow such._ See my Answer for version numbers and their release dates. That is, Updating may be the answer for you. – Rick James Nov 21 '18 at 19:11
  • the 5th Answer is the correct one as of October 2021. – t1f Oct 17 '21 at 09:35

10 Answers10

87

[Edit] As of MySQl 8.0.13 it is supported. See this answer below.

It doesn't work because it's not supported

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 column

http://dev.mysql.com/doc/refman/5.5/en/create-table.html

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • @inControl: yep, using `DATE_FORMAT` – zerkms Dec 09 '13 at 00:33
  • 38
    It's 2016 and, honestly, the fact that this still holds true is ridiculous. I thought the whole reason for constant synonyms was to allow them in places non-constant expressions were forbidden. – Dan Lugg Mar 31 '16 at 13:52
  • 12
    @NorthbornDesign I solved it for myself by migrating to postgresql :-) – zerkms Mar 31 '16 at 19:47
  • 1
    please note that the answer from @NorthbornDesign is the correct one :) – Yehosef Jul 07 '16 at 08:48
  • For a `change_date` column (update and delete): `CREATE TABLE t1 ( ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);` https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html – Ralf Hundewadt Sep 03 '18 at 09:51
  • 1
    the 5th Answer is the correct one as of October 2021. It works! – t1f Oct 28 '21 at 12:31
50

According to this documentation, starting in MySQL 8.0.13, you will be able to specify:

CREATE TABLE INVOICE(
    INVOICEDATE DATE DEFAULT (CURRENT_DATE)
)

MySQL 8.0.13 was released to General Availability in October 2018. The release info is located here.

Amos Long
  • 845
  • 11
  • 14
  • 3
    It worked that way for me, thanks. (look at the mysql version before). – Mylon Dec 11 '21 at 15:06
  • The important thing to note is that an expression has to be inside parentheses. Without parentheses the default must be a literal (except for `CURRENT_TIMESTAMP()`). – Barmar Jun 01 '23 at 17:49
31

Currently from MySQL 8 you can set the following to a DATE column:

In MySQL Workbench, in the Default field next to the column, write: (curdate())

If you put just curdate() it will fail. You need the extra ( and ) at the beginning and end.

t1f
  • 3,021
  • 3
  • 31
  • 61
27

declare your date column as NOT NULL, but without a default. Then add this trigger:

USE `ddb`;
DELIMITER $$
CREATE TRIGGER `default_date` BEFORE INSERT ON `dtable` FOR EACH ROW
if ( isnull(new.query_date) ) then
 set new.query_date=curdate();
end if;
$$
delimiter ;
user3630264
  • 381
  • 3
  • 3
  • I had to set field to `NULL` to make this work, otherwise it was inserting `0000-00-00` instead. MySQL v5.6.12. – lolbas Jun 29 '17 at 05:37
16
create table the_easy_way(
  capture_ts DATETIME DEFAULT CURRENT_TIMESTAMP,
  capture_dt DATE AS (DATE(capture_ts))
)

(MySQL 5.7)

Brian Purgert
  • 159
  • 1
  • 3
7

I have the current latest version of MySQL: 8.0.20

So my table name is visit, my column name is curdate.

alter table visit modify curdate date not null default (current_date);

This writes the default date value with no timestamp.

aworkinghuman
  • 128
  • 1
  • 8
4

----- 2016-07-04 MariaDB 10.2.1 -- Release Note -- -----

Support for DEFAULT with expressions (MDEV-10134).

----- 2018-10-22 8.0.13 General Availability -- -- -----

MySQL now supports use of expressions as default values in data type specifications. This includes the use of expressions as default values for the BLOB, TEXT, GEOMETRY, and JSON data types, which previously could not be assigned default values at all. For details, see Data Type Default Values.

Rick James
  • 135,179
  • 13
  • 127
  • 222
3

As the other answer correctly notes, you cannot use dynamic functions as a default value. You could use TIMESTAMP with the CURRENT_TIMESTAMP attribute, but this is not always possible, for example if you want to keep both a creation and updated timestamp, and you'd need the only allowed TIMESTAMP column for the second.

In this case, use a trigger instead.

Community
  • 1
  • 1
Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
3

I came to this page with the same question in mind, but it worked for me!, Just thought to update here , may be helpful for someone later!!

MariaDB [niffdb]> desc invoice;
+---------+--------+------+-----+---------+----------------+
| Field   | Type   | Null | Key | Default | Extra          |
+---------+--------+------+-----+---------+----------------+
| inv_id  | int(4) | NO   | PRI | NULL    | auto_increment |
| cust_id | int(4) | NO   | MUL | NULL    |                |
| inv_dt  | date   | NO   |     | NULL    |                |
| smen_id | int(4) | NO   | MUL | NULL    |                |
+---------+--------+------+-----+---------+----------------+
4 rows in set (0.003 sec)

MariaDB [niffdb]> ALTER TABLE invoice MODIFY inv_dt DATE NOT NULL DEFAULT (CURRENT_DATE);
Query OK, 0 rows affected (0.003 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [niffdb]> desc invoice;
+---------+--------+------+-----+-----------+----------------+
| Field   | Type   | Null | Key | Default   | Extra          |
+---------+--------+------+-----+-----------+----------------+
| inv_id  | int(4) | NO   | PRI | NULL      | auto_increment |
| cust_id | int(4) | NO   | MUL | NULL      |                |
| inv_dt  | date   | NO   |     | curdate() |                |
| smen_id | int(4) | NO   | MUL | NULL      |                |
+---------+--------+------+-----+-----------+----------------+
4 rows in set (0.002 sec)

MariaDB [niffdb]> SELECT VERSION();
+---------------------------+
| VERSION()                 |
+---------------------------+
| 10.3.18-MariaDB-0+deb10u1 |
+---------------------------+
1 row in set (0.010 sec)

MariaDB [niffdb]>
Arun
  • 1,651
  • 4
  • 20
  • 31
  • worked for me `DEFAULT LAST_DAY(CURDATE())` even without parentheses. some phpmyadmin functions might trigger error though – Hebe Jan 05 '22 at 11:26
1

While creating a table, you have to use CURRENT_DATE() function as default value. Please see below example I just tested.

CREATE TABLE SALES_DATA (
    SALES_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
    SALES_GIRL_ID INT UNSIGNED NOT NULL,
    SALES_DATE DATE NOT NULL DEFAULT (CURRENT_DATE()),
    TOTAL_SALES FLOAT(6, 2),
    PRIMARY KEY (SALES_ID),
    FOREIGN KEY (SALES_GIRL_ID) REFERENCES SALES_GIRLS(ID)
);
Vipul Verma
  • 115
  • 1
  • 7