Creating a Table in MySQL to set current date as default

MySQLMySQLi Database

Following is the syntax for creating a table and adding DEFAULT constraint to set default value −

CREATE TABLE yourTableName
(
yourColumnName1 dataType not null ,
yourColumnName2 dataType default anyValue,
.
.
.
N
);;

Let us create a table wherein we have set “employee_joining_date” with default constraint for current date as default −

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 some records into the table with the help of insert command −

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)

Display records from the table using select statement −

mysql> select *from demo43;

This will produce the following output −

+-------------+---------------+-----------------+-----------------------+
| 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)
raja
Published on 19-Nov-2020 12:56:33
Advertisements