Syntax error Can we use “When” as column name in CREATE TABLE statement?

Can we use “When” as column name in CREATE TABLE statement?



Before beginning, let us try to set ‘when’ as column name while using CREATE TABLE statement −

mysql> create table DemoTable693(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(100),
   When datetime
);

This will produce the following output. An error would be visible:

ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near 'When datetime at line 5

You need to wrap the reserved word using backticks, for example, `when`. Let us first create a table and implement the same:

mysql> create table DemoTable693 (
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(100),
   `When` datetime
);
Query OK, 0 rows affected (0.63 sec)

Insert some records in the table using insert command:

mysql> insert into DemoTable693(StudentName,`When`) values('Chris',NOW());
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable693(StudentName,`When`) values('Robert',CURDATE());
Query OK, 1 row affected (0.22 sec)

Display all records from the table using select statement -

mysql> select *from DemoTable693;

This will produce the following output -

+-----------+-------------+---------------------+
| StudentId | StudentName | When                |
+-----------+-------------+---------------------+
| 1         | Chris       | 2019-07-21 18:57:19 |
| 2         | Robert      | 2019-07-21 00:00:00 |
+-----------+-------------+---------------------+
2 rows in set (0.00 sec)
Updated on: 2019-08-21T11:39:49+05:30

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements