How to create a table with date column?


To create a table with only date column, you can use DATE type. Let us first create a table −

mysql> create table DemoTable
   (
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(20),
   StudentAdmissionDate DATE
   );
Query OK, 0 rows affected (0.47 sec)

Insert records in the table using INSERT command

mysql> insert into DemoTable(StudentName,StudentAdmissionDate) values('Chris',now());
Query OK, 1 row affected, 1 warning (0.12 sec)
mysql> insert into DemoTable(StudentName,StudentAdmissionDate) values('Robert',curdate());
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(StudentName,StudentAdmissionDate) values('David','2019-05-21');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using SELECT statement

mysql> select * from DemoTable;

This will produce the following output −

+-----------+-------------+----------------------+
| StudentId | StudentName | StudentAdmissionDate |
+-----------+-------------+----------------------+
| 1         | Chris       | 2019-04-27           |
| 2         | Robert      | 2019-04-27           |
| 3         | David       | 2019-05-21           |
+-----------+-------------+----------------------+
3 rows in set (0.00 sec)

Updated on: 02-Sep-2023

75K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements