### 1.7.1 标准 SQL 的 MySQL 扩展 [](<>)[](<>) MySQL 服务器支持一些您在其他 SQL DBMS 中不太可能找到的扩展。请注意,如果您使用它们,您的代码很可能无法移植到其他 SQL 服务器。在某些情况下,您可以使用以下形式的注释编写包含 MySQL 扩展但仍可移植的代码: ``` /*! MySQL-specific code */ ``` 在这种情况下,MySQL 服务器解析并执行注释中的代码,就像任何其他 SQL 语句一样,但其他 SQL 服务器应该忽略扩展。例如,MySQL 服务器识别`STRAIGHT_JOIN`以下语句中的关键字,但其他服务器不应: ``` SELECT /*! STRAIGHT_JOIN */ col1 FROM table1,table2 WHERE ... ``` 如果在后面加上版本号`!`字符,注释中的语法只有在 MySQL 版本大于或等于指定的版本号时才会执行。这`KEY_BLOCK_SIZE`以下注释中的子句仅由 MySQL 5.1.10 或更高版本的服务器执行: ``` CREATE TABLE t1(a INT, KEY (a)) /*!50110 KEY_BLOCK_SIZE=1024 */; ``` 以下描述列出了按类别组织的 MySQL 扩展。 - 磁盘上的数据组织 [](<>)[](<>)[](<>)[](<>) MySQL Server 将每个数据库映射到 MySQL 数据目录下的一个目录,并将数据库中的表映射到数据库目录中的文件名。因此,在具有区分大小写文件名的操作系统(例如大多数 Unix 系统)上,数据库和表名在 MySQL Server 中是区分大小写的。看[第 9.2.3 节,“标识符大小写敏感性”](identifier-case-sensitivity.html). - 通用语言语法 - 默认情况下,字符串可以由`"`也`'`.如果[`ANSI_QUOTES`](sql-mode.html#sqlmode_ansi_quotes)启用 SQL 模式,字符串只能由`'`并且服务器解释由`"`作为标识符。 - `\`是字符串中的转义字符。 - 在 SQL 语句中,您可以使用*`db_name.tbl_name`*句法。一些 SQL 服务器提供相同的功能,但称之为`用户空间`.MySQL Server 不支持在如下语句中使用的表空间:`创建表 ralph.my_table ... 在 my_tablespace`. - SQL 语句语法 - 这[`分析表`](analyze-table.html),[`检查表`](check-table.html),[`优化表`](optimize-table.html), 和[`维修台`](repair-table.html)陈述。 - 这[`创建数据库`](create-database.html),[`删除数据库`](drop-database.html), 和[`更改数据库`](alter-database.html)陈述。看[第 13.1.12 节,“CREATE DATABASE 语句”](create-database.html),[第 13.1.24 节,“DROP DATABASE 语句”](drop-database.html), 和[第 13.1.2 节,“ALTER DATABASE 语句”](alter-database.html). - 这[`做`](do.html)陈述。 - [`解释选择`](explain.html)获取查询优化器如何处理表的描述。 - 这[`冲洗`](flush.html)和[`重置`](reset.html)陈述。 - 这[`放`](set-variable.html)陈述。看[第 13.7.6.1 节,“变量赋值的 SET 语法”](set-variable.html). - 这[`显示`](show.html)陈述。看[第 13.7.7 节,“显示语句”](show.html).由许多 MySQL 特定的信息产生的[`显示`](show.html)可以通过使用以更标准的方式获得语句[`选择`](select.html)查询`INFORMATION_SCHEMA`.看[第 26 章,*信息\_SCHEMA 表*](information-schema.html). - [](<>) [](<>)用于[`加载数据`](load-data.html).在许多情况下,此语法与 Oracle 兼容[`加载数据`](load-data.html).看[第 13.2.7 节,“加载数据语句”](load-data.html). - 用于[`重命名表`](rename-table.html).看[第 13.1.36 节,“重命名 TABLE 语句”](rename-table.html). - 用于[`代替`](replace.html)代替[`删除`](delete.html)加[`插入`](insert.html).看[第 13.2.9 节,“REPLACE 语句”](replace.html). - 用于`改变 *`col_name`*`,`降低 *`col_name`*`, 要么[`删除索引`](drop-index.html),`忽视`要么`改名`在[`更改表`](alter-table.html)陈述。使用多个`添加`,`改变`,`降低`, 要么`改变`中的从句[`更改表`](alter-table.html)陈述。看[第 13.1.9 节,“ALTER TABLE 语句”](alter-table.html). - 索引名称的使用、列前缀的索引以及`指数`要么`钥匙`在[`创建表`](create-table.html)陈述。看[第 13.1.20 节,“CREATE TABLE 语句”](create-table.html). - 用于`暂时的`要么`如果不存在`和[`创建表`](create-table.html). - 用于`如果存在`和[`删除表`](drop-table.html)和[`删除数据库`](drop-database.html). - 一个删除多个表的能力[`删除表`](drop-table.html)陈述。 - 这`订购方式`和`限制`的条款[`更新`](update.html)和[`删除`](delete.html)陈述。 - `插入 *`tbl_name`* 放 *`col_name`* = ...`句法。 - 这`延迟`的条款[`插入`](insert.html)和[`代替`](replace.html)陈述。 - 这`低优先级`的条款[`插入`](insert.html), [`代替`](replace.html), [`删除`](delete.html), 和[`更新`](update.html)陈述。 - 用于`进入档案`要么`转储文件`在[`选择`](select.html)陈述。看[第 13.2.10 节,“SELECT 语句”](select.html). - 选项如`STRAIGHT_JOIN`要么`SQL_SMALL_RESULT`在[`选择`](select.html)陈述。 - 您无需在`通过...分组`条款。这为一些非常具体但非常正常的查询提供了更好的性能。看[第 12.20 节,“聚合函数”](aggregate-functions-and-modifiers.html). - 您可以指定`ASC`和`DESC`和`通过...分组`,不只是与`订购方式`. - 在语句中设置变量的能力`:=`赋值运算符。看[第 9.4 节,“用户定义的变量”](user-variables.html). - 数据类型 - 这[`中型`](integer-types.html),[`放`](set.html), 和[`枚举`](enum.html)数据类型和各种[`斑点`](blob.html)和[`文本`](blob.html)数据类型。 - 这`自动递增`,`二进制`,`空值`,`未签名`, 和`零填充`数据类型属性。 - 函数和运算符 - 为了方便从其他 SQL 环境迁移的用户,MySQL Server 支持许多函数的别名。例如,所有字符串函数都支持标准 SQL 语法和 ODBC 语法。 - MySQL 服务器理解[`||`](logical-operators.html#operator_or)和[`&&`](logical-operators.html#operator_and)运算符表示逻辑 OR 和 AND,就像在 C 编程语言中一样。在 MySQL 服务器中,[`||`](logical-operators.html#operator_or)和[`要么`](logical-operators.html#operator_or)是同义词[`&&`](logical-operators.html#operator_and)和[`和`](logical-operators.html#operator_and).由于这种漂亮的语法,MySQL Server 不支持标准 SQL[`||`](logical-operators.html#operator_or)字符串连接运算符;采用[`连接()`](string-functions.html#function_concat)反而。因为[`连接()`](string-functions.html#function_concat)接受任意数量的参数,很容易转换使用[`||`](logical-operators.html#operator_or)运算符到 MySQL 服务器。 - 用于[`计数(不同 *`价值清单`*)`](aggregate-functions.html#function_count)在哪里*`价值清单`*有不止一种元素。 - 默认情况下,字符串比较不区分大小写,排序顺序由当前字符集的排序规则确定,即`utf8mb4`默认。要改为执行区分大小写的比较,您应该使用`二进制`属性或使用`二进制`cast,这会导致使用底层字符代码值而不是词法顺序进行比较。 - [](<>) [](<>)这[`%`](mathematical-functions.html#function_mod)运算符是的同义词[`模组()`](mathematical-functions.html#function_mod).那是,`*`ñ`* % *`米`*`相当于[`模组(*`ñ`*,*`米`*)`](mathematical-functions.html#function_mod).[`%`](mathematical-functions.html#function_mod)支持 C 程序员并与 PostgreSQL 兼容。 - 这[`=`](comparison-operators.html#operator_equal),[`<>`](comparison-operators.html#operator_not-equal),[`<=`](comparison-operators.html#operator_less-than-or-equal),[`<`](comparison-operators.html#operator_less-than),[`>=`](comparison-operators.html#operator_greater-than-or-equal),[`>`](comparison-operators.html#operator_greater-than),[`<<`](bit-functions.html#operator_left-shift),[`>>`](bit-functions.html#operator_right-shift),[`<=>`](comparison-operators.html#operator_equal-to),[`和`](logical-operators.html#operator_and),[`要么`](logical-operators.html#operator_or), 要么[`喜欢`](string-comparison-functions.html#operator_like)运算符可用于输出列列表中的表达式(在`从`) 在[`选择`](select.html)陈述。例如: ``` mysql> SELECT col1=1 AND col2=2 FROM my_table; ``` - 这[`LAST_INSERT_ID()`](information-functions.html#function_last-insert-id)函数返回最近的`自动递增`价值。看[第 12.16 节,“信息功能”](information-functions.html). - [`喜欢`](string-comparison-functions.html#operator_like)在数值上是允许的。 - 这[`正则表达式`](regexp.html#operator_regexp)和[`不是正则表达式`](regexp.html#operator_not-regexp)扩展的正则表达式运算符。 - [`连接()`](string-functions.html#function_concat)要么[`字符()`](string-functions.html#function_char)有一个论点或两个以上论点。(在 MySQL 服务器中,这些函数可以采用可变数量的参数。) - 这[`BIT_COUNT()`](bit-functions.html#function_bit-count),[`案子`](flow-control-functions.html#operator_case),[`埃尔特()`](string-functions.html#function_elt),[`FROM_DAYS()`](date-and-time-functions.html#function_from-days),[`格式()`](string-functions.html#function_format),[`如果()`](flow-control-functions.html#function_if),[`MD5()`](encryption-functions.html#function_md5),[`PERIOD_ADD()`](date-and-time-functions.html#function_period-add),[`PERIOD_DIFF()`](date-and-time-functions.html#function_period-diff),[`TO_DAYS()`](date-and-time-functions.html#function_to-days), 和[`平日()`](date-and-time-functions.html#function_weekday)职能。 - 用于[`修剪()`](string-functions.html#function_trim)修剪子字符串。标准 SQL 仅支持删除单个字符。 - 这`通过...分组`职能[`性病()`](aggregate-functions.html#function_std),[`位或()`](aggregate-functions.html#function_bit-or),[`位与()`](aggregate-functions.html#function_bit-and),[`BIT_XOR()`](aggregate-functions.html#function_bit-xor), 和[`GROUP_CONCAT()`](aggregate-functions.html#function_group-concat).看[第 12.20 节,“聚合函数”](aggregate-functions-and-modifiers.html).