当前位置:网站首页>MySQL basic knowledge learning (I)
MySQL basic knowledge learning (I)
2022-07-27 08:49:00 【Never stop learning】
Catalog
One 、 Core elements of relational database
Two 、SQL Main classification of language
5、 ... and 、 frequently-used SQL Language ( Case insensitive )
6、 ... and 、 Common field constraints
7、 ... and 、 Conditions of the query
11、 ... and 、 Data paging display
One 、 Core elements of relational database
- database ( Set of tables , There can be multiple tables in a database )——database
- surface ( A two-dimensional table consisting of rows and columns ), The basic unit of data stored in a database ——table
- That's ok ( Record ), A line in the table , It's called a record in a database ——record
- Column ( Field ), A column in the table , It's called a field in a database ——field
Two 、SQL Main classification of language
- DQL: Data query language , Used to query data , Such as select;
- DDL: Data definition language , Do database 、 The management of the table , Such as create、drop;
- DML: Data operation language , Add to the data 、 Delete 、 Change , Such as insert、delete、update;
- TPL: The language of things , Deal with things , Include begin transaction,commit,rollback;
3、 ... and 、 notes
- Single-line comments :-- Content of notes
- Multiline comment :/* Content of notes */
Four 、 Common data types
- Integers :int
- Small integers :tinyint
- decimal :decimal
- character string :varchar
- Date time :datetime
5、 ... and 、 frequently-used SQL Language ( Case insensitive )
1、 Create table :create table
create table Table name (
Field name data type constraint ,
Field name data type constraint
...
);2、 Add data :insert
- Type 1 : Set values for all fields , The order of values corresponds to the order of fields in the table
insert into Table name values(...);- Type 2 : Some fields set values , The order of the values corresponds to the order of the fields given
insert into Table name ( Field 1, Field 2,...) values( value 1, value 2,...);- One insert Statement to add multiple records , The data are separated by English commas
insert into Table name values(...),(...)...; -- All fields
insert into Table name ( Field 1,...) values( value 1,...),( value 1,...)...; -- Partial field 3、 Simple query :select
- Query all fields
select * from Table name ;
- Query the specified field
select Field 1, Field 2,... from Table name ;
select Field 1 as Alias , Field 2 as Alias ,... from Table name as Alias ; -- Alias fields and tables ,as It can be omitted notes :distinct It can eliminate the duplication of query results , Add to select after
4、 Modifying data :update
update Table name set Field 1= value 1, Field 2= value 2... where Conditions ;5、 Delete records in table :delete
delete from Table name where Conditions ;6、 Delete records in table :truncate
truncate table Table name ; -- Delete the entire table directly truncate And delete The difference between :
- In speed ,truncate>delete;
- If you want to delete some data, use delete, Pay attention to take where Words and expressions ;
- If you want to keep the table and delete all the data , The self growth field is restored from 1 Start , use truncate;
7、 Delete table :drop table
drop table Table name ;
drop table if exists Table name ; -- First judge whether the table exists , Then execute the delete command 6、 ... and 、 Common field constraints
1、 Primary key (primary key): Value cannot be duplicate ,auto_increment The representative value increases automatically
create table Table name (
Field name data type primary key auto_increment,
Field name data type constraint
...
constraint primary key( Field name ) -- Primary keys can also be created at the end , Choose one of them
);notes : When inserting, the value of the primary key self growth field can be replaced by a placeholder ,0 or null
2、 Non empty (not null): This field is not allowed to fill in null value
create table Table name (
Field name data type not null,
...
);3、 only (unique): Duplicate values are not allowed for this field
create table Table name (
Field name data type unique,
...
);4、 The default value is (default): When this value is not filled in, the default value is used , If it is filled in, the filling shall prevail
create table Table name (
Field name data type default value ,
...
);7、 ... and 、 Conditions of the query
1、 Comparison operator
- be equal to :=
- Greater than :>
- Greater than or equal to :>=
- Less than :<
- Less than or equal to :<=
- It's not equal to :!= or <>
2、 Logical operators
- and( And ),and There are two conditions , Must satisfy at the same time
- or( or ),or There are two conditions , Meet one of them
- not( Not ),not There is only one condition , The result is opposite to the condition
3、 Fuzzy query
- like
- % Represents any number of arbitrary characters , coordination like Use
- _ Represents an arbitrary character , coordination like Use
4、 Range queries
- in In a discontinuous range ,not in Indicates that it is not in the discontinuous range ,in( value 1, value 2,...)
- between...and... In a continuous range ,not between... and... Means not in a continuous range
5、 Empty judgment
- Judge empty :is null
- Judgment is not empty :is not null
Be careful :null And '' Is different ,null Means nothing ,'' The representative length is 0 String
8、 ... and 、 Sort
- asc( The default value is ) Sort from small to large , In ascending order
- desc Sort from large to small , In descending order
select * from Table name where Conditions
order by Field 1 asc|desc, Field 2 asc|desc,... -- The default is ascending ,asc Don't write , Fields have high priority before Nine 、 Aggregate functions
1、count Total number of records
- count(*) Indicates the total number of records calculated , Write... In brackets * And field name , The result is the same
2、max Maximum
- max( Field ) Indicates to find the maximum value of this field
3、min minimum value
- min( Field ) Means to find the minimum value of this field
4、sum Sum up
- sum( Field ) Means to sum this field
5、avg Average
- avg( Field ) Means to find the average value of this field
Be careful :(1) Aggregate function cannot be in where Used in the following conditions
(2) Aggregate functions cannot appear in query results at the same time as ordinary fields
(3)count Functions do not ignore null values null, The rest ignore null values null
Ten 、 The data packet
1、 grouping (group by)
- Group by fields , Indicates that the same data in this field will be placed in a group
- The purpose of grouping is to cooperate with the aggregation function , The aggregation function will count the data of each group separately
select Field 1, Field 2, Aggregate functions ...
from Table name
where constraint condition
group by Field 1, Field 2...2、 Data filtering after grouping (having)
select Field 1, Field 2, Aggregate functions ...
from Table name
where constraint condition
group by Field 1, Field 2...
having Field 1,... polymerization ...Be careful : Use having You must group before group by
contrast where And having:
- where It's right from The table specified later performs data filtering , It belongs to the screening of original data
- having It's right group by Results of
- having Aggregate functions can be used in the following conditions ,where The following conditions cannot use aggregate functions
11、 ... and 、 Data paging display
1、 Get some lines
- limit Start , Get the number of lines
- from start Start , obtain count Data
- start Index from 0 Start , If omitted start The default from the 0 Start
select * from Table name limit start,count2、 Pagination
- When there are too many records in a table , You need to use paging display
- It is known that : Each page shows m Data , seek : Query the first n Pages of data
select * from Table name limit (n-1)*m,m边栏推荐
- “寻源到结算“与“采购到付款“两者有什么不同或相似之处?
- NIO this.selector.select()
- [I2C reading mpu6050 of Renesas ra6m4 development board]
- Transaction, order system add transaction
- Realize SPU management in the background
- Interface test tool - JMeter pressure test use
- What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
- General view, DRF view review
- Creation and simple application of QPushButton button button
- Aruba学习笔记10-安全认证-Portal认证(web页面配置)
猜你喜欢

