[MySQL] Advanced use of MySQL database

Don't lose heart, everything will be fine...
insert image description here


1. MySQL basic query

1. Create (increase) the content of the table

1.1 Use of insert statement

1.
What we have learned before are all DDL statements, and what we will learn next is the real DML statement.
The sql statement for inserting data is insert into table_name (column1, column2, ...) values ​​(data1, data2, ...). When the brackets on the left of values ​​are not added, the default means to insert all the columns of the table and not ignore any column. When adding brackets, you can specify some columns to insert, but it is worth noting that if some columns do not have default constraints and you ignore them for data insertion, the operation of inserting data will definitely fail. The number of brackets on the right of values ​​indicates how many rows of data to insert into the table, and the data separated by commas in the brackets correspond to the column fields in the table one by one.

insert image description here

2.
When inserting data, if you encounter a primary key conflict or a unique key conflict, the data insertion may fail. At this time, there is a synchronous update operation syntax to ensure that when the data insertion fails, it can be updated to insert new data.
In fact, just add on duplicate key update column1=data1, column2=data2... after the original insert sql statement.
When the updated data conflicts with the table, if the conflicting data is the same as the data to be updated, the original conflicting data in the table will not change, and the return result of the SQL statement is 0 row affected. If the conflicting data is different from the data to be updated, the original conflicting data in the table will be deleted first, and then the new data to be updated will be inserted. The result returned by the sql statement is 2 row affected; if the updated data does not conflict with the table, the function of the statement is the same as the ordinary insert statement, and the return result of the sql statement is 1 row affected; MySQL has a function called row_count(), which is used to count the number of rows affected by the latest sql operation
.

insert image description here

3.
In addition to insert conflicts, we use update to update the syntax, we can also directly use replace into to replace the data in the table. When there is conflicting data in the table, delete the conflicting data and then insert it. If there is no conflicting data, insert it directly.

insert image description here

1.2 Insert query results (delete duplicate records in the table)

1.
In addition to directly inserting data, insert also supports inserting the results of the select query. If you want to delete duplicate records in the table, we want to make this operation atomic.
Then we can create a new no_duplicate_table, and then query the non-duplicate data in the duplicate_table, that is, when selecting the query, add the keyword distinct, and insert the query result into the table no_duplicate_table. The sql statement used is: insert into no_duplicate_table (id, name) select distinct * from duplicate_table; where (id, name) can be added or not, if not, then The default is full column insert.
After insertion, the value in no_duplicate_table is non-duplicate data. At this time, we only need to rename the table, which is equivalent to deleting duplicate records in the table. The rename process must be atomic.

insert image description here

2. Retrieve (read) the contents of the table

1.
Retrieve means retrieval. In MySQL, it can be considered as a read operation. The operation of querying data in MySQL, that is, the R operation is the most frequent operation, and it is also an operation that needs to be learned. In the R operation, the most typical SQL statement is the select statement, which is used to query the data in the table.
The following is the syntax of select

insert image description here

2.
In practice, it is not recommended to use full-column query, because it needs to display all the data in the table, and some of the data may not be in the memory at this time. The mysqld service also needs disk IO to load the remaining data of the table, which reduces the performance of MySQL query. At the same time, full-column query cannot use the index to optimize the query process, because the index can only improve the query of part of the data. Once the query data involves column fields that are not included in the index, the B+ index structure cannot be used to optimize the query speed at this time. Greatly reduce the query speed.
In addition, the MySQL database used by the actual company stores at least a few million records. Once the full list is queried, the query results will be frantically swiped to the monitor, and it is impossible to see it, and it may cause the mysqld service to freeze. Therefore, we usually type some simple database sql by ourselves, and we can use it. After entering the company, we must not use the full list query.
The field order of the specified column query can be customized, and does not need to be consistent with the column field order in the table.

insert image description here

3.
The query field can also be an expression, and the column field can also be mixed in the expression for query

insert image description here

You can also use as to specify an alias for the query field. As can be omitted, I added it here.
If the select query result has duplicate data, you can also add the distinct keyword after select to deduplicate the query result.
If there are multiple columns after the select, the deduplication processing of the multi-column fields will be performed, that is, only when the multiple column fields are repeated at the same time, the deduplication will be performed. If there is only one column, deduplication is performed only for that column.

insert image description here
insert image description here

4.
The where clause is a filter condition commonly used by select when querying. When the where condition is judged to be true, select will display the query results. Next, we will use multiple use cases to familiarize ourselves with the use of where conditions and logical operators.

insert image description here

