`

Server SQL Modes

阅读更多

5.1.7. Server SQL Modes

The MySQL server can operate in different SQL modes, and can apply these modes differently for different clients. This capability enables each application to tailor the server's operating mode to its own requirements.

For answers to some questions that are often asked about server SQL modes in MySQL, see Section A.3, “MySQL 5.0 FAQ — Server SQL Mode”.

Modes define what SQL syntax MySQL should support and what kind of data validation checks it should perform. This makes it easier to use MySQL in different environments and to use MySQL together with other database servers.

You can set the default SQL mode by starting mysqld with the --sql-mode="modes" option, or by usingsql-mode="modes" in my.cnf (Unix operating systems) or my.ini (Windows). modes is a list of different modes separated by comma (“,”) characters. The default value is empty (no modes set). The modesvalue also can be empty (--sql-mode="" on the command line, or sql-mode="" in my.cnf on Unix systems or in my.ini on Windows) if you want to clear it explicitly.

You can change the SQL mode at runtime by using a SET [GLOBAL|SESSION] sql_mode='modes' statement to set the sql_mode system value. Setting the GLOBAL variable requires the SUPER privilege and affects the operation of all clients that connect from that time on. Setting the SESSION variable affects only the current client. Any client can change its own session sql_mode value at any time.

You can retrieve the current global or session sql_mode value with the following statements:

SELECT @@GLOBAL.sql_mode;
SELECT @@SESSION.sql_mode;

The most important sql_mode values are probably these:

  •  ANSI

    This mode changes syntax and behavior to conform more closely to standard SQL.

  •  STRICT_TRANS_TABLES

    If a value could not be inserted as given into a transactional table, abort the statement. For a non-transactional table, abort the statement if the value occurs in a single-row statement or the first row of a multiple-row statement. More detail is given later in this section. (Implemented in MySQL 5.0.2)

  •  TRADITIONAL

    Make MySQL behave like a “traditional” SQL database system. A simple description of this mode is “give an error instead of a warning” when inserting an incorrect value into a column.

    Note

    The INSERT/UPDATE aborts as soon as the error is noticed. This may not be what you want if you are using a non-transactional storage engine, because data changes made prior to the error may not be rolled back, resulting in a “partially done” update. (Added in MySQL 5.0.2)

When this manual refers to “strict mode,” it means a mode where at least one ofSTRICT_TRANS_TABLES or STRICT_ALL_TABLES is enabled.

The following list describes all supported modes:

  •  ALLOW_INVALID_DATES

    Don't do full checking of dates. Check only that the month is in the range from 1 to 12 and the day is in the range from 1 to 31. This is very convenient for Web applications where you obtain year, month, and day in three different fields and you want to store exactly what the user inserted (without date validation). This mode applies to DATE and DATETIME columns. It does not apply TIMESTAMP columns, which always require a valid date.

    This mode is implemented in MySQL 5.0.2. Before 5.0.2, this was the default MySQL date-handling mode. As of 5.0.2, the server requires that month and day values be legal, and not merely in the range 1 to 12 and 1 to 31, respectively. With strict mode disabled, invalid dates such as '2004-04-31' are converted to '0000-00-00' and a warning is generated. With strict mode enabled, invalid dates generate an error. To allow such dates, enable ALLOW_INVALID_DATES.

  •  ANSI_QUOTES

    Treat “"” as an identifier quote character (like the “`” quote character) and not as a string quote character. You can still use “`” to quote identifiers with this mode enabled. WithANSI_QUOTES enabled, you cannot use double quotes to quote literal strings, because it is interpreted as an identifier.

  •  ERROR_FOR_DIVISION_BY_ZERO

    Produce an error in strict mode (otherwise a warning) when a division by zero (or MOD(X,0)) occurs during an INSERT or UPDATE. If this mode is not enabled, MySQL instead returns NULL for divisions by zero. For INSERT IGNORE or UPDATE IGNORE, MySQL generates a warning for divisions by zero, but the result of the operation is NULL. (Implemented in MySQL 5.0.2)

  •  HIGH_NOT_PRECEDENCE

    From MySQL 5.0.2 on, the precedence of the NOT operator is such that expressions such as NOT a BETWEEN b AND c are parsed as NOT (a BETWEEN b AND c). Before MySQL 5.0.2, the expression is parsed as (NOT a) BETWEEN b AND c. The old higher-precedence behavior can be obtained by enabling the HIGH_NOT_PRECEDENCE SQL mode. (Added in MySQL 5.0.2)

    mysql> SET sql_mode = '';
    mysql> SELECT NOT 1 BETWEEN -5 AND 5;
            -> 0
    mysql> SET sql_mode = 'HIGH_NOT_PRECEDENCE';
    mysql> SELECT NOT 1 BETWEEN -5 AND 5;
            -> 1
    
  •  IGNORE_SPACE

    Allow spaces between a function name and the “(” character. This causes built-in function names to be treated as reserved words. As a result, identifiers that are the same as function names must be quoted as described in Section 8.2, “Schema Object Names”. For example, because there is a COUNT()function, the use of count as a table name in the following statement causes an error:

    mysql> CREATE TABLE count (i INT);
    ERROR 1064 (42000): You have an error in your SQL syntax
    

    The table name should be quoted:

    mysql> CREATE TABLE `count` (i INT);
    Query OK, 0 rows affected (0.00 sec)
    

    The IGNORE_SPACE SQL mode applies to built-in functions, not to user-defined functions or stored functions. It is always allowable to have spaces after a UDF or stored function name, regardless of whether IGNORE_SPACE is enabled.

    For further discussion of IGNORE_SPACE, see Section 8.2.3, “Function Name Parsing and Resolution”.

  •  NO_AUTO_CREATE_USER

    Prevent the GRANT statement from automatically creating new users if it would otherwise do so, unless a non-empty password also is specified. (Added in MySQL 5.0.2)

  •  NO_AUTO_VALUE_ON_ZERO

    NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. NO_AUTO_VALUE_ON_ZEROsuppresses this behavior for 0 so that only NULL generates the next sequence number.

    This mode can be useful if 0 has been stored in a table's AUTO_INCREMENT column. (Storing 0 is not a recommended practice, by the way.) For example, if you dump the table with mysqldump and then reload it, MySQL normally generates new sequence numbers when it encounters the 0 values, resulting in a table with contents different from the one that was dumped. Enabling NO_AUTO_VALUE_ON_ZERO before reloading the dump file solves this problem. mysqldump now automatically includes in its output a statement that enables NO_AUTO_VALUE_ON_ZERO, to avoid this problem.

  •  NO_BACKSLASH_ESCAPES

    Disable the use of the backslash character (“\”) as an escape character within strings. With this mode enabled, backslash becomes an ordinary character like any other. (Implemented in MySQL 5.0.1)

  •  NO_DIR_IN_CREATE

    When creating a table, ignore all INDEX DIRECTORY and DATA DIRECTORY directives. This option is useful on slave replication servers.

  •  NO_ENGINE_SUBSTITUTION

    Control automatic substitution of the default storage engine when a statement such as CREATE TABLE orfont-family: monospace, fixed; font-weight: bold; colo

    评论

相关推荐

    SQLServer表转Model实体类

    支持直连SQLServer数据库:左边窗口选择需要转换的数据库和表,直接在右边窗口实时浏览转换后的实体类代码 支持自定义NameSpace空间:还可以在窗口的右边定义命名空间 支持导出:选择文件保存路径,导出到本机指定...

    SqlServer数据库表生成C# Model实体类 小工具 TableToModel源码

    SqlServer数据库表生成C# Model实体类 小工具 TableToModel源码 具体讲解:https://blog.csdn.net/weixin_38211198/article/details/91127595

    sql server基本图标

    "系統图标"可能是指SQL Server中与系统相关的图标,例如系统数据库(如master、model、msdb和tempdb)的图标,或者是系统对象、系统存储过程和系统视图的表示。 4. **开发文档** 对于开发人员来说,了解这些图标...

    sql server 数据库 无法获得数据库'model'上的排他锁 解决办法

    sql server 数据库新建数据库时出现异常 无法获得数据库'model'上的排他锁 这是解决办法

    SQL Server 2000系统表地图.rar_sql_sql server_sql server 2000_多变量

    在SQL Server 2000中,系统表分为几大类别,包括信息Schema视图、master数据库中的系统表、model和msdb数据库中的系统表。这些表提供了对数据库对象的深入洞察,如: 1. **信息Schema视图**:这些视图提供了一种...

    Thinkphp 连接 SQL SERVER 类

    然而,当涉及到与非MySQL数据库系统的交互,例如Microsoft SQL Server时,开发者可能会遇到一些挑战。本篇文章将详细探讨如何在ThinkPHP框架中配置和使用SQL Server数据库。 首先,`ThinkPHP 连接 SQL SERVER 类`指...

    协同过滤 sql server DataModel

    在本场景中,"协同过滤 sql server DataModel"指的是将协同过滤算法应用于SQL Server数据库,而原本Mahout库默认只支持MySQL。 Mahout是Apache的一个开源项目,专注于机器学习和数据挖掘,提供了多种推荐算法,包括...

    MFC连接SQL Server数据库

    MFC连接SQL Server数据库 MFC(Microsoft Foundation Classes)是一种基于C++的应用程序框架,广泛应用于Windows平台的桌面应用程序开发。其中一个重要的应用场景是连接数据库,以便进行数据存储和检索。本文将详细...

    c# 基于sqlserver 快速开发工具 三层架构生成工具

    本文将深入探讨基于C#语言,针对SQL Server数据库的三层架构(Model-BLL-DAL)生成工具,以及如何利用这些工具进行高效开发。 首先,"C#"是一种面向对象的编程语言,广泛应用于Windows桌面应用、Web应用及游戏开发...

    SQL Server2000数据库文件损坏时如何恢复

    SQL Server2000数据库文件损坏时如何恢复,SQL Server2000数据库文件损坏时如何恢复

    Microsoft SQL Server 2005 Native Client

    SQL Server Native Client的OLE DB提供程序支持ADO(ActiveX Data Objects),使得基于COM(Component Object Model)的组件如VB6、ASP等能够轻松访问SQL Server。此外,OLE DB提供程序还支持高级查询功能,如分布式...

    sqlserver数据库课件

    【SQL Server 2000】是微软公司推出的一款企业级关系型数据库管理系统,它具有丰富的特性和功能,广泛应用于各种规模的企业。本课件主要介绍了SQL Server 2000的基本概念、安装要求和主要系统数据库。 首先,SQL ...

    Java程序通过JDBC连接SQLServer2000数据库全解.doc

    例如,如果数据库服务器地址为`localhost`,端口号为`1433`,数据库名为`model`,则连接字符串应为`jdbc:microsoft:sqlserver://localhost:1433;databaseName=model`。 3. **执行查询**: 建立连接后,可以通过`...

    SQL server 2000驱动包

    SQL Server 2000驱动包是用于连接到Microsoft SQL Server 2000数据库管理系统的重要组件。在本文中,我们将深入探讨SQL Server 2000的驱动、其功能、以及如何使用这些驱动来建立数据库连接。 首先,SQL Server 2000...

    sqlserver2014做数据还原

    - **恢复模型(Recovery Model)**:SQL Server支持三种恢复模型:简单(Simple)、完整(Full)和大容量日志(Bulk-Logged)。不同的恢复模型支持不同类型的备份策略。 #### 二、SQL Server 2014 数据库还原的方法...

    sql server 2005安装截图

    安装完成后,SQL Server 2005会生成一个默认的系统数据库,包括Master、Model、Msdb和TempDB。Master数据库存储了SQL Server的所有系统信息,如登录信息、数据库列表等;Model是新数据库的模板;Msdb用于调度作业、...

    【SQL-Server数据库】-SQL-Server关系数据库管理系统.ppt

    2. 系统数据库和用户数据库:系统数据库由SQL Server自动创建和维护,包括Master、Model、Msdb和TempDB等,用户数据库则是根据需求创建的,存储用户数据。 **数据库对象** 1. 表和视图:表是基本的数据存储单元,由...

    SpringMVC+Mybatis+SQLServer整合源码 含数据库文件

    SpringMVC、Mybatis和SQLServer是Java Web开发中常见的技术栈,它们分别负责不同的职责。SpringMVC作为Spring框架的一部分,是用于构建Web应用程序的模型-视图-控制器(MVC)架构。Mybatis是一个轻量级的持久层框架...

    使用PowerDesigner设计SQL Server数据库

    ### 使用PowerDesigner设计SQL Server数据库知识点详解 #### 一、PowerDesigner概述 - **PowerDesigner**是一款由SAP公司开发的强大的数据建模工具,它能够帮助开发者进行数据库设计、元数据管理和数据仓库构建等...

    SQL Server Native Client 客户端

    SQL Server Native Client(简称SQLNCLI)是微软推出的一个专门用于与SQL Server交互的客户端库,它是SQL Server 2005引入的新特性,旨在提供更高效、更安全的数据库访问方式。SQLNCLI集成了ODBC(Open Database ...

Global site tag (gtag.js) - Google Analytics