当前位置:网站首页>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
边栏推荐
- gatling 之性能测试
- New technology releases a small program UNIPRO to meet customers' mobile office scenarios
- Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
- Clever use of curl command
- Master the use of auto analyze in data warehouse
- [unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
- 无心剑中译伊丽莎白·毕肖普《一门技艺》
- 就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
- Developers, MySQL column finish, help you easily from installation to entry
- To sort out messy header files, I use include what you use
猜你喜欢
Hidden corners of coder Edition: five things that developers hate most
Ks007 realizes personal blog system based on JSP
【华为HCIA持续更新】SDN与FVC
补能的争议路线:快充会走向大一统吗?
Vscode modification indentation failed, indent four spaces as soon as it is saved
Electronic pet dog - what is the internal structure?
【HCIA持续更新】WLAN工作流程概述
Recast of recastnavigation
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
随机推荐
【HCIA持续更新】网络管理与运维
【每日一题】556. 下一个更大元素 III
Recast of recastnavigation
People in the workplace with a miserable expression
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
android使用SQLiteOpenHelper闪退
Introduction of time related knowledge in kernel
wuzhicms代码审计
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
【HCIA持续更新】WLAN概述与基本概念
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
[daily question] 556 Next bigger element III
MVC mode and three-tier architecture
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
使用3DMAX制作一枚手雷
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
Is BigDecimal safe to calculate the amount? Look at these five pits~~
78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
如何进行MDM的产品测试
华为云ModelArts的使用教程(附详细图解)