Detailed explanation of MySQL Strict Mode knowledge points

Detailed explanation of MySQL Strict Mode knowledge points

I. Strict Mode Explanation

According to the restrictions of strict mode (STRICT_TRANS_TABLES) in MySQL 5.0 and above:

1). Does not support inserting null values ​​into not null fields

2). It does not support inserting '' value into the auto-increment field, but null value can be inserted

3). Does not support default values ​​for text fields

Look at the following code: (the first field is an auto-increment field)

Sql code

$query="insert into demo values('','$firstname','$lastname','$sex')";

The above code is only valid in non-strict mode.

Code

$query="insert into demo values(NULL,'$firstname','$lastname','$sex')";

The above code is only valid in strict mode. Replace the empty value '' with NULL.

II. Make the database support Strict Mode

1. Make the following improvements to the database structure to support strict mode:

1) Set a non-null default value for all not null fields. The default value for strings is '', the default value for numbers is 0, and the default value for dates is '0000-00-00 00:00:00'

2) Remove the default value of the text field

3) Standardization improvements: Change the title field to varchar(255) and change the null field with default value to not null field

2. If the installed PHP program database structure turns off Strict mode

1). One is to remove strict mode when installing MySQL 5.0 (or above).

Edit my.cnf and turn off Strict Mode:

sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

2). Another way is to modify the query statement. For example,

if ($this->dbcharset) {
@mysql_query("SET NAMES ".$this->dbcharset);
}

Execute later

mysql_query("SET @@sql_mode = ''");

Make sure you are using MySQL 5.

The mysqli method is similar, that is, the execution is

mysqli_query($this->connection_id, "SET @@sql_mode = ''");

This is the end of this article about the detailed explanation of MySQL Strict Mode knowledge points. For more relevant MySQL Strict Mode content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of sql_mode mode example in MySQL
  • Django2 connects to MySQL and model test example analysis
  • Detailed explanation on reasonable settings of MySQL sql_mode
  • MySQL sql_mode analysis and setting explanation
  • Detailed explanation of viewing and setting SQL Mode in MySQL

<<:  js uses the reduce method to make your code more elegant

>>:  The most convenient way to build a Zookeeper server in history (recommended)

Recommend

W3C Tutorial (8): W3C XML Schema Activities

XML Schema is an XML-based alternative to DTD. XM...

Solve the problem of ifconfig being unavailable in docker

Recently, when I was learning docker, I found tha...

HTML Tutorial: HTML horizontal line segment

<br />This tag can display a horizontal line...

Detailed explanation of Vue data proxy

Table of contents 1. What I am going to talk abou...

A designer complains about Hammer's official website again

Last year, the open letter was a huge hit, even a...

Linux completely removes node.js and reinstalls it through the yum command

first step Delete it once with the built-in packa...

Using js to implement the two-way binding function of data in Vue2.0

Object.defineProperty Understanding grammar: Obje...

Mysql uses stored procedures to quickly add millions of data sample code

Preface In order to reflect the difference betwee...

How to use shell to perform batch operations on multiple servers

Table of contents SSH protocol SSH Connection pro...

Analysis of Hyper-V installation CentOS 8 problem

CentOS 8 has been released for a long time. As so...

Introduction to NFS service construction under Centos7

Table of contents 1. Server 2. Client 3. Testing ...

Monitor changes in MySQL table content and enable MySQL binlog

Preface binlog is a binary log file, which record...

Design Reference Beautiful and Original Blog Design

All blogs listed below are original and uniquely ...

jQuery realizes the shuttle box effect

This article example shares the specific code of ...