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

相关文章:

mysql 从日期格式中提取年份

mysql - 无法为列设置默认空值

python - MySQL 没有建立与数据库的连接

php - 按其他表中的标题对结果进行分组

MySQL 前 6 个月累计总和

regex - 使用grep时引用?

php - 邮件系统数据库结构,需要帮助

sql - 选择没有重复项的随机行

python - 如何在 Python 中连接单个引号

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