在 MySQL 中创建一个表以将当前日期设置为默认值
以下是创建表格并添加默认约束以设置默认值的语法 –
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)
使用插入命令将一些记录插入该表 –
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)
阅读更多:MySQL 教程