MySQL - table additions, deletions, and changes

CRUD: Create (create), Retrieve (read), Update (update), Delete (delete)

1.Create

Create a table to store student information

 1. Single row data + full column insertion

 

 2. Multi-row data + specified column insertion

 3. insert else update

When the primary key or unique key conflict, you can choose to perform synchronous update operation syntax:

INSERT INTO students (id, sn, name) VALUES (1, 1, 'zhang')
ON DUPLICATE KEY UPDATE sn = 1, name = 'zhang';
Query OK, 2 rows affected (0.47 sec)
-- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,并且数据已经被更新
-- 通过 MySQL 函数获取受到影响的数据行数
SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
|      2      |
+-------------+

4. Replace

If there is no conflict, it will be inserted directly, and if there is a conflict, it will be deleted and then inserted.

二.Retrieve

select column

 1. Full column query

But it is generally not recommended to use * for full-column queries.

2. Specify column query

 

3. Query field expression 

3.1 Fields not included in the table

 3.2 Expression contains a field

3.3 Expression contains multiple fields

 

 4. Specify aliases for query results

5. Deduplication of results

 select distinct table_ele_name from table_name 

WHERE condition

Comparison operator:

) If it is any of the options, return TRUE(1) IS NULL is NULL IS NOT NULL is not NULL LIKE fuzzy matching. % means any number (including 0) of any character; _ means any character








Logical Operators:

Operator Description
AND Multiple conditions must be TRUE(1), the result is TRUE(1)
OR Any condition is TRUE(1), the result is TRUE(1)
NOT The condition is TRUE(1), the result is FALSE( 0)

This is the full table

1. We find students whose math scores are less than 80

2. Next, let's look for students with Chinese scores of [75, 85]

 The above uses and for conditional splicing, and then uses between...and...

 3. Students whose Chinese score is 80.81 or 82

 

 Above we use or, below we use in condition

 4. A classmate named Zhang

 The % above matches any number of arbitrary characters, and if _ is used, it matches one arbitrary character. We then insert the data Zhang Sansan for verification.

 5. Students with better Chinese grades than maths grades

 6. Students whose total score is less than 150

7. Students with a total score of more than 150 and no surname Zhang (Li Si), let's verify

Sort results

-- ASC is ascending (small to large)
-- DESC is descending (large to small)
-- default is ASC

1. We display the data in the table in ascending order of math scores

 2. We display the data in the table in descending order of math scores

 3. Display in ascending order of language and descending order of mathematics

When collation conflicts, the collation written first takes precedence.

4. Inquire about classmates and their total scores, and display them from high to low (in descending order)

 

5. Query the grades of students surnamed Zhang, and the results are displayed in descending order of math grades (in descending order) 

Filter results pagination

-- Starting from s, filter n results, which is more explicit than the second usage, it is recommended to use
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;

For better viewing, we insert another data

3.Update

1. Change Zhang San's score to 85

 

 2. Change Li Si's Chinese and math scores to 85

 3. Add 10 points to the last place

We can see that the last place is 130 points (calculated according to 130 due to mistakes) 

 4. Double the Chinese grades of all students

4. delete

delete data from table

1. Delete Zhang San data

2. Next, we delete the entire table data (use with caution)

 truncate table

 

5. Insert query results

Create a repeating table

 Create another table with no duplicates

6. Aggregate function

Function Description
COUNT([DISTINCT] expr) Returns the number of queried data
SUM([DISTINCT] expr) Returns the sum of queried data, not a number meaningless
AVG([DISTINCT] expr) Returns the average of queried data , not a number meaningless
MAX([DISTINCT] expr) returns the maximum value of the queried data, not a number meaningless
MIN([DISTINCT] expr) returns the minimum value of the queried data, not a number meaningless

1. Check how many students there are in the class

 2. View the number of qq collected in the table

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324304119&siteId=291194637