= is generally used to judge equality in MySQL, and there is another thing that is relatively useless: <=>. = is NULL unsafe, and <=> is NULL safe. Judging whether the two values ​​are equivalent = the frequency of use is relatively high.
insert image description here

Students who fail in English and their English scores ( < 60 )

insert image description here

Students with Chinese scores in [80, 90] and their Chinese scores

The condition of between and logical judgment happens to be a closed interval, so the functions of the upper and lower query SQL statements are equivalent

insert image description here

Students whose math scores are 58 or 59 or 98 or 99 and their math scores

insert image description here

A classmate surnamed Sun and a classmate named Sun

insert image description here

Students whose Chinese scores are better than their English scores

insert image description here

Students with a total score of 200 or less

Note: Expressions can be used in where conditions, but aliases cannot appear in where conditions!

insert image description here

Students whose Chinese score is > 80 and whose surname is not Sun

insert image description here

Student Sun, otherwise the total score > 200 and the Chinese score < the math score and the English score > 80

insert image description here

5.
The order by clause can sort and display the results of the select query.

Classmates and math scores, displayed in ascending order of math scores

If the order by clause does not add asc or desc, the default is asc ascending order
insert image description here

Classmates and qq numbers, sorted by qq numbers

insert image description here

Query students' scores in various subjects, and display them in descending order of mathematics, ascending order of English, and ascending order of Chinese

When sorting by multiple fields, the priority of sorting decreases with the order of writing.
insert image description here

Query classmates and total scores, from high to low

Because the order of keyword execution is: from, select, order by, the alias of total score can appear in the order by clause. At the same time, expressions can also appear in the order by clause.

insert image description here

Query the math scores of students surnamed Sun or students surnamed Cao, and the results are displayed in descending order of math scores

insert image description here

6.
When querying an unknown table, it is best to display it in pages, so as to avoid that when the data in the table is too large, the entire table data will be queried and the database will be stuck.

It is recommended to use limit+offset to display data in pages, because the semantics of this are more clear. If you don’t want to use it, you can also use limit number, number to display data in pages.
insert image description here

The left and right two have the same effect on paging display of table data. It is recommended to use the right one
insert image description here
Sort the students with a total score greater than 200 in descending order first, and then display them in pages, with 2 pieces of data per page

insert image description here

3. Update the content of the table

1.
update is used to change the data of a certain row or multiple rows in the table. It is worth noting that when using update to update the data in the table, if you do not keep up with the where clause to filter the data, update will update all the rows in the table to update the field value of a column, because the where clause can filter out the rows with symbolic conditions, and it is reasonable to update the update data for rows that meet the conditions.
When updating, it can also be followed by where clause, order by clause, and limit clause. The function of these clauses is nothing more than row-level filtering of data. Generally, limit will be used in conjunction with order by clause, because the rows directly filtered by limit are not sequential, that is to say, the order of the results directly displayed is undefined. We should not rely on this order, but use the order by clause to return the defined order. After the clause returns the specific row data, you can update the field data of a column for these row data.

insert image description here

Change the math score of Sun Wukong to 80 points

insert image description here

Change Cao Mengde's mathematics score to 60 points and Chinese score to 70 points

insert image description here

Add 30 points to the mathematics scores of the three students with the bottom three total scores

insert image description here

Update the Chinese scores of all students to double the original

If you do not filter row data with clauses such as where, order by, limit, etc., all row data in the table will be updated with column field values ​​by default
insert image description here

4. Delete the contents of the table

4.1 Use of delete from statement

1.
The delete statement can delete the filtering results of clauses such as where, order by, and limit.

insert image description here
Delete the test scores of Sun Wukong

insert image description here

Delete the student with the lowest total score

Limit can be combined with the order by clause to filter out the students who are the last one

insert image description here

4.2 The difference between truncate and delete from

The soul of the interviewer asked: What is the difference between delete, truncate, and drop in MySQL?

The following answers are all excerpted from the above article, the above article summarizes in detail, you can go to have a look

1.
delete is a DML statement, and it will go through a transaction when it is executed. It will first cache the deleted data to the rollback segment, and it will take effect immediately after the transaction is committed.
For delete from table_name, in InnoDB, delete does not actually delete the data, but only marks the deleted data to indicate that the data is invisible, but the space occupied by the disk file corresponding to the data will not be released. When data is inserted into the table next time, this part of the space can be reused, and the deleted data will be overwritten by new data.
For delete from table_name, in MyISAM, the disk file space where the deleted data is located will be released immediately.
For delete from table_name where xxx conditional deletion, neither InnoDB nor MyISAM will release disk space.
Regardless of the InnoDB or MyISAM storage engine, if you want to free up disk space after deleting data, you can execute the optimize table table_name statement.
The delete operation deletes data line by line, and at the same time, the delete operation log of the line is recorded in the redo and undo tablespaces for rollback and redo operations, and a large number of logs generated will also occupy disk space

