Home Database Mysql Tutorial mysql insert 语法_MySQL

mysql insert 语法_MySQL

Jun 01, 2016 pm 01:51 PM
grammar

INSERT语法INSERT [LOW_PRIORITY |DELAYED| HIGH_PRIORITY] [IGNORE]       [INTO]tbl_name[(col_name,...)]       VALUES ({expr| DEFAULT},...),(...),...       [ON DUPLICATE KEY UPDATEcol_name=expr, ... ]或:INSERT [LOW_PRIORITY |DELAYED| HIGH_PRIORITY] [IGNORE]       [INTO]tbl_name       SETcol_name={expr| DEFAULT}, ...       [ON DUPLICATE KEY UPDATEcol_name=expr, ... ]或:INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]       [INTO]tbl_name[(col_name,...)]       SELECT ...       [ON DUPLICATE KEY UPDATEcol_name=expr, ... ] 一、DELAYED的使用

 

使用延迟插入操作DELAYED调节符应用于INSERT和REPLACE语句。当DELAYED插入操作到达的时候,服务器把数据行放入一个队列中,并立即给客户端返回一个状态信息,这样客户端就可以在数据表被真正地插入记录之前继续进行操作了。如果读取者从该数据表中读取数据,队列中的数据就会被保持着,直到没有读取者为止。接着服务器开始插入延迟数据行(delayed-row)队列中的数据行。在插入操作的同时,服务器还要检查是否有新的读取请求到达和等待。如果有,延迟数据行队列就被挂起,允许读取者继续操作。当没有读取者的时候,服务器再次开始插入延迟的数据行。这个过程一直进行,直到队列空了为止。几点要注意事项:· INSERT DELAYED应该仅用于指定值清单的INSERT语句。服务器忽略用于INSERT DELAYED...SELECT语句的DELAYED。· 服务器忽略用于INSERT DELAYED...ON DUPLICATE UPDATE语句的DELAYED。· 因为在行被插入前,语句立刻返回,所以您不能使用LAST_INSERT_ID()来获取AUTO_INCREMENT值。AUTO_INCREMENT值可能由语句生成。· 对于SELECT语句,DELAYED行不可见,直到这些行确实被插入了为止。· DELAYED在从属复制服务器中被忽略了,因为DELAYED不会在从属服务器中产生与主服务器不一样的数据。注意,目前在队列中的各行只保存在存储器中,直到它们被插入到表中为止。这意味着,如果您强行中止了mysqld(例如,使用kill -9)或者如果mysqld意外停止,则所有没有被写入磁盘的行都会丢失。

 

二、IGNORE的使用

 

IGNORE是MySQL相对于标准SQL的扩展。如果在新表中有重复关键字,或者当STRICT模式启动后出现警告,则使用IGNORE控制ALTER TABLE的运行。如果没有指定IGNORE,当重复关键字错误发生时,复制操作被放弃,返回前一步骤。如果指定了IGNORE,则对于有重复关键字的行,只使用第一行,其它有冲突的行被删除。并且,对错误值进行修正,使之尽量接近正确值。insert ignore into tb(...) value(...)这样不用校验是否存在了,有则忽略,无则添加

 

三、ON DUPLICATE KEY UPDATE的使用

 

如果您指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则执行旧行UPDATE。例如,如果列a被定义为UNIQUE,并且包含值1,则以下两个语句具有相同的效果:mysql>INSERT INTO table (a,b,c) VALUES (1,2,3)       ->ON DUPLICATE KEY UPDATE c=c+1; mysql>UPDATE table SET c=c+1 WHERE a=1;如果行作为新记录被插入,则受影响行的值为1;如果原有的记录被更新,则受影响行的值为2。注释:如果列b也是唯一列,则INSERT与此UPDATE语句相当:mysql>UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;如果a=1 OR b=2与多个行向匹配,则只有一个行被更新。通常,您应该尽量避免对带有多个唯一关键字的表使用ON DUPLICATE KEY子句。您可以在UPDATE子句中使用VALUES(col_name)函数从INSERT...UPDATE语句的INSERT部分引用列值。换句话说,如果没有发生重复关键字冲突,则UPDATE子句中的VALUES(col_name)可以引用被插入的col_name的值。本函数特别适用于多行插入。VALUES()函数只在INSERT...UPDATE语句中有意义,其它时候会返回NULL。示例:mysql>INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)       ->ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);本语句与以下两个语句作用相同:mysql>INSERT INTO table (a,b,c) VALUES (1,2,3)       ->ON DUPLICATE KEY UPDATE c=3;mysql>INSERT INTO table (a,b,c) VALUES (4,5,6)       ->ON DUPLICATE KEY UPDATE c=9;当您使用ON DUPLICATE KEY UPDATE时,DELAYED选项被忽略。 总结:DELAYED 做为快速插入,并不是很关心失效性,提高插入性能。        ignore     只关注主键对应记录是不存在,无则添加,有则忽略。     ON DUPLICATE KEY UPDATE 在添加时操作,关注非主键列,注意与ignore的区别。有则更新指定列,无则添加。 
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to quickly turn your Python code into an API How to quickly turn your Python code into an API Apr 14, 2023 pm 06:28 PM

