当前位置:网站首页>[database] addition, deletion, modification and query of MySQL table (basic)
[database] addition, deletion, modification and query of MySQL table (basic)
2022-07-22 17:46:00 【Hao Shaokang】
MySQL Table addition, deletion, modification and query ( Basics )
List of articles
- CRUD;create( newly added ),retrieve( retrieval ),update( to update ),delete( Delete ). ‘
Configure the default character set
Insert a record :insert into student values(20,“haoshaokang”);
Be careful : Chinese strings cannot be inserted here at the beginning , because mysql The default character set for databases and servers is Latin , So we can change the character set to utf8, One way is to set the character set when creating the database , such as :create database java104 character set utf8;
View the default character set commands :show variables like “character%”;
You can see that the default character set of the database and server are Latin
But there is another way to do it once and for all : Modify the configuration file , Set the default character set of database and server to utf8. The profile name is :(my.ini) route :C:\ProgramData\MySQL\MySQL Server 5.7( This computer path ).
- Back up the configuration file first
- Modify the configuration file
Save configuration file
Restart the server
Be careful : The previously created database still uses the previous character set , Only the newly created character set will be used utf8.
After changing the configuration file , Let's check the default character set again :
so , The database and server default character sets have become utf8;
The new data
insert into student values (22,“ Hao Shaokang ”); ( Insert a record )
insert into student values (22,“ Hao Shaokang ”),(12,“ Li Si ”); ( Insert multiple records ).
insert into student(name) values(“ Hao Shaokang ”),(“ Li Si ”); ( Insert multiple records in the specified column );
- stay MySQL Insert one record at a time , Inserting multiple records at a time is much slower than inserting multiple records at a time .
- Because every time the client sends a request to the server , Server resolution SQL After the statement , Start the operation , And return the operation result to the client .
- One by one, there will be multiple requests / Respond to . Just insert once, and there is only one request / Respond to .
- And the insertion operation requires certain traversal operations in the database , Find the right place . Insert more than once , The server needs to do many preparations .
Query data
Full column query
*select * from student; ( Full column query )( Represent wildcard )
- however select * Operation may be risky . Because if there is a lot of data in the database ( Hundreds of millions ), The server needs to find data from the disk , Then return to the client through the network , In this process, it is possible to put the disk IO Full , Or eat up the network bandwidth . If your server is an online server , Other users may have a bad experience .
- In case of execution select * operation , Sure ctrl+c Cancel operation .
Specified column query
select Name , Name from student; ( Specified column query )
- select All operation results are presented in a temporary table , It will not change the data in the database .
Query with expression
select id ,name,chinese+math+english from exam_result;
select id,name,math+10 from exam_result;
Alias query
select id,name,chinese+math+english as total from exam_result;
select id,name,chinese+math+english total from exam_result;
( Is there any as Fine )
Go to check again
select distinct math from exam_result; (math Remove the weight in the column )
select dintinct math,english from exam_result; (math and english It's all the same before going heavy )
select distinct * from exam_result; ( Only when all the columns are the same can the weight be removed )
Sort query
select * from exam_result order by math; ( The default is ascending sort )
select * from exam_result order by math desc; ( null )
select * from exam_result orde by math desc,chinese;
- Specify multi column sorting , If the data in the first column is equal , Then sort by the second column .
select name,chinese+math+english as total from exam_result order by total desc;
- Specify alias sorting .
NULL When operating with other data types , The result is NULL
Conditions of the query
Basic query
- Filter the data in the table according to the conditions , And return to the temporary table .
select * from exam_result where math > 60;
select * from exam_result where chinese > english;
select name,chinese+math+english as total from exam_result where chinese+math+english < 200;
- where Alias cannot be used after .
select * from exam_result where math > 60 and english > 70; (and Stands for logic and )
select * from exam_result where math > 60 or english > 70; (or For logic or )
- and Has a higher priority than or
Interval query
select * from exam_result where math between 60 and 80; ( Screening mathematics in 60 To 80 Between the records )( Closed interval )
select * from exam_result where math in (60,78,63); ( Screening mathematics is 60,78,63 One of the records )
Fuzzy query
Use wildcards ,% Replace any character ,_ Represents an arbitrary character
select * from exam_result where name like “ Grandchildren %”; ( Names beginning with sun )
select * from exam_result where name like “% Grandchildren ” ( The name ending with sun )
select from exam_result where name like “% Grandchildren %” ( Include sun's name )*
select * from exam_result where name like “ Grandchildren _”;
NULL Query for
select * from exam_result where math is not NULL;
select * from exam_result where math is NULL;
- is and is not Can only be used to query NULL.
- Use = and NULL Can't match correctly ,NULL=NULL The result is NULL
- Use <=> You can talk to NULL Right match ,NULL<=>NULL The result is true
Paging query
Let the query result , Take out only part of it , So as to reduce the cost , Speed up query
select * from exam_result limit 3 offset 3; ( Limit the query of three records , Offset from the first record 3 Position start ).
select * from exam_result limit 3;( From the first record ).
- Query operation can also add conditions [where] [order by] [limit]
Modifying data
update exam_result set math = 30 where name = “ The Monkey King ”;
update exam_result set math = 50,chinese = 70 where name = “ Cao mengde ”;
update exam_result set math = math-30 order by chinese+math+english limit 3; ( The last three students in the total score minus 30)
update exam_result set chinese = chinese - 10; ( All students' Chinese minus 10)
- Conditions can also be added to the modification operation [where] [order by] [limit]
Delete data
delete from exam_result where name = “ The Monkey King ”;
delete from exam_result order by math limit 3; ( Delete the record of the last three students in Mathematics ).
delete from exam_result; ( Delete the entire table data )
- Delete operation can also set conditions [where] [order by] [limit]
边栏推荐
- Li Hongyi machine learning 2020--p20 & 21 RNN
- MySQL constraint_ Primary key constraint primary key
- Detailed explanation of inheritance
- 【HMS core】【push kit】关于消息分类问题的集合
- sqlmap的打开方式以代码的形式打开不是以图像形式打开
- Why do some parameters reload can take effect, while some parameters must restart the database?
- flinksql 的task可以监控是否中断吗?
- Oracle怎么设置创建时不去检查编译错误?
- Guys, when Flink SQL job submits a job to yarn, it reports an SQL error that cannot be executed. If it is executed locally, it does not report an error. The server
- [HMS core] [Health Kit] [FAQ] collection of data subscription function questions
猜你喜欢
Data Lake (18): Flink and iceberg integrate SQL API operations
[Digital IC] understand Axi protocol in simple terms
Mongodb query statement >, & gt;=、& lt;、& lt;=、=、!=、 In, not in usage introduction
[external sorting] fast sorting idea completes external sorting
NFS Shared Storage Service
Chery Xingtu's product plan was exposed, and the 2.0T turbocharged engine was launched at the end of the year
How does win11 close the touch pad? Three solutions for closing the touch panel in win11
关键路径实现
On the dilemma faced by non transferable reputation points NFT SBTS
2022-07-21:给定一个字符串str,和一个正数k, 你可以随意的划分str成多个子串, 目的是找到在某一种划分方案中,有尽可能多的回文子串,长度>=k,并且没有重合。 返回有几个回文子串。 来
随机推荐
Conference OA project introduction & Conference release
MySQL series article 4: execution plan
Take CRM system as an example to explain data analysis (importance introduction and analysis method)
备战攻防演练,这里有一张腾讯安全重保布防图!
(十一)51单片机——用AT24C02实现存储秒表数据(附成果展示)
Default constraint of MySQL constraint default
[leetcode] 814. Binary tree pruning
Nightmare of concurrent programs -- data competition
Instruction arrangement problem
[external sorting] fast sorting idea completes external sorting
Win11显示麦克风未插上怎么办?Win11显示麦克风未插上的解决方法
ACM warm-up exercise 3 in 2022 summer vacation (summary of some topics)
为什么有些参数reload就可以生效,而有些参数必须重启数据库?
有人知道oracle cdc这个问题吗?source没有空值,但是查询定义的cdc表时说有空值,让修
Discussion on the path of Chinese enterprise management software towards globalization and internationalization
How is VR panorama displayed in all walks of life? How to implement the application?
Hcip OSPF interface network type experiment report
How does win11 close the touch pad? Three solutions for closing the touch panel in win11
Misc进阶
【云原生】Docker部署数据库的持久化