2.
Truncate is a DDL statement that does not pass through transactions when executed. Among the three types of SQL statements, only DML will pass through transactions (InnoDB). Unlike delete, truncate can only operate on the data of the entire table, and cannot operate on partial data like DELETE.
When truncate is executed, the deleted data will not be placed in the rollback segment, and it will take effect immediately after execution. If the data is not backed up, the deleted data cannot be retrieved.
Regardless of whether it is InnoDB or MyISAM, after executing truncate, the disk space will be released immediately, all data in the table will be deleted, and the data page will be released directly.
Use truncate carefully, especially when there is no backup, if you accidentally delete the online form, remember to contact CAAC in time, booking phone: 400-806-9553

3.
Drop is also a DDL statement. Like truncate, if you delete the data directly without a backup, it cannot be retrieved.
Be careful using drop and truncate, these are two very dangerous commands, brothers who want to delete the table and run away, please execute the operation after the ticket is successfully booked! Booking phone: 400-806-9553

It can be understood that for a book, delete means tearing up the directory, truncate means tearing out the content of the book and burning it, and dropping means burning the book
insert image description here

insert image description here

5. Aggregation function (add distinct in parentheses for deduplication)

1.
The column fields that can be aggregated must be numbers, otherwise it is meaningless. Except for the count function, the fields in the brackets of count can be numbers, column field names, wildcards, etc., because count is only responsible for counting the number of records in the table (a row of data in the table becomes a record), so count is special, and the fields in the brackets of the other four aggregation functions can only be column field names whose values ​​are numbers, otherwise it is meaningless.

insert image description here

Count the number of students in the class
insert image description here

Count the number of math scores in this exam

insert image description here

Statistical Mathematics Total Score

insert image description here

Statistical average score. Returns the highest score in English. Returns the minimum math score > 70 points.

insert image description here
2.
The following sql statement does not conform to the syntax support. If there may be multiple minimum scores, there will be multiple name column fields, but the column field after min aggregation can only have one value, which is obviously problematic. Select cannot display a two-dimensional determinant structure, so MySQL must not support such a syntax.
If you want to support it, you should use select name, min(math) from exam_result where math > 70 group by name; when this SQL statement is executed, the priority order of keywords is from>where>group by>select>, in fact, it is to first filter the data with the where condition, and then group the filtered data. When grouping, group according to the difference of name. For the display of column fields, only two columns of fields are displayed at this time, one is name, and the other is the aggregate statistical result min(math)

insert image description here

6. The use of the group by clause (conditional filtering after grouping and aggregation statistics with having)

1.
Generally speaking, group by is usually used with aggregation functions to perform group aggregation statistics.

insert image description here
The following is the classic test table of oracle 9i.

How to display average salary and maximum salary for each department

insert image description here

Display the average and minimum wages for each job type in each sector
First divide the data in emp into three groups according to different departments, and then subdivide the groups according to the different positions in each group, then perform aggregation statistics on the final subdivided groups, and then display the aggregation statistics results.

insert image description here

Display the department with average salary below 2000 and its average salary

The above requirement is different from the previous one. It has a filter condition. In group by, having is usually used as the filter condition. When the grouping aggregation statistics are completed, having will be used to filter the statistical results, and finally the filtered aggregation result column field and other column fields will be displayed in select.

insert image description here

7. Written interview questions

Niuke: SQL228 batch insert data

insert image description here

Niuke: SQL202 Find out the current salary situation of all employees

insert image description here

Niuke: SQL195 Find all the information of the latest employee

insert image description here

Niuke: SQL196 Find all the information of the employee whose entry time is the third last

When sorting by entry time, you must add distinct to deduplicate, because there may be employees with the same entry time. If you do not remove the duplication, the third-to-last entry time and the penultimate and the penultimate are the same entry time, so you must remove the duplicates, and then use the filtered real bottom-to-third entry time as the filter condition of the where clause to find all employees in the table with the entry time.
insert image description here

Niuke: SQL201 Find the employee number emp_no with more than 15 salary records and the corresponding number of records t

insert image description here

Niuke: Obtain the current (salaries.to_date='9999-01-01') salary of the manager in all departments (dept_manager.to_date='9999-01-01'), give dept_no, emp_no and salary, and the output results are arranged in ascending order of dept_no (please note that the same person may have multiple salary records)

This question is a multi-table query question. You can return to the head to do this question after finishing the following studies. When doing the question, pay attention to the execution priority of SQL keywords. from>where>select>order by
insert image description here

Niuke: Obtain from the titles table and group by title

insert image description here

Leetcode: 182. Find Duplicate Emails

insert image description here

Ricochet: 595. Big Country

insert image description here

Buckle: 177. Nth highest salary

Interview question: The order of execution of each keyword in the SQL query
from > on> join > where > group by > with > having > select> distinct > order by > limit

Two, MySQL built-in functions

1. Date function

1.
The following are some of the more common date functions. There are many other date functions. You can check them yourself. Here we will only talk about this part of the date functions.

insert image description here
The following is an example of using the date function, it is not difficult, just take a look
insert image description here
2.
The date type can generally be used to record birthday fields, the date type can store dates, the time type can store time, and the datetime type can store date + time data.

insert image description here
Here are two use cases for date types

insert image description here

insert image description here

2. String functions

insert image description here

charset can display the character encoding format used by the parameter field, and concat can connect multiple string parameters together

insert image description here

instr can check whether the substring to be queried is in the queried string, if it is, it will return the subscript, if it is not, it will return 0, ucase is used to convert each English character in the string to uppercase, lcase is used to convert each English character in the string to lowercase, length can find the number of bytes occupied by the string.
insert image description here

insert image description here
If you want to display a long list of information, you can use concat to connect column fields and other strings together, and then select to display
insert image description here

replace can find the position of the second parameter in the first parameter, and replace it with the third parameter after finding it. replace does not change the storage of data, but only changes at the select display level.
insert image description here
substring can be used to intercept substrings, the first parameter represents the string to be intercepted, the second parameter represents the subscript position from which to intercept, and the third parameter represents the length of the interception. substring does not change the storage of data, it only changes at the select display level
insert image description here

ltrim and rtrim only delete the left or right spaces of the string. If there is a space in the middle of the string, it will not be deleted. trim is the function of ltrim+rtrim, which directly deletes the spaces on the left and right sides.
insert image description here

3. Mathematical functions

abs is used to find the absolute value, bin can be used to find the binary representation of the number, hex is used to find the hexadecimal representation, and conv is used to convert the first parameter from the base represented by the second parameter to the base represented by the third parameter.
insert image description here

format is used to format the decimal form, you can specify the number of digits to keep the decimal. Mod is used to take modulus. In addition to positive integers, negative numbers can also be used to take modulus. Negative number modulo can be divided into two cases in form. One is that the first parameter is a negative number, and the other is that the second parameter is a negative number. However, in actual calculation, we can expand the second parameter to an integer multiple and then add a number equal to the first parameter, and the added number at this time is the result of mod.
For example, 10mod-3, -3 is expanded by -3 times and then added with 1 is equal to 10, then the modulo result is 1. -10mod3, 3 is expanded by -3 integer times and then added -1 is equal to 10, then the modulo result is -1. It should be noted that when expanding integer times, it must be close to the last equal number, but not more than this number. For example, when -10mod3, 3 cannot be expanded by -4 times and then added by 2. This is not acceptable because it is not close to -10.

insert image description here

rand can return a random number between 0 and 1.0.
insert image description here

Regarding rounding, common rounding methods can be divided into four types, rounding to zero, rounding up, rounding down, and rounding.
The result of rounding can be seen in the following case. It is not difficult, and you will understand after reading it.

insert image description here

4. Other functions

Here are some other common functions in MySQL, you can look at the use cases yourself.

insert image description here

insert image description here

Niuke: SQL245 finds the number of occurrences of commas in a string

insert image description here

3. MySQL compound query

1. Basic query review

Query employees whose salary is higher than 500 or whose position is MANAGER, and their initials must be capitalized J

insert image description here

Sort by department number in ascending order and employee salary in descending order

insert image description here

Sort in descending order by annual salary

insert image description here

Display the names and job titles of the highest paid employees

insert image description here

Display information about employees whose salary is higher than the average salary

insert image description here

Show average and maximum salaries for each department

insert image description here

Display the department numbers whose average salary is less than 2000 and their average salary

insert image description here

Show total number of employees for each job, average salary

insert image description here

2. Multi-table query

1.
Like all the queries we have learned above, such as where clauses, order by clauses, group by clauses, having clauses, and pagination display limit queries are all single-table queries, and the data we usually query may come from more than one table, and it is likely to come from multiple tables, so multi-table queries are required.

Display employee name, employee salary, and department name

From is followed by two tables, and the two tables are first used as a Cartesian product, but the table after the Cartesian product has many rows of data that are redundant, because employees can only be in one department, so emp.deptno must be consistent with dept.deptno, so after the Cartesian product, the where condition is required to filter out reasonable records.
insert image description here

Display the department name, employee name and salary of department number 10

insert image description here

Display the name, salary, and salary level of each employee

insert image description here

Display the number and name of the superior leader of the employee FORD (mgr is the number of the employee leader – empno)

Requirements like the above actually require self-joining, you can use multi-table query, or you can choose sub-query

insert image description here

3. Nested query (subquery)

1.
Subqueries refer to select statements embedded in other SQL statements, also called nested queries.
Through the returned results of select statements embedded in other SQL statements, subqueries can be subdivided into single-row subqueries, multi-row subqueries, and multi-column subqueries. In addition to being used in the where clause as a filter condition, subqueries can also be used in the from clause as a temporary table for Cartesian product.

Show employees in the same department as SMITH

insert image description here

Query the name, position, salary, department number, and department name of the employee who is the same as the job in department 10, but does not include 10's own job

insert image description here

Display the name, salary, and department number of employees whose salary is higher than that of all employees in department 30

insert image description here

Display the name, salary, and department number of employees whose salary is higher than that of any employee in department 30 (including employees in their own department)

insert image description here

Query all employees who have the same department and position as SMITH, excluding SMITH himself

insert image description here

Display the name, department, salary, and average salary of each employee whose salary is higher than the average salary of his department

insert image description here

Find the name, salary, department, highest salary of the person with the highest salary in each department

insert image description here

Display the information of each department (department name, number, address) and the number of personnel

insert image description here

2.
Merge query is to merge the query results of multiple selects together. When union is merged, duplicate rows (two rows with completely duplicated columns and fields) will be automatically removed, and union all will not remove duplicate rows.

union: This operator is used to obtain the union of two result sets. When using this operator, duplicate rows in the result set are automatically removed.

Find out the people whose salary is greater than 2500 or whose position is MANAGER

insert image description here

union all: This operator is used to obtain the union of two result sets. Duplicate rows in the result set are not removed when this operator is used.

Find out the people whose salary is greater than 2500 or whose position is MANAGER

insert image description here

4. Actual OJ

Find the salary status of all employees when they joined the company

insert image description here

SQL204 Get all non-manager employees emp_no

insert image description here

SQL205 Get the current manager of all employees

insert image description here

Four, MySQL internal and external connection

1. Inner join

1.
The inner join is actually to filter the join results of the tables according to the on condition first, so the priority of the keyword is from>on>join, because before performing the Cartesian product, the join conditions of the tables must be specified, so that the two tables can be purposefully joined when they are actually joined.
Some people may have questions, why not connect first, and then filter the on conditions? If this is the case, the efficiency must be low, because you need to connect first, then filter, which requires two steps, but if you first on and then join, you only need one step, and you can connect directly with the filtering conditions.

insert image description here
Display SMITH's first and department names

insert image description here

2. Outer join (full display of the left table or full display of the right table)

1.
When connecting tables, if a table must be fully displayed, we call it an outer join. When the left table is fully displayed, we call it a left outer join. When the right table is fully displayed, we call it a right outer join.

insert image description here

Query the grades of all students. If the student has no grades, the student's personal information will also be displayed

insert image description here

Jointly query the stu table and the exam table to display all the grades, even if there is no student corresponding to the grade, it must be displayed

insert image description here

List department names and employee information for those departments, as well as departments with no employees

insert image description here

3. Actual OJ

178. Score ranking

The execution order of keywords in SQL is from>where>group by>select>order by. After grouping, the aggregation function will be executed, then select will be displayed, and finally order by will be sorted. When aliasing a column field, single quotation marks should be added to prevent the alias from conflicting with the keywords in MySQL.
The idea of ​​this question is to take the two tables as a Cartesian product, and the number of all scores greater than or equal to the current score after deduplication is the ranking of the current score. For example, if the score is 3.5 3.65 4.0 3.85 4.0 3.65, the ranking of 3.5 should be the fourth place, because the numbers greater than or equal to 3.5 are only 4.0 3.85 3.65 3.5, so the ranking of 3.5 is the fourth place.
After the Cartesian product, only one row of records with the score in table b greater than or equal to the score in table a is kept, and then grouped by the id of table a, so that the score score of table a in each group is unique, and the number of records after deduplication of the score of b in the group is the ranking of table a.

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/erridjsis/article/details/131769987