Mysql SQL Mode
一、Mysql SQL Mode简介 MySQL服务器能够工作在不同的SQL模式下,并能针对不同的客户端以不同的方式应用这些模式。这样,应用程序就能对服务器操作进行量身定制以满足自己的需求。这类模式定义了MySQL应支持的SQL语法,以及应该在数据上执行何种确认检查。这
一、Mysql SQL Mode简介 MySQL服务器能够工作在不同的SQL模式下,并能针对不同的客户端以不同的方式应用这些模式。这样,应用程序就能对服务器操作进行量身定制以满足自己的需求。这类模式定义了MySQL应支持的SQL语法,以及应该在数据上执行何种确认检查。这样,就能在众多不同的环境下、与其他数据库服务器一起更容易地使用MySQL。可以使用“--sql-mode="modes"”选项,通过启动mysqld来设置默认的SQL模式。从MySQL 4.1开始,也能在启动之后,使用SET [SESSION|GLOBAL] sqlmode='modes'语句,通过设置sqlmode变量更改模式。
通常在linux下安装完mysql后,默认的sql-mode值是空,在这种情形下mysql执行的是一种不严格的检查,例如日期字段可以插入’0000-00-00 00:00:00’这样的值,还有如果要插入的字段长度超过列定义的长度,那么mysql不会终止操作,而是会自动截断后面的字符继续插入操作,如下例: mysql> create table t5 (c1 char(3)); mysql> insert into t5 values('abcd'); mysql> select * from t5; +------+ | c1 | +------+ | abc | +------+ 1 row in set (0.00 sec) 我们发现插入的字符被自动截断了,但是如果我们本意希望如果长度超过限制就报错,那么我们可以设置sqlmode为STRICTTRANS_TABLES,如下: mysql> set session sqlmode='STRICTTRANS_TABLES' 这样我们再执行同样的操作,mysql就会告诉我们插入的值太长,操作被终止,如下: mysql> insert into t5 values('abcd'); ERROR 1406 (22001): Data too long for column 'c1' at row 1
经常使用的sql_mode值如下: ANSI :更改语法和行为,使其更符合标准SQL。
STRICTTRANSTABLES:如果不能将给定的值插入到事务表中,则放弃该语句。对于非事务表,如果值出现在单行语句或多行语句的第1行,则放弃该语句。
TRADITIONAL:Make MySQL的行为象“传统”SQL数据库系统。该模式的简单描述是当在列中插入不正确的值时“给出错误而不是警告”。注释:一旦发现错误立即放弃INSERT/UPDATE。如果你使用非事务存储引擎,这种方式不是你想要的,因为出现错误前进行的数据更改不会“滚动”,结果是更新“只进行了一部分”。 说明:如果把sqlmode的值设置成后面的两个值(也就是我们说的严格模式),那么当在列中插入或更新不正确的值时,mysql将会给出错误,并且放弃insert/update操作。在我们的一般应用中建议使用这两种模式,而不是使用默认的空或ANSI模式。但是需要注意的问题是,如果数据库运行在严格模式下,并且你的存储引擎不支持事务,那么有数据不一致的风险存在,比如一组sql中有两个dml语句,如果后面的一个出现了问题,但是前面的已经操作成功,那么mysql并不能回滚前面的操作。因此说设置sqlmode需要应用人员权衡各种得失,从而得到一个合适的选择。
Sql_mode的值还有很多,这里不再累述,可以参考相关的手册。
二、SQL Mode与可移植性 如果mysql与其它异构数据库之间有数据移植的需求的话,那么下面的sql_mode的组合设置可以达到相应的效果: 数据库 Sql_mode值 DB2 :PIPESASCONCAT、ANSIQUOTES、IGNORESPACE、NOKEYOPTIONS、NOTABLEOPTIONS、NOFIELDOPTIONS
MAXDB: PIPESASCONCAT、ANSIQUOTES、IGNORESPACE、NOKEYOPTIONS、NOTABLEOPTIONS、NOFIELDOPTIONS、 NOAUTOCREATE_USER
MSSQL: PIPESASCONCAT、ANSIQUOTES、IGNORESPACE、NOKEYOPTIONS、NOTABLEOPTIONS、 NOFIELDOPTIONS
ORACLE: PIPESASCONCAT、ANSIQUOTES、IGNORESPACE、NOKEYOPTIONS、NOTABLEOPTIONS、NOFIELDOPTIONS、NOAUTOCREATE_USER
POSTGRESQL: PIPESASCONCAT、ANSIQUOTES、IGNORESPACE、NOKEYOPTIONS、NOTABLEOPTIONS、NOFIELDOPTIONS
三、SQL Mode与数据效验 SQL Mode 还可以实现对数据效验和转移等功能如: ①效验日期数据合法性. ②在INSERT或UPDATE过程中,如果被零除(或MOD(X,0)),则产生错误 ③将‘"’视为识别符引号(‘`’引号字符) ④禁用反斜线字符(‘\’)做为字符串内的退出字符。启用NOBACKSLASHESCAPES模式,反斜线则成为普通字符。 ⑤将||视为字符串连接操作符(+)(同CONCAT()),而不视为OR。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
