Getting MySQL - DML statements articles

Preface: 

In the last article, mainly to introduce a DDL statement with attentive students it may have been found. This article will mainly focus on DML statements, as we explain table data related operations.

Described here under the classification DDL and DML statements, some students may be less clear.
DDL (Data Definition Language): data definition language used to create, delete, modify, or database table structure, the structure or operation of the database table. Common are create, alter, drop and so on.
DML (Data Manipulation Language): a data manipulation language, updating the main record table (add, delete, change). Common are insert, update, delete and so on.

1. Insert Data

The main use is to insert data insert syntax official document also gives a lot of options:

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 [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    SET assignment_list
    [ON DUPLICATE KEY UPDATE assignment_list]

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]

value:
    {expr | DEFAULT}

value_list:
    value [, value] ...

assignment:
    col_name = value

assignment_list:
    assignment [, assignment] ...

Interested students can consult the various options under study above, oh, here I will tell you classify some common syntax.

INSERT INTO ... VALUES (...) 

This could be your written statement to insert the most commonly used, standard usage is:

INSERT INTO <表名> [ <列名1> [ , … <列名n>] ]
VALUES (值1) [… , (值n) ];

#插入多行
INSERT INTO table(column1,column2...)
VALUES (value1,value2,...),
       (value1,value2,...),
...;

Syntax is as follows:

  • &lt;表名&gt; : Specifies the name of the table being operated.
  • &lt;列名&gt; : Specifies the need to insert column names of the data. If the inserted data to all columns in the table, then all column names can be omitted, directly using INSERT <table name> VALUES (...) can be.
  • VALUES Or VALUE clause: This clause contains the list of data to be inserted. Data inventory and order data to the order of the columns, respectively.

INSERT ... SET ... 

insert ... set a statement can only insert data, you can insert the value of some of the columns to the table, this approach is more flexible.

INSERT INTO <表名>
SET <列名1> = <值1>,
    <列名2> = <值2>,
    …
#其中 INTO 可以省略

INSERT INTO ... SELECT ... 

INSERT INTO ... SELECT ... FROM statement is used to quickly retrieve data from one or more tables, and data such as the line data into another table.

SELECT clause returns a query result set to, INSERT statement result sets into the specified table, each row of the result set number of data fields, the data type of the field must be exactly the same table is operated.

For example, if the test table and the table structure test_bak consistent, we want to insert data into the test table test_bak table, so we can operate:

INSERT INTO test_bak select * from test;

INSERT ... ON DUPLICATE KEY UPDATE 

If you want to insert a new row in violation of the primary key (PRIMARY KEY) or UNIQUE constraint, MySQL error, this syntax is to resolve this error. When a record exists in the database, the implementation of this statement will update it when this record does not exist, it will be inserted.

Here's an example to demonstrate the effect of everyone:

#假设student表结构和原始数据如下:
CREATE TABLE `student` (
  `xuehao` int(11) primary key,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

mysql> select * from student;
+--------+------+------+
| xuehao | name | age  |
+--------+------+------+
|   1001 | aaa  |   18 |
|   1002 | bbb  |   19 |
|   1003 | ccc  |   20 |
+--------+------+------+

#比如我们想插入这条数据,MySQL发现主键重复后会执行后面的更新语句
insert into student (xuehao,name,age) 
 values (1003,'ccc',19) on DUPLICATE KEY UPDATE age = 19;

#执行之后发现数据变成了这样
mysql> select * from student;
+--------+------+------+
| xuehao | name | age  |
+--------+------+------+
|   1001 | aaa  |   18 |
|   1002 | bbb  |   19 |
|   1003 | ccc  |   19 |
+--------+------+------+

#即上条语句等效于执行 update student set age = 19 where xuehao = 1003;

REPLACE INTO ... VALUES ... 

replace into similar with the insert function, except that: replace into first attempts to insert data into the table 1. If it is found in the table have this line of data (based on the master key or unique index determination) is to remove this line data, and then insert the new The data. 2. Otherwise, directly into the new data.

Also the described example:

#还是上面那个student表,xuehao是主键 原有数据为
mysql> select * from student;
+--------+------+------+
| xuehao | name | age  |
+--------+------+------+
|   1001 | aaa  |   18 |
|   1002 | bbb  |   19 |
|   1003 | ccc  |   19 |
+--------+------+------+

#如果执行
replace into student values (1003,'ccc',17);

#则新的表数据为
mysql> select * from student;
+--------+------+------+
| xuehao | name | age  |
+--------+------+------+
|   1001 | aaa  |   18 |
|   1002 | bbb  |   19 |
|   1003 | ccc  |   17 |
+--------+------+------+

#效果等同于将xuehao为1003的行删除,然后再插入新行

2. Update Data

update statement to update the table data, the official recommended syntax is:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET assignment_list
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

value:
    {expr | DEFAULT}

assignment:
    col_name = value

assignment_list:
    assignment [, assignment] ...

Similarly, here only to introduce common single table update syntax:

UPDATE <表名> SET 字段 1=值 1 [,字段 2=值 2… ] [WHERE 子句 ]
[ORDER BY 子句] [LIMIT 子句]

Syntax is as follows:

  • &lt;表名&gt;: Used to specify the name of the table to be updated.
  • SET Clause: used to specify the table you want to modify column names and column values. Wherein each column specified value can be an expression, or may be a default value corresponding to the column. If you specify a default value, the keyword DEFAULT available indicates that the column value.
  • WHERE Clause: Optional. For defining rows in the table to be modified. If not specified, all rows in the table is modified.
  • ORDER BY Clause: Optional. Used to define the order of rows in the table are modified.
  • LIMIT Clause: Optional. For defining the number of rows to be modified.

3. Delete Data

delete statement to remove the data table, the official document recommended syntax:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

Using the DELETE statement to delete data from a single table, the syntax is:

DELETE FROM <表名> [WHERE 子句] [ORDER BY 子句] [LIMIT 子句]

Syntax is as follows:

  • &lt;表名&gt;: Specifies the name of the table you want to delete the data.
  • ORDER BY Clause: Optional. When the means to delete, delete rows in each table according to the order specified in the clause.
  • WHERE Clause: Optional. It represents a deletion condition defining a delete operation, if the clause is omitted, delete all the rows represents the table.
  • LIMIT Clause: Optional. Used to inform the server before being returned to the client is the maximum delete rows in the control command.

to sum up: 

This paper describes three DML statements grammar, sounds simple, but a variety of options which is very complex, especially the insert statement, there are many options frequently used. Here we must also remind you that we must be careful when performing update or delete statement, where conditions can not use the update, or delete all data oh.

Guess you like

Origin blog.51cto.com/10814168/2430498