为此,请在列数据类型之后使用DEFAULT关键字。
让我们创建一个表-
mysql> create table demo33 −> ( −> id int not null auto_increment primary key, −> name varchar(20) not null, −> start_date date default(current_date), −> end_date date default NULL, −> category enum('Good','Medium','Low') default 'Low' −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo33(name) values('John'); mysql> insert into demo33(name,end_date,category) values('David','2020−12−21','Medium'); mysql> insert into demo33(name,start_date,category) values('David','2020−04−01','Good'); mysql> insert into demo33(name,start_date,end_date,category) values('David','2020−03−10','2020−08−22','Good');
使用select语句显示表中的记录-
mysql> select *from demo33;
这将产生以下输出-
+----+-------+------------+------------+----------+ | id | name | start_date | end_date | category | +----+-------+------------+------------+----------+ | 1 | John | 2020−10−28 | NULL | Low | | 2 | David | 2020−10−28 | 2020−12−21 | Medium | | 3 | David | 2020−04−01 | NULL | Good | | 4 | David | 2020−03−10 | 2020−08−22 | Good | +----+-------+------------+------------+----------+ 4 rows in set (0.00 sec)