When it comes to API development, you may think of DjangoRESTFramework, Flask, and FastAPI. Yes, they can be used to write APIs. However, the framework shared today allows you to convert existing functions into APIs faster. It is Sanic . Introduction to Sanic Sanic[1] is a Python3.7+ web server and web framework designed to improve performance. It allows the use of the async/await syntax added in Python 3.5, which can effectively avoid blocking and improve response speed. Sanic is committed to providing a simple and fast way to create and launch

New type alias syntax in PHP8.0 New type alias syntax in PHP8.0 May 14, 2023 pm 02:21 PM

With the release of PHP 8.0, a new type alias syntax has been added, making it easier to use custom types. In this article, we'll take a closer look at this new syntax and its impact on developers. What is a type alias? In PHP, a type alias is essentially a variable that references the name of another type. This variable can be used like any other type and declared anywhere in the code. The main function of this syntax is to define custom aliases for commonly used types, making the code easier to read and understand.

What are the syntax and structure characteristics of lambda expressions? What are the syntax and structure characteristics of lambda expressions? Apr 25, 2024 pm 01:12 PM

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

The connection and difference between Go language and JS The connection and difference between Go language and JS Mar 29, 2024 am 11:15 AM

The connection and difference between Go language and JS Go language (also known as Golang) and JavaScript (JS) are currently popular programming languages. They are related in some aspects and have obvious differences in other aspects. This article will explore the connections and differences between the Go language and JavaScript, and provide specific code examples to help readers better understand these two programming languages. Connection: Both Go language and JavaScript are cross-platform and can run on different operating systems.

Parent class calling syntax in PHP8.0 Parent class calling syntax in PHP8.0 May 14, 2023 pm 01:00 PM

PHP is a server-side scripting language widely used in Web development, and PHP8.0 version introduces a new parent class calling syntax to make object-oriented programming more convenient and concise. In PHP, we can create a parent class and one or more subclasses through inheritance. Subclasses can inherit the properties and methods of the parent class, and can modify or extend their functionality by overriding the methods of the parent class. In ordinary PHP inheritance, if we want to call the method of the parent class in the subclass, we need to use the parent keyword to refer to the parent

Learn the basic syntax of using CSS selectors Learn the basic syntax of using CSS selectors Jan 13, 2024 am 11:44 AM

To master basic CSS selector syntax, specific code examples are required. CSS selectors are a very important part of front-end development. They can be used to select and modify various elements of HTML documents. Mastering basic CSS selector syntax is crucial to writing efficient stylesheets. This article will introduce some common CSS selectors and corresponding code examples. Element selector The element selector is the most basic selector, which can select the corresponding element by its tag name. For example, to select all paragraphs (p elements), you can use

What is the difference between C and C++? What is the difference between C and C++? Aug 29, 2023 pm 11:53 PM

The C programming language C is a general-purpose, high-level language originally developed by Dennis M. Ritchie at Bell Labs to develop the UNIX operating system. C was first implemented in 1972 on the DECPDP-11 computer. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, C compiler, and almost all UNIX applications are written in C. For various reasons, C language has now become a widely used professional language. It is a structured language that is easy to learn, it produces efficient programs, it can handle low-level activities, and it can run on a variety of computers.

The usage and syntax of exponentiation operation in C language The usage and syntax of exponentiation operation in C language Feb 18, 2024 pm 04:05 PM

Introduction to the syntax and usage of power operation in C language: In C language, power operation (poweroperation) is a common mathematical operation, which is used to calculate the power of a number. In C language, we can use standard library functions or custom functions to implement exponentiation operations. This article will introduce the syntax and usage of exponentiation operation in C language in detail, and provide specific code examples. 1. Use the pow() function in math.h. In C language, the pow() function is provided in the math.h standard library for executing

See all articles