以下是创建表和添加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) −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo43(employee_name,employee_status,employee_joining_date) values('John','JOINED','2020-05-10'); mysql> insert into demo43(employee_name,employee_status,employee_joining_date) values('David','JOINED','2020-10-12'); mysql> insert into demo43(employee_name) values('Bob');
使用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)