1

I'm currently working on a small project in which I need to insert some data into database tables. What I want to do is to set the default value of a certain column to today's date, but only to today's date.

Is this even possible? I already tried the datatypes DATETIME and TIME and its functions CURRENT_TIMESTAMP() and TIMESTAMP() to set their value. Problem is that I only want to have the today's date in my column, not the date and the time. Does anyone know if that's possible?

Here an example:

CREATE TABLE IF NOT EXISTS Bestellung(
BestellNr MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
Bestelldatum DATE DEFAULT ??? NOT NULL,
FahrerID TINYINT UNSIGNED NOT NULL,
KundenNr SMALLINT UNSIGNED NOT NULL,
FOREIGN KEY(FahrerID) REFERENCES
Fahrer(FahrerID) ON UPDATE CASCADE,
FOREIGN KEY(KundenNr) REFERENCES
Kunde(KundenNr) ON UPDATE CASCADE,
PRIMARY KEY(BestellNr),
INDEX(Bestelldatum)
) 
Pete Hilde
  • 659
  • 1
  • 10
  • 24
  • 2
    You can't set the default value of a `DATE` column. See https://dev.mysql.com/doc/refman/5.7/en/data-type-defaults.html – Barmar Jun 22 '17 at 16:49
  • @Barmar: Wow, what a stupid restriction. – dnoeth Jun 22 '17 at 17:28
  • @dnoeth MySQL has lots of stupid restrictions, like not being able to refer to the same temporary table twice in a query, not allowing subqueries in views, not allowing `LIMIT` in subqueries used in `WHERE-IN`. – Barmar Jun 22 '17 at 18:32
  • @dnoeth Until recently you couldn't have a default value on more than one `TIMESTAMP` column in a table. – Barmar Jun 22 '17 at 18:33
  • What version of MySQL/MariaDB are you running? – Rick James Jul 04 '17 at 01:44

1 Answers1

0

In MySL you could use the function CURDATE() to retun only the date as YYYY-MM-DD.

Reference: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html

Thais Marinho
  • 191
  • 1
  • 7