众昂矿业:新能源行业快速发展,氟化工产品势头强劲

【进程间通信IPC】- 信号量的学习

网络IO总结文

How to upload qiniu cloud

Low cost, low threshold, easy deployment, a new choice for the digital transformation of 48 million + small and medium-sized enterprises

Solution of database migration error

4274. 后缀表达式

Minio 安装与使用

vscod

How to view instances of software objects in QSIM?
随机推荐
JWT authentication and login function implementation, exit login
Flask's operations on model classes
4275. Dijkstra序列
Creation and simple application of QPushButton button button
4279. 笛卡尔树
Matlab 利用M文件产生模糊控制器
MySQL Express
Make a game by yourself with pyGame 01
691. Cube IV
2034: [Blue Bridge Cup 2022 preliminary] pruning shrubs
ROS2安装时出现Connection failed [IP: 91.189.91.39 80]
Aruba学习笔记10-安全认证-Portal认证(web页面配置)
4277. Block reversal
Implementation of queue (sequential storage, chain storage)
“寻源到结算“与“采购到付款“两者有什么不同或相似之处?
[flutter -- geTx] preparation
tensorflow包tf.keras模块构建和训练深度学习模型
Background order management
3428. 放苹果
The shelf life you filled in has been less than 10 days until now, and it is not allowed to publish. If the actual shelf life is more than 10 days, please truthfully fill in the production date and pu