MySQL 的 DML 遵守 SQL 的 DML 规范,数据操作语言(Data Manipulation Language),它主要用于添加、删除、更新和查询数据库记录,并检查数据完整性,常用的语句关键字主要包括 insert、delete、udpate 和 select 等。
INSERT 语句
MySQL INSERT 语句主要三种形式,明确指定的值进行插入的 INSERT ... VALUES ... 和 INSERT ... SET ...,还有一种形式是从另一个表或多个表中的查询结果作为插入的值,以 INSERT ... SELECT ... 格式。
INSERT ... VALUES ... 形式
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
INTO tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[(col_name [, col_name] ...)]
{VALUES | VALUE} (value_list) [, (value_list)] ...
[ON DUPLICATE KEY UPDATE assignment_list]
INSERT ... SET ... 形式
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
INTO tbl_name
[PARTITION (partition_name [, partition_name] ...)]
SET assignment_list
[ON DUPLICATE KEY UPDATE assignment_list]
INSERT ... SELECT ... 形式
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
INTO tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[(col_name [, col_name] ...)]
SELECT ...
[ON DUPLICATE KEY UPDATE assignment_list]
INSERT 语句支持如下的修饰符:
- IGNORE
- 顾名思义插入忽略的操作,如果要插入的数据和现有的唯一索引(unique index)或主键(primary key)冲突(已存在),则该插入值操作忽略;若省略 IGNORE 修饰符时,键冲突时,会报失败错误。
- ON DUPLICATE KEY UPDATE
- 和 IGNORE 用法相反,当插入的数据和唯一键值或主键冲突时,进行更新操作,此时返回的受影响的行数为 2;若键值不冲突,则执行普通的插入操作,受影响的行数为 1;还有一种特殊情况是键值冲突进行更新时,若要更新的数据和原有的数据一样,受影响行数返回 0,即第一种的特殊情况。此修饰符的作用,通俗的讲就是“记录存在则更新,不存在则插入”。
关于 MySQL 更多的插入语句详情可以参考 MySQL 插入/添加数据语句(INSERT INTO)用法详解。
DELETE 语句
DELETE 语句用来删除指定表中符合条件的行数据。
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
DELETE 语句返回删除的行数。
关于 MySQL 更多的删除语句详情可以参考 MySQL 删除数据语句(DELETE)用法详解。
UPDATE 语句
UPDATE 语句用来修改指定表的行数据。
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET assignment_list
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
SELECT 语句
SELECT 语句是我们在开发中最常用的语句,用于查询指定表的相应信息。
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr] ...
[into_option]
[FROM table_references
[PARTITION partition_list]]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[into_option]
[FOR UPDATE | LOCK IN SHARE MODE]
REPLACE 语句
REPLACE 语句类似与 INSERT 语句,作用也是插入替换操作,不同的是当插入的值的唯一索引和主键冲突时,它也会做替换更新的操作,但是底层是先做了删除再插入,所以该操作并非原子操作。通俗讲,它的作用是“要么插入,要么删除再插入”。
它也和 INSERT 一样有 3 种形式,分别跟着 VALUES、SET 和 SELECT,具体如下:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[(col_name [, col_name] ...)]
{VALUES | VALUE} (value_list) [, (value_list)] ...
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] ...)]
SET assignment_list
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[(col_name [, col_name] ...)]
SELECT ...
LOAD DATA 语句
LOAD DATA 语句可以非常高效地从文本文件中读取数据一行一行地写入指定表中。
LOAD DATA
[LOW_PRIORITY | CONCURRENT] [LOCAL]
INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[CHARACTER SET charset_name]
[{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number {LINES | ROWS}]
[(col_name_or_user_var
[, col_name_or_user_var] ...)]
[SET col_name={expr | DEFAULT}
[, col_name={expr | DEFAULT}] ...]
LOAD XML 语句
LOAD XML 可以从 xml 格式的文件中读取数据到指定表。
LOAD XML
[LOW_PRIORITY | CONCURRENT] [LOCAL]
INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE [db_name.]tbl_name
[CHARACTER SET charset_name]
[ROWS IDENTIFIED BY '<tagname>']
[IGNORE number {LINES | ROWS}]
[(field_name_or_user_var
[, field_name_or_user_var] ...)]
[SET col_name={expr | DEFAULT},
[, col_name={expr | DEFAULT}] ...]
CALL 语句
CALL 语句用来调用预定义的存储过程。
CALL sp_name([parameter[,...]])
CALL sp_name[()]
DO 语句
DO 语句执行表达式,但不返回任何结果。绝大多数情况下,它是 SELECT expr, ... 的简写方式,且它有执行速度快,不关心返回结果。
DO expr [, expr] ...
HANDLER 语句
HANDLER 语句提供了直接访问表存储引擎的接口,可用于 InnoDB 和 MyISAM 引擎。
HANDLER tbl_name OPEN [ [AS] alias]
HANDLER tbl_name READ index_name { = | <= | >= | < | > } (value1,value2,...)
[ WHERE where_condition ] [LIMIT ... ]
HANDLER tbl_name READ index_name { FIRST | NEXT | PREV | LAST }
[ WHERE where_condition ] [LIMIT ... ]
HANDLER tbl_name READ { FIRST | NEXT }
[ WHERE where_condition ] [LIMIT ... ]
HANDLER tbl_name CLOSE
子查询语句
子查询是 SELECT 语句中有存在 SELECT 子句左右外部查询或嵌套查询等。