1

Hello! I can't understand what's a problem?

CREATE TABLE expenses(
  num INT,
  paydate DATE DEFAULT DATE(),
  receiver INT NOT NULL DEFAULT 1,
  value DEC(10,2) NOT NULL,
  PRIMARY KEY(num)
);

And I have a problem:

ERROR 1064(42000):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 '(), receiver INT NOT NULL DEFAULT 1, value DEC(10,2) NOT NULL, PRIMARY KEY(num))' at line 4

Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
Aleks
  • 45
  • 4
  • See [this tread](https://stackoverflow.com/questions/20461030/current-date-curdate-not-working-as-default-date-value) for using a trigger – SBF Sep 24 '17 at 12:05
  • If you use MariaDB: "From MariaDB 10.2.1 you can use most functions in DEFAULT" (https://mariadb.com/kb/en/library/create-table/). So the following would work: `paydate DATE DEFAULT CURDATE()`. – Paul Spiegel Sep 24 '17 at 16:49

1 Answers1

3

Unfortunately, MySQL does not let you default only the date. You need to default a datetime:

CREATE TABLE expenses (
  num INT,
  paydate datetime DEFAULT now(),
  receiver INT NOT NULL DEFAULT 1,
  value DEC(10,2) NOT NULL,
  PRIMARY KEY (num)
);

Here is a SQL Fiddle.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786