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/34515255/

相关文章:

php - 将参数从 Controller 传递到模型以进行 mysql 查询的优化方法

php - 对于 Mysql 和 PHP 中的 UTF-8,我真的需要从 VARCHAR 切换到 VARBINARY 吗?

sql - 带有关系表的 Postgres tsvector

mysql - 从两个具有不同列的表中进行选择,其中一个需要计数

php - 带有邮政编码数据的城市名称

SQL 按类型和位置唯一分组

sql - 创建一个 View ,其中 PostgreSQL 显示特定字段的前 100 行,然后移动到下一个

excel - 用双引号括起来的csv文件不去除引号

sql - 如何在 SQL 准备语句中转义单引号和双引号?

shell - shell脚本中双引号内的单引号