在 MySQL 中创建表以将当前日期设置为默认值
mysqlmysqli database
以下是创建表并添加 DEFAULT 约束以设置默认值的语法 −
CREATE TABLE yourTableName ( yourColumnName1 dataType not null , yourColumnName2 dataType default anyValue, . . . N );;
让我们创建一个表,其中我们设置“employee_joining_date”,并将默认约束设置为当前日期 −
mysql> create table demo43 −> ( −> employee_id int not null auto_increment primary key, −> employee_name varchar(40) not null, −> employee_status varchar(60) default "NOT JOINED", −> employee_joining_date date default(CURRENT_DATE) −> ); Query OK, 0 rows affected (0.66 sec)
使用 insert 命令向表中插入一些记录 −
mysql> insert into demo43(employee_name,employee_status,employee_joining_date) values('John','JOINED','2020-05-10'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo43(employee_name,employee_status,employee_joining_date) values('David','JOINED','2020-10-12'); Query OK, 1 row affected (0.21 sec) mysql> insert into demo43(employee_name) values('Bob'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的记录 −
mysql> select *from demo43;
这将产生以下输出 −
+-------------+---------------+-----------------+-----------------------+ | employee_id | employee_name | employee_status | employee_joining_date | +-------------+---------------+-----------------+-----------------------+ | 1 | John | JOINED | 2020−05−10 | | 2 | David | JOINED | 2020−10−12 | | 3 | Bob | NOT JOINED | 2020−10−31 | +-------------+---------------+-----------------+-----------------------+ 3 rows in set (0.00 sec)