1.3. Server SQL Modes


Many operational characteristics of MySQL Server can be configured by setting the SQL mode. This mode consists of optional values that each control some aspect of query processing. By setting the SQL mode appropriately, a client program can instruct the server how strict or forgiving to be about accepting input data, enable or disable behaviors relating to standard SQL conformance, or provide better compatibility with other database systems. This section discusses how to set the SQL mode. It's necessary to understand how to do this because references to the SQL mode occur throughout this study guide.

The SQL mode is controlled by means of the sql_mode system variable. To assign a value to this variable, use a SET statement. The value should be an empty string, or one or more mode names separated by commas. If the value is empty or contains more than one mode name, it must be quoted. If the value contains a single mode name, quoting is optional. SQL mode values are not case sensitive, although this study guide always writes them in uppercase. Here are some examples:

  • Clear the SQL mode:

     SET sql_mode = ''; 

  • Set the SQL mode using a single mode value:

     SET sql_mode = ANSI_QUOTES; SET sql_mode = 'TRADITIONAL'; 

  • Set the SQL mode using multiple mode names:

     SET sql_mode = 'IGNORE_SPACE,ANSI_QUOTES'; SET sql_mode = 'STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO'; 

To check the current sql_mode setting, select its value like this:

 mysql> SELECT @@sql_mode; +----------------------------------------------+ | @@sql_mode                                   | +----------------------------------------------+ | STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO | +----------------------------------------------+ 

Some SQL mode values are composite modes that actually enable a set of modes. Values in this category include ANSI and trADITIONAL. To see which mode values a composite mode consists of, retrieve the value after setting it:

 mysql> SET sql_mode='TRADITIONAL"; Query OK, 0 rows affected (0.07 sec) mysql> SELECT @@sql_mode\G *************************** 1. row *************************** @@sql_mode: STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,             NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,             NO_AUTO_CREATE_USER 1 row in set (0.03 sec) 

The MySQL Reference Manual lists all available SQL mode values. The following list briefly describes some of the values referred to elsewhere in this study guide:

  • ANSI_QUOTES

    This mode causes the double quote character ('"') to be interpreted as an identifier-quoting character rather than as a string-quoting character.

  • IGNORE_SPACE

    By default, functions must be written with no space between the function name and the following parenthesis. Enabling this mode causes the server to ignore spaces after function names. This allows spaces to appear between the name and the parenthesis, but also causes function names to be reserved words.

  • ERROR_FOR_DIVISION_BY_ZERO

    By default, division by zero produces a result of NULL and is not treated specially. Enabling this mode causes division by zero in the context of inserting data into tables to produce a warning, or an error in strict mode.

  • STRICT_TRANS_TABLES, STRICT_ALL_TABLES

    These values enable "strict mode," which imposes certain restrictions on what values are acceptable as database input. By default, MySQL is forgiving about accepting values that are missing, out of range, or malformed. Enabling strict mode causes bad values to be treated as erroneous. STRICT_TRANS_TABLES enables strict mode for transactional tables, and STRICT_ALL_TABLES enables strict mode for all tables.

  • trADITIONAL

    This is a composite mode that enables both strict modes plus several additional restrictions on acceptance of input data.

  • ANSI

    This is a composite mode that causes MySQL Server to be more "ANSI-like." That is, it enables behaviors that are more like standard SQL, such as ANSI_QUOTES (described earlier) and PIPES_AS_CONCAT, which causes || to be treated as the string concatenation operator rather than as logical OR.

Section 5.8, "Handling Missing or Invalid Data Values," provides additional detail about the use of strict and traditional SQL modes for controlling how restrictive the server is about accepting input data.



MySQL 5 Certification Study Guide
MySQL 5.0 Certification Study Guide
ISBN: 0672328127
EAN: 2147483647
Year: 2006
Pages: 312

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net