MySQL Server supports some extensions that you are not likely to find in other SQL DBMSs. MySQL Server支持一些在其他SQL数据库管理系统中找不到的扩展。Be warned that if you use them, your code is most likely not portable to other SQL servers. 请注意,如果您使用它们,您的代码很可能无法移植到其他SQL服务器。In some cases, you can write code that includes MySQL extensions, but is still portable, by using comments of the following form:在某些情况下,您可以使用以下形式的注释来编写包含MySQL扩展但仍然可移植的代码:
/*! MySQL-specific code
*/
In this case, MySQL Server parses and executes the code within the comment as it would any other SQL statement, but other SQL servers should ignore the extensions. 在这种情况下,MySQL Server会像解析任何其他SQL语句一样解析并执行注释中的代码,但其他SQL服务器应该忽略扩展。For example, MySQL Server recognizes the 例如,MySQL Server可以识别以下语句中的STRAIGHT_JOIN
keyword in the following statement, but other servers should not:STRIGHT_JOIN
关键字,但其他服务器不应该识别:
SELECT /*! STRAIGHT_JOIN */ col1 FROM table1,table2 WHERE ...
If you add a version number after the 如果在之后添加版本号!
character, the syntax within the comment is executed only if the MySQL version is greater than or equal to the specified version number. !
字符,只有当MySQL版本大于或等于指定的版本号时,才会执行注释中的语法。The 以下注释中的KEY_BLOCK_SIZE
clause in the following comment is executed only by servers from MySQL 5.1.10 or higher:KEY_BLOCK_SIZE
子句仅由MySQL 5.1.10或更高版本的服务器执行:
CREATE TABLE t1(a INT, KEY (a)) /*!50110 KEY_BLOCK_SIZE=1024 */;
The following descriptions list MySQL extensions, organized by category.以下描述按类别列出了MySQL扩展。
Organization of data on disk磁盘上的数据组织
MySQL Server maps each database to a directory under the MySQL data directory, and maps tables within a database to file names in the database directory. Consequently, database and table names are case-sensitive in MySQL Server on operating systems that have case-sensitive file names (such as most Unix systems). MySQL Server将每个数据库映射到MySQL数据目录下的一个目录,并将数据库中的表映射到数据库目录中的文件名。因此,在具有区分大小写文件名的操作系统(如大多数Unix系统)上的MySQL Server中,数据库和表名是区分大小写的。See Section 9.2.3, “Identifier Case Sensitivity”.请参阅第9.2.3节,“标识符大小写敏感性”。
General language syntax通用语言语法
By default, strings can be enclosed by 默认情况下,字符串可以用"
as well as '
. "
以及'
括起来。If the 如果启用了ANSI_QUOTES
SQL mode is enabled, strings can be enclosed only by '
and the server interprets strings enclosed by "
as identifiers.ANSI_QUOTES
SQL模式,则字符串只能用'
括起来,并且服务器将用"
括起来的字符串解释为标识符。
\
is the escape character in strings.是字符串中的转义符。
In SQL statements, you can access tables from different databases with the 在SQL语句中,可以使用db_name.tbl_name
syntax. db_name.tbl_name
语法访问来自不同数据库的表。Some SQL servers provide the same functionality but call this 某些SQL服务器提供相同的功能,但调用此User space
. User space
。MySQL Server doesn't support tablespaces such as used in statements like this: MySQL Server不支持诸如以下语句中使用的表空间:CREATE TABLE ralph.my_table ... IN my_tablespace
.CREATE TABLE ralph.my_table ... IN my_tablespace
。
SQL statement syntaxSQL语句语法
The ANALYZE TABLE
, CHECK TABLE
, OPTIMIZE TABLE
, and REPAIR TABLE
statements.ANALYZE TABLE
、CHECK TABLE
、OPTIMIZE TABLE
和REPAIR TABLE
语句。
The CREATE DATABASE
, DROP DATABASE
, and ALTER DATABASE
statements. See Section 13.1.12, “CREATE DATABASE Statement”, Section 13.1.24, “DROP DATABASE Statement”, and Section 13.1.2, “ALTER DATABASE Statement”.CREATE DATABASE
、DROP DATABASE
和ALTER DATABASE
语句。请参阅第13.1.12节,“CREATE DATABASE语句”、第13.1.24节,“DROP DATABASE语句”和第13.1.2节,“ALTER DATABASE语句”。
EXPLAIN SELECT
to obtain a description of how tables are processed by the query optimizer.EXPLAIN SELECT
以获得查询优化器如何处理表的描述。
The SET
statement. See Section 13.7.6.1, “SET Syntax for Variable Assignment”.SET
语句。参见第13.7.6.1节,“变量赋值的SET语法”。
The SHOW
statement. See Section 13.7.7, “SHOW Statements”. SHOW
语句。参见第13.7.7节,“SHOW语句”。The information produced by many of the MySQL-specific 许多MySQL特有的SHOW
statements can be obtained in more standard fashion by using SELECT
to query INFORMATION_SCHEMA
. SHOW
语句产生的信息可以通过使用SELECT
查询INFORMATION_SCHEMA
以更标准的方式获得。See Chapter 26, INFORMATION_SCHEMA Tables.请参阅第26章,“INFORMATION_SCHEMA表格”。
Use of LOAD DATA
. In many cases, this syntax is compatible with Oracle LOAD DATA
. LOAD DATA
的使用。在许多情况下,此语法与Oracle LOAD DATA
兼容。See Section 13.2.7, “LOAD DATA Statement”.请参阅第13.2.7节,“LOAD DATA语句”。
Use of RENAME TABLE
. See Section 13.1.36, “RENAME TABLE Statement”.RENAME TABLE
的使用。参见第13.1.36节,“RENAME TABLE语句”。
Use of 使用REPLACE
instead of DELETE
plus INSERT
. REPLACE
而不是DELETE
加INSERT
。See Section 13.2.9, “REPLACE Statement”.请参阅第13.2.9节,“REPLACE语句”。
Use of 在CHANGE
, col_name
DROP
, or col_name
DROP INDEX
, IGNORE
or RENAME
in ALTER TABLE
statements. ALTER TABLE
语句中使用CHANGE col_name
、DROP col_name
或DROP INDEX
、IGNORE
或RENAME
。Use of multiple 在ADD
, ALTER
, DROP
, or CHANGE
clauses in an ALTER TABLE
statement. ALTER TABLE
语句中使用多个ADD
、ALTER
、DROP
或CHANGE
子句。See Section 13.1.9, “ALTER TABLE Statement”.请参阅第13.1.9节,“ALTER TABLE语句”。
Use of index names, indexes on a prefix of a column, and use of 在INDEX
or KEY
in CREATE TABLE
statements. CREATE TABLE
语句中使用索引名称、列前缀上的索引以及INDEX
或KEY
。See Section 13.1.20, “CREATE TABLE Statement”.请参阅第13.1.20节,“CREATE TABLE语句”。
Use of 将TEMPORARY
or IF NOT EXISTS
with CREATE TABLE
.TEMPORARY
或IF NOT EXISTS
与CREATE TABLE
一起使用。
Use of 将IF EXISTS
with DROP TABLE
and DROP DATABASE
.IF EXISTS
与DROP TABLE
和DROP DATABASE
一起使用。
The capability of dropping multiple tables with a single 使用单个DROP TABLE
statement.DROP TABLE
语句删除多个表的功能。
The ORDER BY
and LIMIT
clauses of the UPDATE
and DELETE
statements.UPDATE
和DELETE
语句的ORDER BY
和LIMIT
子句。
INSERT INTO
syntax.tbl_name
SET col_name
= ...INSERT INTO tbl_name SET col_name = ...
语法。
The DELAYED
clause of the INSERT
and REPLACE
statements.INSERT
和REPLACE
语句的DELAYED
子句。
The LOW_PRIORITY
clause of the INSERT
, REPLACE
, DELETE
, and UPDATE
statements.INSERT
、REPLACE
、DELETE
和UPDATE
语句的LOW_PRIORITY
子句。
Use of 在INTO OUTFILE
or INTO DUMPFILE
in SELECT
statements. SELECT
语句中使用INTO OUTFILE
或INTO DUMPFILE
。See Section 13.2.10, “SELECT Statement”.请参阅第13.2.10节,“SELECT语句”。
Options such as STRAIGHT_JOIN
or SQL_SMALL_RESULT
in SELECT
statements.SELECT
语句中的STRIGHT_JOIN
或SQL_SMALL_RESULT
等选项。
You don't need to name all selected columns in the 您不需要在GROUP BY
clause. GROUP BY
子句中命名所有选定的列。This gives better performance for some very specific, but quite normal queries. 这为一些非常具体但非常正常的查询提供了更好的性能。See Section 12.20, “Aggregate Functions”.请参阅第12.20节,“聚合函数”。
You can specify 可以使用ASC
and DESC
with GROUP BY
, not just with ORDER BY
.GROUP BY
指定ASC
和DESC
,而不仅仅是使用ORDER BY
。
The ability to set variables in a statement with the 使用:=
assignment operator. :=
赋值运算符在语句中设置变量的能力。See Section 9.4, “User-Defined Variables”.请参阅第9.4节,“用户定义变量”。
Data types数据类型
Functions and operators功能和运算符
To make it easier for users who migrate from other SQL environments, MySQL Server supports aliases for many functions. For example, all string functions support both standard SQL syntax and ODBC syntax.为了方便从其他SQL环境迁移的用户,MySQL Server支持许多函数的别名。例如,所有字符串函数都支持标准SQL语法和ODBC语法。
MySQL Server understands the MySQL Server将||
and &&
operators to mean logical OR and AND, as in the C programming language. ||
和&&
运算符理解为逻辑OR
和AND
,就像在C编程语言中一样。In MySQL Server, 在MySQL Server中,||
and OR
are synonyms, as are &&
and AND
. ||
和OR
是同义词,&&
和AND
也是同义词。Because of this nice syntax, MySQL Server doesn't support the standard SQL 由于这种良好的语法,MySQL Server不支持标准的SQL ||
operator for string concatenation; use CONCAT()
instead. ||
运算符进行字符串连接;请改用CONCAT()
。Because 因为CONCAT()
takes any number of arguments, it is easy to convert use of the ||
operator to MySQL Server.CONCAT()
接受任意数量的参数,所以很容易将||
运算符的使用转换为MySQL Server。
Use of 使用COUNT(DISTINCT
where value_list
)value_list
has more than one element.COUNT(DISTINCT value_list)
,其中value_list
有多个元素。
String comparisons are case-insensitive by default, with sort ordering determined by the collation of the current character set, which is 默认情况下,字符串比较不区分大小写,排序顺序由当前字符集的排序规则决定,默认情况下为utf8mb4
by default. utf8mb4
。To perform case-sensitive comparisons instead, you should declare your columns with the 要执行区分大小写的比较,您应该使用BINARY
attribute or use the BINARY
cast, which causes comparisons to be done using the underlying character code values rather than a lexical ordering.BINARY
属性声明列,或者使用BINARY
强制转换,这会导致使用基础字符代码值而不是词法排序来进行比较。
The %
operator is a synonym for MOD()
. %
运算符是MOD()
的同义词。That is, 也就是说,
is equivalent to N
% M
MOD(
. N
,M
)N%M
等于MOD(N,M)
。%
is supported for C programmers and for compatibility with PostgreSQL.%
支持C程序员以及与PostgreSQL的兼容性。
The =
, <>
, <=
, <
, >=
, >
, <<
, >>
, <=>
, AND
, OR
, or LIKE
operators may be used in expressions in the output column list (to the left of the FROM
) in SELECT
statements. For example:=
、<>
、<=
、<
、>=
、>
、<<
、>>
、<=>
、AND
、OR
或LIKE
运算符可用于SELECT语句中输出列列表(FROM左侧)的表达式中。例如
mysql> SELECT col1=1 AND col2=2 FROM my_table;
The LAST_INSERT_ID()
function returns the most recent AUTO_INCREMENT
value. See Section 12.16, “Information Functions”.LAST_INSERT_ID()
函数返回最新的AUTO_INCREMENT
值。参见第12.16节,“信息函数”。
允许对数值使用LIKE
is permitted on numeric values.LIKE
。
The REGEXP
and NOT REGEXP
extended regular expression operators.REGEXP
和NOT REGEXP
扩展了正则表达式运算符。
具有一个参数或两个以上参数的CONCAT()
or CHAR()
with one argument or more than two arguments. (In MySQL Server, these functions can take a variable number of arguments.)CONCAT()
或CHAR()
。(在MySQL Server中,这些函数可以采用可变数量的参数。)
The BIT_COUNT()
, CASE
, ELT()
, FROM_DAYS()
, FORMAT()
, IF()
, MD5()
, PERIOD_ADD()
, PERIOD_DIFF()
, TO_DAYS()
, and WEEKDAY()
functions.BIT_COUNT()
、CASE
、ELT()
、FROM_DAYS()
、FORMAT()
、IF()
、MD5()
,PERIOD_ADD()
、PERIOD_DIFF()
、TO_DAYS()
和WEEKDAY()
函数。
Use of 使用TRIM()
to trim substrings. Standard SQL supports removal of single characters only.TRIM()
修剪子字符串。标准SQL仅支持删除单个字符。
The GROUP BY
functions STD()
, BIT_OR()
, BIT_AND()
, BIT_XOR()
, and GROUP_CONCAT()
. GROUP BY
函数STD()
、BIT_OR()
、BIT_AND()
、BIT_XOR()
和GROUP_CONNCT()
。See Section 12.20, “Aggregate Functions”.请参阅第12.20节,“聚合函数”。