Before we begin, let’s try setting “when” to the column name while using the 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. The error will appear:
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 use backticks to surround reserved words, such as "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)
Use select statement to display all the records in the table -
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)
The above is the detailed content of Can we use 'When' as column name in CREATE TABLE statement?. For more information, please follow other related articles on the PHP Chinese website!