Home > Database > Mysql Tutorial > Can we use 'When' as column name in CREATE TABLE statement?

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

王林
Release: 2023-09-11 16:17:02
forward
1513 people have browsed it

我们可以在 CREATE TABLE 语句中使用“When”作为列名吗?

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
);
Copy after login

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
Copy after login

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)
Copy after login

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)
Copy after login

Use select statement to display all the records in the table -

mysql> select *from DemoTable693;
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template