Language DML (Data Manipulation Language)

#DML language
/ *
Data Manipulation Language:
insert: insert
modified: update
delete: delete

*/

# First, insert statements
# One way: classic insert
/ *
Syntax:
INSERT INTO table name (column name, ...) values (values 1, ...);

* /
The SELECT * the FROM Beauty;
. #. 1 type of the value is inserted to be consistent or compatible with the type column of
the INSERT the INTO Beauty (ID, NAME, Sex, borndate, Phone, Photo, boyfriend_id)
the VALUES (13 is, 'Don Xin Yi', ' F ',' 1990-4-23 ',' 1898888888 ', NULL, 2);

# 2. The column can be a null value must be inserted. How can insert null values in a column?
# A way:
INSERT INTO Beauty (the above mentioned id, NAME, Sex, borndate, Phone, Photo, boyfriend_id)
VALUES (13, 'Tang Xin Yi', 'female', '1990-4-23', '1898888888 ', NULL, 2) ;

# Second way:

The INTO Beauty the INSERT (ID, NAME, Sex, Phone)
the VALUES (15, 'Nazha', 'F', '1388888888');


# 3 column order may be reversed if
the INSERT the INTO Beauty (NAME, Sex, ID, Phone)
the VALUES ( 'Xin Jiang', 'woman', 16, '110');


# 4. The number of columns and the number of values ​​must be consistent

The INTO Beauty the INSERT (NAME, Sex, ID, Phone)
the VALUES ( 'Xiaotong off', 'woman', 17, '110');

# 5. Column names can be omitted, the default for all columns, and the order of columns in the same column in the table and

The INTO Beauty the INSERT
the VALUES (18 is, 'Zhang', 'M', NULL, '119', NULL, NULL);

# Second way:
/ *

Syntax:
INSERT INTO table
set column name = value, column name = value, ...
* /


INSERT INTO beauty
SET id=19,NAME='刘涛',phone='999';


# Two ways big pk ★


# 1, a way to support the insertion of multiple rows, the second does not support the way

The INTO Beauty the INSERT
the VALUES (23 is' 1 Tang Xin Yi ',' F ',' 1990-4-23 ',' 1898888888 ', NULL, 2)
, (24' Don Xin Yi 2 ',' F ',' 1990-4- 23 is', '1,898,888,888', NULL, 2)
, (25 'Don Xin Yi 3', 'F', '1990-4-23', '1898888888 ', NULL, 2);

# 2, a way to support subqueries, second approach is not supported

INSERT INTO beauty(id,NAME,phone)
SELECT 26,'宋茜','11809866';

INSERT INTO beauty(id,NAME,phone)
SELECT id,boyname,'1234567'
FROM boys WHERE id<3;

# Second, modify the statement

/*

1. Modify single table recording ★

Syntax:
Update table
set column = new value, a new column = value, ...
the WHERE filter criteria;

2. Modify the record of multi-table [supplement]

Syntax:
SQL92 Syntax:
Update Alias Table 1, Table 2 aliases
set value column =, ...
WHERE join conditions
and filters;

sql99 Syntax:
Update Alias Table 1
inner | left | right join Alias Table 2
on the connection condition
set column = value, ...
WHERE filters;


*/


# 1 Record modify single-table
# Case 1: Modify the table name is Tang beauty of the goddess telephone 13899888899

UPDATE beauty SET phone = '13899888899'
WHERE NAME LIKE '唐%';

Case # 2: Modify boys table name id 2 as well as Zhang, Charisma 10
the UPDATE = boyname the SET boys 'Zhang', 10 = usercp
the WHERE id = 2;

 

# 2. Record modify multi-table

Case # 1: Modify zhangwuji girlfriend's phone number is 114

UPDATE boys bo
INNER JOIN beauty b ON bo.`id`=b.`boyfriend_id`
SET b.`phone`='119',bo.`userCP`=1000
WHERE bo.`boyName`='张无忌';

 

Case # 2: Modify the goddess no boyfriend boyfriend are numbered as No. 2

UPDATE boys bo
RIGHT JOIN beauty b ON bo.`id`=b.`boyfriend_id`
SET b.`boyfriend_id`=2
WHERE bo.`id` IS NULL;

SELECT * FROM boys;


# Third, delete statement
/ *

Method 1: delete
syntax:

1 to remove the single-table ★] [
delete from table where Filters

2, delete multiple tables [supplement]

sql92 Syntax:
Delete Alias Table 1, Table 2, the alias
from Table 1, the alias, the alias in Table 2
where connection condition
and filtering condition;

sql99 syntax:

Table 1 delete the alias, the alias in Table 2
from Table 1 alias
inner | left | right join alias on the connection condition table 2
where the filters;

 

Second way: truncate
syntax: truncate table table name;

*/

# A way: delete
delete # 1 single table.
# Case: delete the phone number information to the goddess ending 9

DELETE FROM beauty WHERE phone LIKE '%9';
SELECT * FROM beauty;


Delete # 2. Multiple Tables

Case #: delete the message girlfriend zhangwuji

DELETE b
FROM beauty b
INNER JOIN boys bo ON b.`boyfriend_id` = bo.`id`
WHERE bo.`boyName`='张无忌';


Case #: delete information Huang Xiaoming and his girlfriend of information
DELETE b, BO
the FROM Beauty b
the INNER JOIN ON b.`boyfriend_id` Boys BO = bo.`id`
the WHERE bo.`boyName` = 'Xiaoming';

 

# Second way: truncate statement

# Case: Charisma> god 100 information deleted
TRUNCATE TABLE boys;

 

#delete pk truncate face questions [★]

/*

1.delete can add where condition, truncate can not add

2.truncate deleted, a high efficiency Diudiu
3. If the table has to be removed from the growing columns,
if deleted with delete, insert data, since the value of the growth column from the break point,
and truncate deleted, and then insert data from the growth column values from the beginning.
4.truncate no return value delete, delete delete the return value

5.truncate delete can not be rolled back, delete delete can be rolled back.

*/

SELECT * FROM boys;

The FROM Boys the DELETE;
TRUNCATE TABLE Boys;
the INSERT the INTO Boys (boyname, usercp)
the VALUES ( 'Zhang', 100), ( 'Bei', 100), ( 'Kuan', 100);

 

Guess you like

Origin www.cnblogs.com/Diyo/p/11360288.html