1.9.4. MySQL Extensions to Standard SQL
MySQL Server supports some extensions that you probably won't find in other SQL DBMSs. Be warned that if you use them, your code won't be 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 */ In this case, MySQL Server parses and executes the code within the comment as it would any other SQL statement, but other SQL servers will ignore the extensions. For example, MySQL Server recognizes the STRAIGHT_JOIN keyword in the following statement, but other servers will not: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. The TEMPORARY keyword in the following comment is executed only by servers from MySQL 3.23.02 or higher:CREATE /*!32302 TEMPORARY */ TABLE t (a INT);
The following descriptions list MySQL extensions, organized by category.
-
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 filenames in the database directory. This has a few implications:
Database and table names are case sensitive in MySQL Server on operating systems that have case-sensitive filenames (such as most Unix systems).
You can use standard system commands to back up, rename, move, delete, and copy tables that are managed by the
MyISAMstorage engine. For example, it is possible to rename aMyISAMtable by renaming the.MYD,.MYI, and.frmfiles to which the table corresponds. (Nevertheless, it is preferable to useRENAME TABLEorALTER TABLE ... RENAMEand let the server rename the files.)
Database and table names cannot contain pathname separator characters (‘
/’, ‘\’). -
General language syntax
By default, strings can be enclosed by either ‘
"’ or ‘'’, not just by ‘'’. (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.In SQL statements, you can access tables from different databases with the
db_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 statement syntax
The
ANALYZE TABLE,CHECK TABLE,OPTIMIZE TABLE, andREPAIR TABLEstatements.The
CREATE DATABASE,DROP DATABASE, andALTER DATABASEstatements.The
DOstatement.EXPLAIN SELECTto obtain a description of how tables are processed by the query optimizer.The
FLUSHandRESETstatements.The
SETstatement.The
SHOWstatement. As of MySQL 5.0, the information produced by many of the MySQL-specificSHOWstatements can be obtained in more standard fashion by usingSELECTto queryINFORMATION_SCHEMA.Use of
LOAD DATA INFILE. In many cases, this syntax is compatible with Oracle'sLOAD DATA INFILE.Use of
RENAME TABLE.Use of
REPLACEinstead ofDELETEplusINSERT.Use of
CHANGE,col_nameDROP, orcol_nameDROP INDEX,IGNOREorRENAMEinALTER TABLEstatements. Use of multipleADD,ALTER,DROP, orCHANGEclauses in anALTER TABLEstatement.Use of index names, indexes on a prefix of a column, and use of
INDEXorKEYinCREATE TABLEstatements.Use of
TEMPORARYorIF NOT EXISTSwithCREATE TABLE.Use of
IF EXISTSwithDROP TABLEandDROP DATABASE.The capability of dropping multiple tables with a single
DROP TABLEstatement.The
ORDER BYandLIMITclauses of theUPDATEandDELETEstatements.INSERT INTOsyntax.tbl_nameSETcol_name= ...The
DELAYEDclause of theINSERTandREPLACEstatements.The
LOW_PRIORITYclause of theINSERT,REPLACE,DELETE, andUPDATEstatements.Options such as
STRAIGHT_JOINorSQL_SMALL_RESULTinSELECTstatements.You don't need to name all selected columns in the
GROUP BYclause. This gives better performance for some very specific, but quite normal queries.You can specify
ASCandDESCwithGROUP BY, not just withORDER BY.-
The ability to set variables in a statement with the
:=assignment operator:mysql>
SELECT @a:=SUM(total),@b:=COUNT(*),@a/@b AS avg
->FROM test_table;
mysql>SELECT @t1:=(@t2:=1)+@t3:=4,@t1,@t2,@t3;
-
Data types
The
MEDIUMINT,SET, andENUMdata types, and the variousBLOBandTEXTdata types.The
AUTO_INCREMENT,BINARY,NULL,UNSIGNED, andZEROFILLdata type attributes.
-
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.
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's easy to convert use of the||operator to MySQL Server.Use of
COUNT(DISTINCTwherevalue_list)value_listhas more than one element.String comparisons are case-insensitive by default, with sort ordering determined by the collation of the current character set, which is
latin1(cp1252 West European) by default. If you don't like this, you should declare your columns with theBINARYattribute or use theBINARYcast, which causes comparisons to be done using the underlying character code values rather then a lexical ordering.The
%operator is a synonym forMOD(). That is,is equivalent toN%MMOD(.N,M)%is supported for C programmers and for compatibility with PostgreSQL.-
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; The
LAST_INSERT_ID()function returns the most recentAUTO_INCREMENTvalue.LIKEis allowed on numeric values.The
REGEXPandNOT REGEXPextended regular expression operators.CONCAT()orCHAR()with one argument or more than two arguments. (In MySQL Server, these functions can take a variable number of arguments.)The
BIT_COUNT(),CASE,ELT(),FROM_DAYS(),FORMAT(),IF(),PASSWORD(),ENCRYPT(),MD5(),ENCODE(),DECODE(),PERIOD_ADD(),PERIOD_DIFF(),TO_DAYS(), andWEEKDAY()functions.Use of
TRIM()to trim substrings. Standard SQL supports removal of single characters only.The
GROUP BYfunctionsSTD(),BIT_OR(),BIT_AND(),BIT_XOR(), andGROUP_CONCAT(). See Section 12.10, “Functions and Modifiers for Use withGROUP BYClauses”.
For a prioritized list indicating when new extensions are added to MySQL Server, you should consult the online MySQL development roadmap at http://dev.mysql.com/doc/mysql/en/roadmap.html.
Labels: dev.mysql
0 Comments:
Post a Comment
<< Home