MySQL Server 支持某些您在其他 SQL DBMS 中不太可能找到的扩展。请注意,如果您使用它们,您的代码很可能无法移植到其他 SQL 服务器。在某些情况下,您可以通过使用以下形式的注释来编写包含 MySQL 扩展但仍然可移植的代码:MySQL Server supports some extensions that you are not likely to find in other SQL DBMSs. Be warned that if you use them, your code is most likely not portable to other SQL servers. In some cases, you can write code that includes MySQL extensions, but is still portable, by using comments of the following form:
/*! MySQL-specific code */
在这种情况下,MySQL Server 会像解析任何其他 SQL 语句一样解析并执行注释中的代码,但其他 SQL 服务器应忽略扩展。例如,MySQL 服务器可以识别以下语句中的 STRAIGHT_JOIN 关键字,但其他服务器则不能: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. For example, MySQL Server
recognizes the STRAIGHT_JOIN keyword in the
following statement, but other servers should not:
SELECT /*! STRAIGHT_JOIN */ col1 FROM table1,table2 WHERE ...
如果在!字符后添加版本号,则仅当 MySQL 版本大于或等于指定的版本号时才执行注释内的语法。以下注释中的 KEY_BLOCK_SIZE 子句仅由 MySQL 5.1.10 或更高版本的服务器执行: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. The KEY_BLOCK_SIZE clause in the
following comment is executed only by servers from MySQL 5.1.10
or higher:
CREATE TABLE t1(a INT, KEY (a)) /*!50110 KEY_BLOCK_SIZE=1024 */;
以下描述按类别列出了 MySQL 扩展。The following descriptions list MySQL extensions, organized by category.
磁盘上数据的组织Organization of data on disk
MySQL 服务器将每个数据库映射到 MySQL 数据目录下的目录,并将数据库内的表映射到数据库目录中的文件名。因此,在文件名区分大小写的操作系统(例如大多数 Unix 系统)上的 MySQL Server 中,数据库和表名称区分大小写。请参阅第 11.2.3 节“标识符区分大小写”。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). See Section 11.2.3, “Identifier Case Sensitivity”.
通用语言语法General language syntax
默认情况下,字符串可以用
"以及'括起来。如果ANSI_QUOTES启用 SQL 模式,字符串只能用'括起来,服务器将"括起来的字符串解释为标识符。By default, strings can be enclosed by"as well as'. If theANSI_QUOTESSQL mode is enabled, strings can be enclosed only by'and the server interprets strings enclosed by"as identifiers.\是字符串中的转义字符。\is the escape character in strings.在 SQL 语句中,您可以使用
db_name.tbl_name语法访问不同数据库中的表。某些 SQL 服务器提供相同的功能,但称之为User space。 MySQL Server 不支持诸如以下语句中使用的表空间:CREATE TABLE ralph.my_table ... IN my_tablespace。In SQL statements, you can access tables from different databases with thedb_name.tbl_namesyntax. Some SQL servers provide the same functionality but call thisUser space. MySQL Server doesn't support tablespaces such as used in statements like this:CREATE TABLE ralph.my_table ... IN my_tablespace.
SQL 语句语法SQL statement syntax
ANALYZE TABLE,CHECK TABLE、OPTIMIZE TABLE和REPAIR TABLE语句。TheANALYZE TABLE,CHECK TABLE,OPTIMIZE TABLE, andREPAIR TABLEstatements.CREATE DATABASE、DROP DATABASE和ALTER DATABASE语句。请参阅第 15.1.12 节“CREATE DATABASE 语句”、第 15.1.24 节“DROP DATABASE 语句”和第 15.1.2 节“ALTER DATABASE 语句”。TheCREATE DATABASE,DROP DATABASE, andALTER DATABASEstatements. See Section 15.1.12, “CREATE DATABASE Statement”, Section 15.1.24, “DROP DATABASE Statement”, and Section 15.1.2, “ALTER DATABASE Statement”.EXPLAIN SELECT获取查询优化器如何处理表的说明。EXPLAIN SELECTto obtain a description of how tables are processed by the query optimizer.SET语句。请参阅第 15.7.6.1 节“SET 变量赋值语法”。TheSETstatement. See Section 15.7.6.1, “SET Syntax for Variable Assignment”.该
SHOW语句。参见第 15.7.7 节,“SHOW 语句”。由许多 MySQL 特定的SHOW可以通过使用更标准的方式获得语句SELECT来查询INFORMATION_SCHEMA。参见第 28 章,INFORMATION_SCHEMA 表.TheSHOWstatement. See Section 15.7.7, “SHOW Statements”. The information produced by many of the MySQL-specificSHOWstatements can be obtained in more standard fashion by usingSELECTto queryINFORMATION_SCHEMA. See Chapter 28, INFORMATION_SCHEMA Tables.LOAD DATA。在许多情况下,此语法与 OracleLOAD DATA兼容。请参阅第 15.2.9 节“LOAD DATA 语句”。 Use ofLOAD DATA. In many cases, this syntax is compatible with OracleLOAD DATA. See Section 15.2.9, “LOAD DATA Statement”.使用
RENAME TABLE。请参阅第 15.1.36 节“RENAME TABLE 语句”。Use ofRENAME TABLE. See Section 15.1.36, “RENAME TABLE Statement”.使用
REPLACE代替DELETE加上INSERT。请参阅第 15.2.12 节“REPLACE 语句”。Use ofREPLACEinstead ofDELETEplusINSERT. See Section 15.2.12, “REPLACE Statement”.使用
CHANGE col_name、DROP col_name或DROP INDEX、IGNORE或RENAME。在ALTER TABLE语句中使用多个ADD、ALTER、DROP或CHANGE子句。请参阅第 15.1.9 节“ALTER TABLE 语句”。Use ofCHANGE,col_nameDROP, orcol_nameDROP INDEX,IGNOREorRENAMEinALTER TABLEstatements. Use of multipleADD,ALTER,DROP, orCHANGEclauses in anALTER TABLEstatement. See Section 15.1.9, “ALTER TABLE Statement”.Use of index names, indexes on a prefix of a column, and use of
INDEX或者KEY在CREATE TABLEstatements. See Section 15.1.20, “CREATE TABLE Statement”.Use of index names, indexes on a prefix of a column, and use ofINDEXorKEYinCREATE TABLEstatements. See Section 15.1.20, “CREATE TABLE Statement”.将
TEMPORARY或IF NOT EXISTS与CREATE TABLE。Use ofTEMPORARYorIF NOT EXISTSwithCREATE TABLE.将
IF EXISTS与DROP TABLE和DROP DATABASE。Use ofIF EXISTSwithDROP TABLEandDROP DATABASE.使用单个
DROP TABLE语句删除多个表的功能。The capability of dropping multiple tables with a singleDROP TABLEstatement.ORDER BY和LIMIT子句。TheORDER BYandLIMITclauses of theUPDATEandDELETEstatements.INSERT INTO tbl_name SET col_name = ...语法。INSERT INTOsyntax.tbl_nameSETcol_name= ...该
DELAYED子句的INSERT以及REPLACE语句。TheDELAYEDclause of theINSERTandREPLACEstatements.该
LOW_PRIORITY子句的INSERT,REPLACE,DELETE,以及UPDATE语句。TheLOW_PRIORITYclause of theINSERT,REPLACE,DELETE, andUPDATEstatements.在
SELECT语句中使用INTO OUTFILE或INTO DUMPFILE。请参阅第 15.2.13 节“SELECT 语句”。Use ofINTO OUTFILEorINTO DUMPFILEinSELECTstatements. See Section 15.2.13, “SELECT Statement”.Options such as
STRAIGHT_JOIN或者SQL_SMALL_RESULT在SELECT语句。Options such asSTRAIGHT_JOINorSQL_SMALL_RESULTinSELECTstatements.您无需在
GROUP BY子句中命名所有选定的列。这为一些非常具体但非常正常的查询提供了更好的性能。请参阅第 14.19 节“聚合函数”。You don't need to name all selected columns in theGROUP BYclause. This gives better performance for some very specific, but quite normal queries. See Section 14.19, “Aggregate Functions”.您可以使用
ASC指定DESC和GROUP BY,而不仅仅是使用ORDER BY。You can specifyASCandDESCwithGROUP BY, not just withORDER BY.能够使用
:=赋值运算符在语句中设置变量。请参阅第 11.4 节“用户定义的变量”。The ability to set variables in a statement with the:=assignment operator. See Section 11.4, “User-Defined Variables”.
数据类型Data types
函数和运算符Functions and operators
为了方便从其他 SQL 环境迁移的用户,MySQL Server 支持许多函数的别名。例如,所有字符串函数都支持标准 SQL 语法和 ODBC 语法。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.
MySQL Server 将
||和&&运算符理解为逻辑 OR 和 AND,就像在 C 编程语言中一样。在 MySQL Server 中,||和OR是同义词,&&和AND。由于这种良好的语法,MySQL Server 不支持用于字符串连接的标准 SQL||运算符;使用CONCAT()代替。由于CONCAT()可以接受任意数量的参数,因此可以轻松地将||运算符的使用转换为 MySQL Server。MySQL Server understands the||and&&operators to mean logical OR and AND, as in the C programming language. In MySQL Server,||andORare synonyms, as are&&andAND. Because of this nice syntax, MySQL Server doesn't support the standard SQL||operator for string concatenation; useCONCAT()instead. BecauseCONCAT()takes any number of arguments, it is easy to convert use of the||operator to MySQL Server.COUNT(DISTINCT value_list),其中value_list包含多个元素。Use ofCOUNT(DISTINCTwherevalue_list)value_listhas more than one element.字符串比较默认是不区分大小写的,排序顺序由当前字符集的校对规则决定,默认情况下是
utf8mb4。要执行区分大小写的比较,应该在列声明中使用BINARY属性或使用BINARY类型转换,比较将使用底层字符代码值而非字典顺序进行。String comparisons are case-insensitive by default, with sort ordering determined by the collation of the current character set, which isutf8mb4by default. To perform case-sensitive comparisons instead, you should declare your columns with theBINARYattribute or use theBINARYcast, which causes comparisons to be done using the underlying character code values rather than a lexical ordering.%运算符是MOD()的同义词。也就是说,N % M相当于MOD(。N,M)%支持 C 程序员并与 PostgreSQL 兼容。 The%operator is a synonym forMOD(). That is,is equivalent toN%MMOD(.N,M)%is supported for C programmers and for compatibility with PostgreSQL.=、<>、<=、<、>=、>、<<、>>、<=>、AND、OR或LIKE运算符可以用在SELECT语句中输出列列表(FROM左侧)的表达式中。例如:The=,<>,<=,<,>=,>,<<,>>,<=>,AND,OR, orLIKEoperators may be used in expressions in the output column list (to the left of theFROM) inSELECTstatements. For example:mysql>
SELECT col1=1 AND col2=2 FROM my_table;LAST_INSERT_ID()函数返回最新的AUTO_INCREMENT值。请参阅第 14.15 节“信息函数”。TheLAST_INSERT_ID()function returns the most recentAUTO_INCREMENTvalue. See Section 14.15, “Information Functions”.REGEXP和NOT REGEXP扩展正则表达式运算符。TheREGEXPandNOT REGEXPextended regular expression operators.CONCAT()或CHAR()具有一个参数或两个以上参数。 (在 MySQL 服务器中,这些函数可以采用可变数量的参数。)CONCAT()orCHAR()with one argument or more than two arguments. (In MySQL Server, these functions can take a variable number of arguments.)BIT_COUNT()、CASE、ELT()、FROM_DAYS()、FORMAT()、IF()、MD5()、PERIOD_ADD()、PERIOD_DIFF()、TO_DAYS()和WEEKDAY()函数。TheBIT_COUNT(),CASE,ELT(),FROM_DAYS(),FORMAT(),IF(),MD5(),PERIOD_ADD(),PERIOD_DIFF(),TO_DAYS(), andWEEKDAY()functions.使用
TRIM()修剪子字符串。标准 SQL 仅支持删除单个字符。Use ofTRIM()to trim substrings. Standard SQL supports removal of single characters only.GROUP BY函数STD()、BIT_OR()、BIT_AND()、BIT_XOR()和GROUP_CONCAT()。请参阅第 14.19 节“聚合函数”。TheGROUP BYfunctionsSTD(),BIT_OR(),BIT_AND(),BIT_XOR(), andGROUP_CONCAT(). See Section 14.19, “Aggregate Functions”.