当前位置:网站首页>MySQL basic learning
MySQL basic learning
2022-07-26 05:07:00 【nineteen million nine hundred and twenty-one thousand one hundr】
0、MySQL Database introduction
0.1、 Common types of databases
Relational type : Have a watch 、 Have a library , To store and express the relationship between data .
oracle、mysql、sql server、sqlite
⾮ Relational type : Store according to key value , No table
Redis、Mongodb0.2、sql Language classification

0.3、 Commonly used sql sentence
Fuzzy query
arbitrarily ⼀ Characters :_
Search for two word names ⽣ select * from students where name like '__'
Any number of characters :%
Query the surname Bai and age ⼤ On 20 Science ⽣ select * from students where name like ' hundred %' and age>20
Continuous range between Start and end
select * from students where age not between 20 and 25
Inquire about 1 Except class, other classes learn ⽣ The average age of 、 most ⼤ Age 、 most ⼩ Age
select class, avg(age),max(age),min(age) from students group by class having
class != "1 class "
where and having The difference between ?
where It's right from Data filtering is performed on the specified table , It belongs to the screening of original data .
having It's right group by Results of .having In the following conditions, you can use the aggregate function ,where You can't .
Pagination
Computation first ⼏⻚ Get data formula :
select * from Table name limit (n-1)*m,m n: The first ⼏⻚ m: Every time ⻚ Show multiple
Internal connection another ⼀ Species writing
select * from students,scores where students.studentNo=scores.studentno
Conclusion : production ⽣ Cartesian product , Of board ⾏ Speed will ⾮ Often slow . Forbid ⽌
⼦ Inquire about
sql Nested in statement sql sentence
⼦sql Statements either serve as conditions , want ⻔ Act as a data source
select * from scores where studentNo = (select studentNo from students where
name = ' Wang Zhaojun ’)
Inquire about 18 Years old school ⽣ The achievement of , Ask to show the result ( Column ⼦ Inquire about )
select * from scores where studentNo in (select studentNo from students where
age=18)
I'm in the same class as Wang Zhaojun 、 Study at the same age ⽣ Information (⾏⼦ Inquire about )
select * from students where (class,age)=(select class,age from students where
name=' Wang Zhaojun ')
Act as a data source
select * from scores inner join (select * from courses where name in (" data
library "," The system test ")) temp on temp.courseNo=scores.courseNo
List each department ⻔ in ⼯ information ⼩ In this department ⻔ Average ⼯ Endowment member ⼯ Information
select e.empname full name ,s.salary ⼯ information ,d.deptname Ministry ⻔, temp.avg_salary Ministry ⻔ Average salary
from employees e
inner join departments d
on e.deptid=d.deptid
INNER JOIN salary s
on s.empid=e.empid
inner join (select d.deptid,avg(s.salary) avg_salary from employees e inner
join departments d on e.deptid=d.deptid INNER JOIN salary s on
s.empid=e.empid group by d.deptname) temp
on temp.deptid=d.deptid
where s.salary< temp.avg_salary
1、 Common commands
Login related
mysql -u root -p Log in locally MySQL
mysql -h 182.167.12.3 -u user1 -p -P 3308 Appoint IP Address and port number login MySQL database
Delete
delete from Table name where Field = value ; Delete ⼀ Records or partial records
truncate table Table name ; Delete all records ( Truncation table )
drop table Table name ; Delete table
delete And truncate The difference between ?
delete from Table name
explain : Primary key ⾃ increase ⻓ Not from 1 Start , You can make ⽤where Conditions
truncate table Table name
explain : Retention structure , Primary key ⾃ increase ⻓ from 1 Start , Don't make ⽤where Conditions , Speed ⽐delete fast
keyword
show databases, See which libraries are currently available
use Database name ; Choose to make ⽤ library ;
create database Library name charset=utf8;⾃⼰ newly build ⼀ Databases
drop database Library name ; Delete database ;
select database(); Confirm which library is currently selected
show tables, View all tables under the current library
desc Table name , Display table design
show create table Table name , Show the table creation statements, including indexes
Data constraints
1、 Primary key (primary key): Only ⼀ identifier
2、⾮ empty (not null): This field is not allowed to fill in null value
3、 But ⼀(unique): Duplicate values are not allowed for this field
4、 The default value is (default): When this value is not filled in, it will make ⽤ The default value is , If it is filled in, the filling shall prevail
5、 Foreign keys (foreign key): Maintain the association between two tables 2、 Data type and corresponding length
2.1、 Integers
The integer type is a numeric value without a decimal part , You can set signed and unsigned types ( It's a positive number )

2.2、 decimal
MySQL Floating point numbers and fixed-point numbers are used in to represent decimals .
There are two floating point types , They are single precision floating-point numbers (FLOAT) And double precision floating point numbers (DOUBLE); There is only one fixed-point type , Namely DECIMAL.
Both floating-point and fixed-point types can be used (M, D) To express , among M It's called precision , The total number of digits ;D Is called a scale , Represents the number of decimal places .
The value range of floating-point number type is M(1~255) and D(1~30, And can't be greater than M-2), Indicates the display width and decimal places respectively .M and D stay FLOAT and DOUBLE Is optional ,FLOAT and DOUBLE The type will be saved to the maximum precision supported by the hardware .DECIMAL Default D The value is 0、M The value is 10.
FLOAT The value range of type is as follows :
Signed value range :-3.402823466E+38~-1.175494351E-38.
The range of unsigned values :0 and -1.175494351E-38~-3.402823466E+38.
DOUBLE The value range of type is as follows :
Signed value range :-1.7976931348623157E+308~-2.2250738585072014E-308.
The range of unsigned values :0 and -2.2250738585072014E-308~-1.7976931348623157E+308.
The advantage of floating-point numbers over fixed-point numbers is that when the length is fixed , Floating point numbers can represent a larger range ; The disadvantage is that it will cause accuracy problems .
Last but not least : stay MySQL in , The number of fixed points is stored as a string , When the accuracy requirements are relatively high ( Such as currency 、 Scientific data ), Use DECIMAL Type is better , The other two floating-point numbers are also prone to problems in subtraction and comparison , So when you use floating-point numbers, you need to pay attention to , And try to avoid floating-point comparison .
2.3、 The date type
MySQL There are several data types representing dates in :YEAR、TIME、DATE、DTAETIME、TIMESTAMP. When only year information is recorded , You can just use YEAR type .

2.4、 String type
MySQL The string types in are CHAR、VARCHAR、TINYTEXT、TEXT、MEDIUMTEXT、LONGTEXT、ENUM、SET etc. .VARCHAR(M) Is a variable length string ,M Indicates the length of the largest column ,M The range is 0~65535. The length limit cannot exceed 65535 Bytes . According to character set , If the character type is gbk, Each character occupies 2 Bytes , The maximum length cannot exceed 65535/2 =32766; If the character type is utf8, Each character takes up a maximum of 3 Bytes , The maximum length cannot exceed 65535/3 =21845
2.5、TEXT type
TEXT Column holds non binary strings , Such as the content of the article 、 Comments, etc . When saving or querying TEXT Column value , Do not delete trailing spaces .TEXT Types are divided into 4 Kind of :TINYTEXT、TEXT、MEDIUMTEXT and LONGTEXT. Different TEXT The storage space and data length of types are different .
TINYTEXT The length is 255(28-1) Character TEXT Column .
TEXT The length is 65535(216-1) Character TEXT Column . Note that this unit is a character , If the character is GBK Then for varchar Of 2 Times the length ,UTF-8 be 3 Times the length
MEDIUMTEXT The length is 16777215(224-1) Character TEXT Column .
LONGTEXT The length is 4294967295 or 4GB(232-1) Character TEXT Column .
2.6、ENUM type
ENUM Values are internally expressed as integers , Each enumeration value has an index value ; The member values allowed by the list value are from 1 Numbered starting ,MySQL This index number is stored , Enumerations can have at most 65535 Elements .
2.7、 Binary string type
Binary string types are sometimes referred to directly as “ Binary type ”.MySQL Binary strings in are BIT、BINARY、VARBINARY、TINYBLOB、BLOB、MEDIUMBLOB and LONGBLOB.

2.8、BLOB type
BLOB It's a binary object , Used to store a variable amount of data .

边栏推荐
- [weekly translation go] how to write your first program with go
- MODFLOW Flex、GMS、FEFLOW、HYDRUS实践应用
- AQS唤醒线程的时候为什么从后向前遍历,我懂了
- Redis解决库存超卖问题
- Vector explanation and iterator failure
- 地球系统模式(CESM)实践技术
- There was an unexpected error (type=Method Not Allowed, status=405).记录报错
- 分布式ID的常用解决方案-一把拿下
- 黑吃黑?男子破解赌博网站漏洞,每月“薅羊毛”10多万元
- Security permission management details
猜你喜欢

I talked with the interviewer about MySQL optimization in five dimensions

can 串口 can 232 can 485 串口转CANbus总线网关模块CAN232/485MB转换器CANCOM

提高shuffle操作中的reduce并行度

Vector explanation and iterator failure

JVM第二讲:类加载机制

MySQL基础学习

Embedded sharing collection 20

C language - pointer one touch ※

Several maturity levels of using MES management system

如何优雅的复现YOLOv5官方历程(二)——标注并训练自己的数据集
随机推荐
Excel VBA: summarize calculation output results by date (SUMIF)
“双碳”目标下资源环境中的可计算一般均衡(CGE)模型实践技术
Add and modify the verification logic, and use -validation- to complete the group verification
[acwing] 1268. Simple questions
新导则下的防洪评价报告编制方法及洪水建模
Briefly describe the application fields of WMS warehouse management system
MySQL eight knowledge points: from getting started to deleting the database
C语言力扣第41题之缺失的第一个正数。两种方法,预处理快排与原地哈希
Install nccl \ mpirun \ horovod \ NVIDIA tensorflow (3090ti)
有ggjj看看这个问题没,是否缓存导致跨域问题?
Minipcie interface can card solves the problem of industrial computer expanding can channel minipcie can
嵌入式分享合集21
Security permission management details
AXI协议(5):AXI协议的burst机制
An online accident, I suddenly realized the essence of asynchrony
黑吃黑?男子破解赌博网站漏洞,每月“薅羊毛”10多万元
时代潮流-云原生数据库的崛起
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
Google Emoji guessing game helps parents guide their children to surf the Internet safely
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly 100 times the improvement of analysis performance