MYSQL study notes six (data manipulation)

First, insert data (INSERT)
1.1 INSERT syntax
INSERT statement has two forms of syntax, namely INSERT ... VALUES statement and INSERT ... SET statement.

1.1.1 INSERT ... VALUES statement
INSERT INTO <table name> [<1 column name> [, ... <column name n->]]
the VALUES (value 1) [..., (n-value)];

<Table name>: Specifies the name of the table is operated.
<Column name>: Specifies the need to insert the data column name. 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.
VALUE or VALUES clause: This clause contains the list of data to be inserted. Data inventory and order data to the order of the columns, respectively.

1.1.2 INSERT ... SET statement
INSERT INTO <table name>
the SET <1 column name> = <value 1>,
<2 column name> = <value 2>,
...

INSERT two forms follows:

Use INSERT ... VALUES statements may be inserted into the table one row of data may be inserted multiple-line transactions;
using INSERT ... SET statement specified value into the row of each column, the value of some of the columns may be specified;
the INSERT ... the SELECT statement to a table inserting data other tables.
Using INSERT ... SET statement can be inserted into the table of the value part of the column, this method is more flexible;
the INSERT statement ... can be inserted into the VALUES plurality of data once.
1.2 to add value in the table all the fields
to create the test table, the command is as follows:

CREATE TABLE testtable (
ID INT ,
fullname CHAR(40) NOT NULL,
age INT NOT NULL
) ;

eg:
inserting a new record in the table testtable, ID value 1, fullname value "name1", age 18 value.

INSERT INTO testtable
(ID ,fullname ,age )
VALUES(1,'name1',18);

Results are as follows:
MYSQL study notes six (data manipulation)
Note: negligible insert the data column name INSERT a data While, if the value does not contain the name of a column, the back VALUES keyword value requires not only complete, but the same order as the columns, and table definition when necessary. If the table structure is modified, the columns add, delete or change the position of operations that will be inserted such that the order of the data in this way also changed. If you specify a column name, the table will not be affected by structural changes.

Adding 1.3 part of the value field in the table
for the specified data field in the table is inserted, is inserted in the INSERT statement to only part of the field value, and the default value when the value table define other fields.
eg:
inserting a new record in the table testtable, ID value 2, fullname value "name1".

INSERT INTO testtable
(fullname ,age )
VALUES('name1',18);

Results are as follows:
MYSQL study notes six (data manipulation)
1.4 INSERT INTO FROM statement replication data
extracted from one or more data tables, and stores the data into another table.

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

Testtable created with the same table structure of data in a database table testtablenew firstdb, the sql script to create a table as follows:

CREATE TABLE testtablenew (
ID INT ,
fullname CHAR(40) NOT NULL,
age INT NOT NULL
)

Command above results are as follows:
MYSQL study notes six (data manipulation)
Query testtable all records from the table, the table and inserted testtablenew. SQL statements and input the execution result is shown below.
SQL statement is as follows:

INSERT INTO testtablenew
(ID,fullname,age)
SELECT ID,fullname,age
FROM testtable

Performed as follows:
MYSQL study notes six (data manipulation)
two data is updated (UPDATE)
2.1 UPDATE syntax of
the UPDATE statement syntax is:

The UPDATE <table name> = 1 the value of the SET 1 field [, field 2 ... value = 2] [the WHERE clause]
[the ORDER BY clause] [the LIMIT clause]
1
2
<table name>: to update a table designation name.
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.
Note: modifying a plurality of column line of data values, each value SET clause can be separated by commas.

2.2 modify data
in testtablenew table, all rows are updated value of the age field 20

UPDATE testtablenew
SET age=20;

执行结果如下:
MYSQL study notes six (data manipulation)
2.3 根据条件修改数据
在 testtablenew表中,更新 ID值为 1 的记录,将 age字段值改为 15,将 fullname字段值改为“testname”,SQL行结果如下所示。

UPDATE testtablenew
SET fullname='testname',age=15
WHERE ID=1;

运行结果如下:
MYSQL study notes six (data manipulation)
2.4 更新数据时,使用LIMIT控制更新数据的行数。
在 testtablenew表中,将符合条件的前2条数据的fullname字段值改为“001”,SQL行结果如下所示。

UPDATE testtablenew
SET fullname='001'
LIMIT 2

执行结果如下:
MYSQL study notes six (data manipulation)
注意:LIMIT 也可以结合where等子句进行使用。LIMIT 2的意思是对满足条件的前2条数据进行处理。
有时候,会见到LIMIT 1,2的写法,在更新语句中需要注意的是,不能直接这样使用,如果需要使用,可以简单看一下

三、删除数据(DELETE)
3.1 DELETE语法
使用 DELETE 语句删除数据的语法格式为:

DELETE FROM <表名> [WHERE 子句] [ORDER BY 子句] [LIMIT 子句]
1
<表名>:指定要删除数据的表名。
ORDER BY 子句:可选项。表示删除时,表中各行将按照子句中指定的顺序进行删除。
WHERE 子句:可选项。表示为删除操作限定删除条件,若省略该子句,则代表删除该表中的所有行。
LIMIT 子句:可选项。用于告知服务器在控制命令被返回到客户端前被删除行的最大值。

3.2 删除表全部数据
eg:
删除 testtablenew 表中的全部数据, SQL 语句如下:

DELETE FROM testtablenew ;
MYSQL study notes six (data manipulation)
注意:在不使用 WHERE 条件的时候,将删除所有数据。删除数据的时候一定要注意。

3.3 根据条件删除表中数据
在 testtablenew 表中,删除 ID为 2 的记录,在上一步已经将该表数据清空,这里需要先给该表插入几条数据,插入数据命令如下:

INSERT INTO testtablenew( ID ,fullname,age)VALUES(1,'001',18);
INSERT INTO testtablenew( ID ,fullname,age)VALUES(2,'002',18);
INSERT INTO testtablenew( ID ,fullname,age)VALUES(3,'003',18);
INSERT INTO testtablenew( ID ,fullname,age)VALUES(4,'004',18);
INSERT INTO testtablenew( ID ,fullname,age)VALUES(5,'005',18);
INSERT INTO testtablenew( ID ,fullname,age)VALUES(6,'006',18);
INSERT INTO testtablenew( ID ,fullname,age)VALUES(7,'007',18);

数据表数据截图如下:
MYSQL study notes six (data manipulation)
删除上表数据中ID为2的记录,其SQL语句如下:

The FROM the WHERE ID = testtablenew the DELETE 2
. 1
results are as follows:
MYSQL study notes six (data manipulation)
3.4 TRUNCATE
3.4.1 Syntax TRUNCATE
TRUNCATE <table name> ## refers to the table to remove the table
. 1
TRUNCATE role is to delete all the data in the table, not with where use.
eg:
delete all the data testtablenew table, the command is as follows:

Testtablenew TRUNCATE
. 1
Note: If the primary key is testtablenew increment, clear the TRUNCATE table data, clear data not occupy the primary key value.

Guess you like

Origin blog.51cto.com/14525650/2436572