Create MySQL datetime column with default 0000-00-00?

MySQLMySQLi Database

To set default, you can use the DEFAULT keyword in MySQL. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> EmployeeName varchar(20),
   -> EmployeeJoiningDate datetime default '0000-00-00 00:00:00'
   -> )ENGINE=MyISAM,AUTO_INCREMENT=100;
Query OK, 0 rows affected (0.21 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(EmployeeName) values('Chris');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(EmployeeName) values('David');
Query OK, 1 row affected (0.04 sec)
mysql> insert into DemoTable(EmployeeName) values('Mike');
Query OK, 1 row affected (0.03 sec)
mysql> insert into DemoTable(EmployeeName) values('Sam');
Query OK, 1 row affected (0.06 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------+--------------+---------------------+
| EmployeeId | EmployeeName | EmployeeJoiningDate |
+------------+--------------+---------------------+
|        100 | Chris        | 0000-00-00 00:00:00 |
|        101 | David        | 0000-00-00 00:00:00 |
|        102 | Mike         | 0000-00-00 00:00:00 |
|        103 | Sam          | 0000-00-00 00:00:00 |
+------------+--------------+---------------------+
4 rows in set (0.00 sec)
raja
Published on 17-Dec-2019 06:27:12
Advertisements