createtableDemoTable693( StudentIdintNOTNULLAUTO_INCREMENTPRIMARYKEY, StudentNamevarchar(100), Whendatetime); This will produce the following output. Error will occur: ERROR1064(42000):You"/> createtableDemoTable693( StudentIdintNOTNULLAUTO_INCREMENTPRIMARYKEY, StudentNamevarchar(100), Whendatetime); This will produce the following output. Error will occur: ERROR1064(42000):You">

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?

王林
王林forward
2023-09-11 16:17:021554browse

我们可以在 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
);

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete