当前位置:网站首页>MySQL common add, delete, modify and query operations (crud)
MySQL common add, delete, modify and query operations (crud)
2022-07-04 18:09:00 【Warm the sun like the wind】
️ Preface ️
This article mainly introduces in MySQL Common additions, deletions, modifications and queries in Databases (CRUD)SQL Statement operation .
Blog home page :【 Warm the sun like the wind 】
The high-quality goods Java special column 【JavaSE】、【 Prepare for blue bridge 】、【JavaEE The first step 】、【MySQL】、【 data structure 】
Welcome to thumb up Collection Leave a comment Private letters must be answeredThis paper is written by 【 Warm the sun like the wind 】 original , First appeared in CSDN
Bloggers will continue to update their learning records and gain , If you have any questions, you can leave a message in the comment area
The source code involved in the blog and the daily practice code of the blogger have been uploaded Code cloud (gitee)、GitHub
Content guide
MySQL Common operations of adding, deleting, modifying, and querying (CRUD)
1. newly added (create)
grammar :
insert into Table name values ( value , value );
Case study :
Create a student table to insert :
create table student(id int ,name varchar(50));
insert into student values (1,' Li Si ');
Bottom line “1 row affected in 5 ms” Indicates that a line has changed .
Specify column insertion :
insert into student (name) values(' Zhang San ');
Multi line insertion :
insert into student values (3,' Wang Wu '),(4,' Zhao Liu ');
Insert... Once N Records are better than inserting one point at a time N Insertion is much faster .
2. Inquire about (retrieve)
The query statement is SQL The most core and complex operation in the statement , Next, let's briefly introduce the primary query statement ( Inquire about select Clause no matter how it goes , Will not change the original data of the database ).
Case study :
-- Create test scores
drop table if exists exam_result;
create table exam_result (
id int,
name varchar(20),
chinese decimal(3,1),
math decimal(3,1),
english decimal(3,1)
);
-- Insert test data
insert into exam_result (id,name, chinese, math, english) values
(1,' Tang Sanzang ', 67, 98, 56),
(2,' The Monkey King ', 87.5, 78, 77),
(3,' Pig Wuneng ', 88, 98.5, 90),
(4,' Cao mengde ', 82, 84, 67),
(5,' Liu Xuande ', 55.5, 85, 45),
(6,' king of Wu in the Three Kingdoms Era ', 70, 73, 78.5),
(7,' Song Gongming ', 75, 65, 30);
2.1 Full column query
The most basic operation , But it is also dangerous in actual development , If the amount of data is large , Performing a full column query will consume a lot of system resources , It may cause downtime .
select*from Table name ;
2.2 Specified column query
This is much more efficient than full column queries , When querying, it will be displayed to tell the database which columns to query , The database will return targeted data .
select Name , Name ...... from Table name ;
2.3 The query field is an expression
Make some queries at the same time Operation ( Between columns )
for example , It is expected that the Chinese score in the query results is much higher than the real score :
Check the total score of each student :
2.4 Specify alias for query field
By specifying aliases, you can prevent the temporary table column names from being too messy .
2.5 De duplication of query results
For query results , Erase duplicate records .
If you are de duplication for multiple columns , Only when the values of these multiple columns are the same can they be regarded as duplicates
2.6 Sort query results
select Name ..... from indicate order by Name asc/desc;
Sort math scores in ascending order asc In ascending order ( The default is ascending. You can leave it blank ),desc For the descending order
Be careful :
1. The database record contains NULL The value of will be regarded as the minimum ( It will be regarded as the minimum value in sorting )
2. Sorting can also be based on aliases or expressions .
3. When specifying multiple column sorting , First sort according to the first column , If the values of the first column are the same, then sort by the results of the second column , The following is the mathematical order , If mathematics is the same, then sort by language .
2.7 Conditions of the query
stay select Add where filter , The query results will keep those that meet the conditions , Filter out the unsatisfied .
select Name from Table name where Conditions ;
Comparison operator :
Operator | explain |
---|---|
>, >=, <, <= | Greater than , Greater than or equal to , Less than , Less than or equal to |
= | be equal to ,NULL unsafe , for example NULL = NULL The result is NULL |
<=> | be equal to ,NULL Security , for example NULL <=> NULL The result is TRUE(1) |
!=, <> | It's not equal to |
BETWEEN a0 AND a1 | Range match ,[a0, a1], If a0 <= value <= a1, return TRUE(1) |
IN (option, …) | If it is option Any one of , return TRUE(1) |
IS NULL | yes NULL |
IS NOT NULL | No NULL |
LIKE | Fuzzy matching .% Denotes any number of ( Include 0 individual ) Any character ;_ Represents any character |
Logical operators :
Operator | explain |
---|---|
AND | More than one condition must be TRUE(1), The result is TRUE(1) |
OR | Any one of the conditions is TRUE(1), The result is TRUE(1) |
NOT | Condition is TRUE(1), The result is FALSE(0) |
notes :
- WHERE Conditions can be expressed as , But you can't use aliases .
- AND Has a higher priority than OR, When used at the same time , You need to use parentheses () The priority part of the package
Case study :
The basic query :
1. Query the English grades of students who fail in English
2. Query students whose Chinese scores are better than English scores
3. The total query score is 200 Students with the following scores
Note here that aliases cannot be usedAND And OR:
1. Query language score is greater than 80 branch , And the English score is greater than 80 Classmate
2. Query language score is greater than 80 branch , Or English scores greater than 80 Classmate
3. Observe AND and OR The priority of the
and Has a higher priority than or, If you want to break the priority, you need to use parentheses ()Range queries
1.between …and…
In the field of programming , Most sections are left closed and right open , Pay attention to SQL Middle is an interval with both left and right closed
Query Chinese scores in [80, 90] Students and Chinese scores
Use and It can also be realized
2.in
The query math score is 58 perhaps 59 perhaps 98 perhaps 99 Students and math scores
Use or A similar effect can be achievedFuzzy query :LIKE
% Match any number of ( Include 0 individual ) character
_ Match a strict arbitrary characterNULL Query for :IS [NOT] NULL
And null The relevant operation results are still hard , So we should find the information of students whose Chinese scores are empty , The results cannot be queried in the following way
Yes <=>、is null These two ways to judge whether it is null
2.8 Paging query
If you find a large number of results , We need to display the page by page in the way shown in the figure above .
stay sql In the use of limit To do paging queries ,
grammar :
-- Starting subscript is 0
-- from 0 Start , Screening n Bar result
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- from s Start , Screening n Bar result
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
-- from s Start , Screening n Bar result , More definite than the second usage , It is recommended to use
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;
The first three query results are shown below
From the subscript for 3 Start , Check back three
We mentioned before select*from
Dangerous operation , So we can add where Conditions or limit To more secure restrictions .
3. modify (update)
grammar :
update Table name set Name = value ( Or expressions ), Name = value ( Or expressions )..... where Conditions ;
Here where Restrict those that meet the conditions to modify .
Case study : Change the math score of Monkey King to 80 branch
update exam_result set math =80 where name=' The Monkey King ';
4. Delete (delete)
grammar :
delete from Table name where Conditions [ORDER BY ...] [LIMIT ...];
Delete the exam results of Monkey King
delete from exam_result where name=' The Monkey King ';
Be careful , If not where Conditions , The data of the whole table will be deleted .
5. Map summary
️ Last words ️
Summing up difficulties , hope uu Don't be stingy with your (^U^)ノ~YO!! If there is a problem , Welcome comments and corrections in the comment area
边栏推荐
- 表情包坑惨职场人
- 你应该懂些CI/CD
- Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
- To sort out messy header files, I use include what you use
- Flask lightweight web framework
- Analysis of I2C adapter driver of s5pv210 chip (i2c-s3c2410. C)
- Performance test of Gatling
- Easy to use map visualization
- 2022 national CMMI certification subsidy policy | Changxu consulting
- Set the transparent hidden taskbar and full screen display of the form
猜你喜欢
2022 national CMMI certification subsidy policy | Changxu consulting
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
Superscalar processor design yaoyongbin Chapter 6 instruction decoding excerpt
解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
曾经的“彩电大王”,退市前卖猪肉
Is it science or metaphysics to rename a listed company?
To sort out messy header files, I use include what you use
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
What is low code development?
随机推荐
CocosCreator事件派發使用
wuzhicms代码审计
我写了一份初学者的学习实践教程!
【每日一题】556. 下一个更大元素 III
数学分析_笔记_第7章:多元函数的微分学
创业两年,一家小VC的自我反思
People in the workplace with a miserable expression
Pytorch深度学习之环境搭建
[daily question] 871 Minimum refueling times
Great Wall Securities security does not open a securities account
设置窗体透明 隐藏任务栏 与全屏显示
高中物理:力、物体和平衡
Stars open stores, return, return, return
【HCIA持续更新】广域网技术
The controversial line of energy replenishment: will fast charging lead to reunification?
Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"
Introduction of time related knowledge in kernel
RecastNavigation 之 Recast
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
Developers, MySQL column finish, help you easily from installation to entry