table data manipulation
- 1. Add data
- 2. Query data (Retrieve)
-
- 1. Full column query
- 2. Specify column query
- 3. Expression query
- 4. Alias query (alias the specified column)
- 5. Deduplication distinct
- 6. Sort order by
- 7. Conditional query
-
- 7.1<=> Usage
- 7.2 null usage
- 7.3 where conditional query
- 7.3.2.where conditional query syntax:
- 7.3.3 Inquiring about those who are absent from the English test
- 7.3.4 Inquire about people whose total score is greater than 100
- 7.3.5 Field name between x and y (can contain x and y values)
- 7.3.6 in query: all sets that satisfy the conditions will be queried
- 7.3.7 Fuzzy query like:
- 8. Paging query LIMIT: Query part of the information on a page (part of the information is intercepted)
- 3. Modify the data
- 4. MySQL delete operation
1. Add data
1. Add all columns (creat)
insert into score values(, , ,);
build table
1.1 Single row full column insertion
Note: If you do not specify a column when adding, the order of the inserted values must be consistent with the order in which the table was created.
1.2 Multi-row full-column insertion
insert into score values( , , , );
2. Specify the column to insert
insert into score( , , , ) values( , , , );
Note:
Columns must be specified
The order of inserted values must be consistent with the order of specified columns.
Extension: Storage directory for tables and table data (two methods)
2. Query data (Retrieve)
1. Full column query
select * from table_name;
There may be two problems with full-column query:
1. The more columns queried, the larger the amount of data that needs to be transmitted, which will consume a lot of bandwidth information, and the transmission speed will be very slow;
2. It may affect the index use.
2. Specify column query
select 列名 from 表名
3. Expression query
select 表达式 from 表名;
SQL expression query, will not modify the original data of the table
4. Alias query (alias the specified column)
select 指定列 as 别名 from 表名;
Can use as or not
5. Deduplication distinct
5.1 Single-column deduplication
select distinct 去重列名 from 表名;
5.2 distinct joint deduplication
Rule: All fields are the same before they are combined to remove duplicates, otherwise they will not be duplicated.
6. Sort order by
6.1 ascending asc according to a column - from small to large
select * from 表名 order by 列名 asc;
6.2 Descending according to a column desc - from large to small
select * from 表名 order by 列名 desc;
If asc or desc are not specified, asc is used by default
6.3 Ascending order based on total score
select 列名 from 表名 oeder by 列名 asc;
6.4 According to the total score in descending order, the total score uses an alias
select 列名 from 表名 oeder by 列名 desc;
Note: order by can be sorted using aliases
NULL is the minimum level when sorting
6.5 Multi-column sorting
select name,math,chinese from score order by math,chinese desc;
7. Conditional query
Note: The execution priority of the logical operator and is higher than that of or, so if there is both an and query and an or query in a query, you need to add () to the or query, the function is to increase the execution priority, add () The SQL statement takes precedence.
Scholarships can only be issued if the following two conditions are met:
1. Mathematics score >= 30 or English score >= 40;
2. The language score must be >=50.
7.1<=> Usage
7.2 null usage
7.3 where conditional query
7.3.1***Notes:***
1. Aliases can be used in order by
where conditional query cannot use aliases
select math+chinese+english as total from score order by total desc;
7.3.2.where conditional query syntax:
selest * from table_name where 查询条件;
Why can't aliases be used in where, but can be used in order by
The execution order of mysql is as follows:
1.FROM stage
2.WHERE stage
3.GROUP BY stage grouping query
4.HAVING stage grouping query conditions
5.SELECT stage
6.ORDER BY stage
7.LIMIT stage paging query
alias is generated in the 5th query stage.
After the alias is obtained, it can be used in the following stages, that is to say, the alias can only be used by the query after step 5.
7.3.3 Inquiring about those who are absent from the English test
7.3.4 Inquire about people whose total score is greater than 100
7.3.5 Field name between x and y (can contain x and y values)
more elegant
7.3.6 in query: all sets that satisfy the conditions will be queried
7.3.7 Fuzzy query like:
%------> can match any character
where 字段名 like '% _';
__------> can match an arbitrary character
% and _ can be used in any position
. All queries in NMySQL should consider the leftmost principle, and put the condition that can filter the most items on the leftmost
8. Paging query LIMIT: Query part of the information on a page (part of the information is intercepted)
Limit 3 ways to write:
- limit limit n;
select * from table_name limit n;
The maximum number of queries is limited to 300. If the amount of data is not enough, the query will not report an error
. 2. limit n offset s offset;
limit n offset s 偏移;
查询n条 跳过前s条
3.limit s,n;
query n items, skip the first s items
Note: The order of 2. and 3. is reversed
3. Modify the data
1. All data modification (very dangerous, accidentally changed all data)
updata table_name set 修改内容;
2. Partial data modification
limit
where
3. Multi-column modification of some data
set , , ,
4. Using Expression Modification
4. MySQL delete operation
1.delete delete DML language
delete from table_name [where...] [order by...] [limit...]
Delete row data, the structure of the table exists
2.drop delete DDL language
drop table table_name;
3. truncate delete (will not delete the table structure, only delete all data in the table)
truncate [table] table_name;
The difference between the three
1. The influence of deletion:
drop (table structure + data) > truncate (delete all data) >= delete (delete all or part of the data)
2. Query conditions : delete can add query conditions to where expressions, but other delete commands cannot.
3. The type of command:
drop belongs to DDL,
delete belongs to DML (execution principle: under the ***InnoDB database engine***, delete does not really delete the data, but marks the data with a delete label to identify the The current data has been deleted)
***truncate 属于 DDL***(执行原理:1.重新创建一个表2.将原来的表删除)
**4. Data recovery is different: **Delete data can be recovered, but data deleted by truncate and drop cannot be recovered
If autocommit = 1, delete can be restored, delete the log updata table table_name set iddel = 0;
execute the SQL negation operation to restore the data. The prerequisite is that the logging of the database is turned on. The execution performance of the database can be improved, and the log can be turned off for systems that are not sensitive to data loss.
**5. Execution efficiency: **drop > truncate > delete
the larger the amount of data, the greater the gap between the above execution efficiency.
6. delete delete does not reset child value added