Home > Database > Mysql Tutorial > Are quotes around tables and columns in MySQL queries really necessary?

Are quotes around tables and columns in MySQL queries really necessary?

PHPz
Release: 2023-09-03 22:49:02
forward
1056 people have browsed it

MySQL 查询中的表和列周围的引号真的有必要吗?

If your table name or column name is any reserved word, then you need to use quotes around the table name and column name in the MySQL query. You need to use backticks around table and column names. The syntax is as follows:

SELECT *FROM `table` where `where`=condition;
Copy after login

Here is a query to create a table without quotation marks and reserved words. You will receive an error message because they are predefined reserved words. The error is as follows:

mysql> create table table
   -> (
   -> where int
   -> );
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 'table
(
   where int
)' at line 1
Copy after login

Now let's put quotes around the table and column names, since "table" and "where" are reserved words. This is the quoted query:

mysql> create table `table`
   -> (
   -> `where` int
   -> );
Query OK, 0 rows affected (0.55 sec)
Copy after login

Use the insert command to insert records in the table. The query is as follows:

mysql> insert into `table`(`where`) values(1);
Query OK, 1 row affected (0.13 sec)
mysql> insert into `table`(`where`) values(100);
Query OK, 1 row affected (0.26 sec)
mysql> insert into `table`(`where`) values(1000);
Query OK, 1 row affected (0.13 sec)
Copy after login

Display specific records in the table with the help of where conditions. The query is as follows:

mysql> select *from `table` where `where`=100;
Copy after login

The following is the output:

+-------+
| where |
+-------+
| 100   |
+-------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of Are quotes around tables and columns in MySQL queries really necessary?. 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