mysql - MySQL 中何时使用单引号、双引号和反引号

标签 mysql sql quotes

我正在尝试学习编写查询的最佳方法。我也明白保持一致的重要性。到目前为止,我只是随意使用单引号、双引号和反引号。

示例:

$query = 'INSERT INTO table (id, col1, col2) VALUES (NULL, val1, val2)';

此外,在上面的示例中,请考虑 tablecol1val1 等可能是变量。

这个的标准是什么?你做什么?

我在这里阅读了类似问题的答案大约 20 分钟,但似乎这个问题没有明确的答案。

最佳答案

反引号用于表和列标识符,但仅当标识符为 MySQL reserved keyword 时才需要。 ,或者当标识符包含空白字符或超出有限集的字符时(见下文),通常建议尽可能避免使用保留关键字作为列或表标识符,以避免引用问题。

单引号应用于字符串值,如 VALUES() 列表中的值。 MySQL 也支持对字符串值使用双引号,但单引号更广泛地被其他 RDBMS 接受,因此使用单引号而不是双引号是一个好习惯。

MySQL 还期望 DATEDATETIME 文字值作为字符串用单引号引起来,如 '2001-01-01 00:00:00'.咨询the Date and Time Literals有关更多详细信息,特别是使用连字符 - 作为日期字符串中的分段分隔符的替代方法,请参阅文档。

因此,使用您的示例,我将双引号 PHP 字符串,并对值 'val1', 'val2' 使用单引号。 NULL 是一个 MySQL 关键字,也是一个特殊的(非)值,因此不带引号。

这些表或列标识符都不是保留字或使用需要引用的字符,但我还是用反引号引用了它们(稍后会详细介绍...)。

RDBMS 的原生函数(例如 MySQL 中的 NOW())不应被引用,尽管它们的参数遵循已经提到的相同字符串或标识符引用规则。

Backtick (`)
table & column ───────┬─────┬──┬──┬──┬────┬──┬────┬──┬────┬──┬───────┐
                      ↓     ↓  ↓  ↓  ↓    ↓  ↓    ↓  ↓    ↓  ↓       ↓
$query = "INSERT INTO `table` (`id`, `col1`, `col2`, `date`, `updated`) 
                       VALUES (NULL, 'val1', 'val2', '2001-01-01', NOW())";
                               ↑↑↑↑  ↑    ↑  ↑    ↑  ↑          ↑  ↑↑↑↑↑ 
Unquoted keyword          ─────┴┴┴┘  │    │  │    │  │          │  │││││
Single-quoted (') strings ───────────┴────┴──┴────┘  │          │  │││││
Single-quoted (') DATE    ───────────────────────────┴──────────┘  │││││
Unquoted function         ─────────────────────────────────────────┴┴┴┴┘    

Variable interpolation

The quoting patterns for variables do not change, although if you intend to interpolate the variables directly in a string, it must be double-quoted in PHP. Just make sure that you have properly escaped the variables for use in SQL. (It is recommended to use an API supporting prepared statements instead, as protection against SQL injection).

// Same thing with some variable replacements
// Here, a variable table name $table is backtick-quoted, and variables
// in the VALUES list are single-quoted 
$query = "INSERT INTO `$table` (`id`, `col1`, `col2`, `date`) VALUES (NULL, '$val1', '$val2', '$date')";

Prepared statements

When working with prepared statements, consult the documentation to determine whether or not the statement's placeholders must be quoted. The most popular APIs available in PHP, PDO and MySQLi, expect unquoted placeholders, as do most prepared statement APIs in other languages:

// PDO example with named parameters, unquoted
$query = "INSERT INTO `table` (`id`, `col1`, `col2`, `date`) VALUES (:id, :col1, :col2, :date)";

// MySQLi example with ? parameters, unquoted
$query = "INSERT INTO `table` (`id`, `col1`, `col2`, `date`) VALUES (?, ?, ?, ?)";

标识符中需要反引号引用的字符:

According to MySQL documentation ,您不需要使用以下字符集引用(反引号)标识符:

ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)

您可以使用超出该设置的字符作为表或列标识符,例如包括空格,但您必须引用(反引号)它们。

此外,虽然数字是标识符的有效字符,但标识符不能仅由数字组成。如果这样做,则必须用反引号括起来。

关于mysql - MySQL 中何时使用单引号、双引号和反引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52285634/

相关文章:

quotes - 这个软件报价是什么意思?

Python:在三引号中键入制表符和\t 之间的区别

php - 在 PHP 中通过 SSH 连接到 MySQL

python - MySql UPDATE 查询在 mysql 中有效,但 1064 来自 Python

php - 基于 mysql 查询的报告生成

MySQL #1064 - 你的 SQL 语法有错误

mysql - 在表中添加新列,其值取决于同一表中另一列的值

lua - 为什么这个 Lua 模式不起作用?

php - 将 AS3 与 PHP 通信。发送变量到php页面,但$_POST为空

python - py脚本从mysql数据库写入xlsx文件