Series Article Directory
The previous blog mainly introduced the basic operations and data types of MySQL. For details, please read inside: The previous blog: Xiaobai learns MySQL
Article Directory
Preface
1. CRUD?
CRUD: Create, Retrieve, Update, Delete are
actually abbreviations for adding, deleting, checking and modifying, but in order for us to enter the company to communicate more conveniently in the future, we still need to understand this abbreviation.
Second, the addition, deletion, and modification of MySQL
1. Create
Mainly divided into "single row insert + full column insert" and "multiple row insert + full column insert"
Single row insert + full column insert : each column of the table must be inserted in order, and none is null, generally only used when inserting less data;
Multi-row insert + full-column insert : You can quickly choose to insert multiple columns of data, without having to insert each column, which is convenient and time-saving.
2. Delete (Delecte)
1. Delete according to the conditions
2. Delete all data of the entire table:
3. Query (Retrieve)
1. Full column query
2. Specify column query
3. The query field is an expression
4. Alias: In the table set of the query result, alias=table name.
In this example, the sum of Chinese, Mathematics, and English scores is divided into aliases. Be sure to remember this format.
5. Deduplication: DISTINCT
This example is to deduplicate the specified column math column to remove duplicate values.
6. Sorting: ORDER BY
(1) For queries without an ORDER BY clause, the return order is undefined;
(2) NULL data sorting is regarded as smaller than any value, ascending order appears at the top, and descending order appears at the bottom Below; the basic format is as follows:
(3) Sort using expressions and aliases
(4) Multiple fields can be sorted, and the priority of sorting is determined by the order of writing (the default order is ascending)
7. Condition query: WHERE
- WHERE conditions can use expressions, but not aliases;
- AND has a higher priority than OR. When used at the same time, you need to use parentheses () to wrap the priority execution part;
Here are some commonly used operators:
Operator | Description |
---|---|
between A and B | A<=value<=B, then return true |
in(a,b,c,d…) | If it is any of the parentheses, it returns true |
is null | Is null |
is not null | Not null |
like | Fuzzy matching,% means any number (including 0) characters; _ means any character |
>,>=,<,<=, | – |
= | Null is not safe, for example, the result of null=null is still null |
<=> | Null safety, such as null<=>null, the result is still null |
!=,<> | Are not equal to |
Logical operator:
and or not
here is just one example, fuzzy matching:
8. Paging query:
OFFSET in the third method of LIMIT is the offset, starting from S, and filtering n results.
4. Update
to sum up
1. For students who are just starting to learn MySQL, the operation of the table that is just starting to learn is not too difficult. They are all single table operations, mainly conditional query statements. When there are multiple statements querying together, clarify the logic , Don’t forget which condition;
2. You need to practice more . For some query sentences, after you write them, you have to experiment by yourself to know whether the sentences you wrote are correct.