I am trying the following code:
create table People (
first_name varchar(15) not null,
last_name varchar(15) not null,
registration_date date not null);
How can I make a default value for the date column? try the now(), but it is not valid..
CodePudding user response:
You can use current_date
create table People (
first_name varchar(15) not null,
last_name varchar(15) not null,
registration_date date not null DEFAULT (CURRENT_DATE)
);
CodePudding user response:
This works, I tested on MySQL 8.0.27.
mysql> create table People (
first_name varchar(15) not null,
last_name varchar(15) not null,
registration_date date not null default (current_date));
Note the parentheses around the default expression. This is required.
mysql> insert into People (first_name, last_name) values ('Bill', 'Karwin');
mysql> select * from People;
------------ ----------- -------------------
| first_name | last_name | registration_date |
------------ ----------- -------------------
| Bill | Karwin | 2022-01-13 |
------------ ----------- -------------------
Expressions as column defaults is a new feature introduced in MySQL 8.0.13, so it won't work in older versions of MySQL. See https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html for more information.
