当前位置:网站首页>Basic data types of MySQL
Basic data types of MySQL
2022-07-04 10:22:00 【Java full stack preacher】
What is the data type ? To put it bluntly, it is the storage format of data 、 Constraints and effective range .
Selection of data type , That is to specify the underlying data storage structure of different data , Directly affect the performance of the database .
The selection of data type is also an important point of database performance tuning , Learning data types carefully is also a preparation for learning database tuning in the future .
Start learning how to fish ……
How do I know which data types there are in the database ?
I talked about how to check it quickly in this article MySQL help : When I forget SQL How to write , I ……
Check the data types
Specific syntax and instructions for each data type
You can see the data type :int Specific syntax and instructions ( Mainly the effective range )
All the data types listed here can be used to view the specific syntax and description in this way , Therefore, there is no need to deliberately remember the specific details of these data types , Remember and forget , Learning how to check is more meaningful than this .
Next, I will explain several commonly used data types .
1. value type
1 Bytes =8 Bit binary ,8 position =2^8=256, therefore 1 Total length of bytes =256
1111 1111=255, therefore 1 The maximum stored value of bytes is 255
among “0000 0000”=0 It's also a value ,1+255=256=2^8
So you know 1 Total length of bytes and Storing maximum values is not the same concept !!! Total length = Maximum +1
Unsigned maximum calculation formula :n byte =(2^n*8)-1
Both integer and point types can be constrained :[UNSIGNED] [ZEROFILL] Namely “ Unsigned ” and “ Zero compensation ”.
Integer and point type syntax : Basic grammar [UNSIGNED] [ZEROFILL] UNSIGNED Only non negative numbers can be stored ZEROFILL Automatically add zero in front of the stored value to fill the remaining digits , And automatically add UNSIGNED attribute
1.1 Examples of integer types
How to choose ? The main Look at the effective range Whether it meets the business requirements
-- int type , Unsigned , Not empty , Auto increment
CREATE TABLE animals (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
-- Insert multiple data , No need to give id assignment
INSERT INTO animals (name)
VALUES
('dog'),('cat'),('penguin'),('lax'),('whale'),('ostrich');
For general automatic increment ,int unsigned Can be stored 42 More than 100 million pieces of data , It's enough to use , No need to use bigint type . Use bigint Type means , The primary key of each data Need to use more than twice as much space and more computing resources .
1.2 Examples of point types
create table t1( id1 float(9,2), id2 double(9,2), id3 decimal(9,2));
It can be seen that the grammar is the same
insert into t1 values('1.2','1.24','1.235');
Meeting Automatically add decimal places 、 Beyond the decimal places rounding
among DECIMAL All basic calculations of columns (+、-、*、/) Are subject to 65 The accuracy of digits is completed . For storage Amount type , Be sure to use decimal Type to store .DECIMAL The maximum range is :DECIMAL(65,30).
2. Character type
char Characteristics :
Save fixed length M
The saved value is greater than M Will be cut off , Less than will be filled with spaces
Maximum character length 255
varchar Characteristics :
Only store the actual length of the string
Use extra 1~2 Bytes to store the length of the value (0~255 Use 1 byte , otherwise 2 byte )
Maximum character length 65535
Key points
char and varchar The length of refers to “ Character length ”, Instead of byte length , Different encoding requires different byte lengths
latin1 Occupy 1 Bytes ,gbk Occupy 2 Bytes ,utf8 Occupy 3 Bytes ,utf8mb4 Occupy 4 Bytes
When using gbk When coding ,char The maximum character length is 255;varchar Maximum character length 32766
When using utf8 When coding ,char The maximum character length is 255;varchar Maximum character length 21844
When using utf8mb4 When coding ,char The maximum character length is 255;varchar Maximum character length 16383
Example :
Such as :char(3), Fixed storage of three characters , Different codes Required for fixing Number of bytes :latin1 need 3 Bytes ,gbk need 6 Bytes ,utf8 need 9 Bytes .
Such as :varchar(3), Store up to three characters , Different codes Maximum required Number of bytes :latin1 need 4 Bytes ,gbk need 7 Bytes ,utf8 need 10 Bytes .
Such as :varchar(256), Maximum storage 256 Characters , Different codes Maximum required Number of bytes :latin1 need 258 Bytes ,gbk need 514 Bytes ,utf8 need 770 Bytes .
-- grammar
CHAR[(M)] [CHARACTER SET charset_name] [COLLATE collation_name]
VARCHAR(M) [CHARACTER SET charset_name] [COLLATE collation_name]
-- Or you could write it as
CHAR[(M)] [CHARSET charset_name] [COLLATE collation_name]
VARCHAR(M) [CHARSET charset_name] [COLLATE collation_name]
How to choose char and varchar
Fixed length option char, Not affected by coding . Such as :uuid character string ,md3 character string ;
Choose one that often changes length varchar. Such as : Home address , Personal profile .
3. Time type
3.1 timestamp and datetime
See the examples in the table for the specific format of time type , among timestamp and datetime Can be accurate to seconds
Example
timestamp By default, it will be automatically updated to the current time ,datetime The default is empty. null, But you can specify the default value when you see the table , Make both the same default .
-- CURRENT_TIMESTAMP Insert the default current time
-- on update CURRENT_TIMESTAMP The default time when updating
create table time2(
id int,
ts timestamp,
dt datetime default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
);
timestamp and datetime difference
The number of bytes required is different ,timestamp 4 Bytes ,datetime 8 Bytes
The effective range is different ,timestamp Maximum 2038 year ,datetime 9999 year
MySQL How to represent the current time in ?
In addition to the default values mentioned above CURRENT_TIMESTAMP
There are also the following
CURRENT_TIMESTAMP()
NOW()
LOCALTIME
LOCALTIME()
LOCALTIMESTAMP
LOCALTIMESTAMP()
Remember which one is simple , Remember function now() Just fine , Others look familiar .
3.2 date and time
There's nothing to talk about , Just remember the data format , See the table above for details
Look at an example
3.3 year
Year fixed 4 position , Let's look at an example
create table year1 ( id int,y year );insert into year1 (id,y) values (1,now()),(2,2022-12-30),(3,2022),(4,22);
Follow official account for free PDF E-books and read more related articles
边栏推荐
- OSPF summary
- Vanishing numbers
- Tables in the thesis of latex learning
- How to teach yourself to learn programming
- Modules golang
- Dos:disk operating system, including core startup program and command program
- Ruby时间格式转换strftime毫秒匹配格式
- Doris / Clickhouse / Hudi, a phased summary in June
- Latex insert picture, insert formula
- 入职中国平安三周年的一些总结
猜你喜欢
Pcl:: fromrosmsg alarm failed to find match for field 'intensity'
MongoDB数据日期显示相差8小时 原因和解决方案
Basic principle of servlet and application of common API methods
Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
What are the advantages of automation?
Fabric of kubernetes CNI plug-in
C language pointer interview question - the second bullet
Doris / Clickhouse / Hudi, a phased summary in June
libmysqlclient. so. 20: cannot open shared object file: No such file or directory
Qtreeview+ custom model implementation example
随机推荐
Idea SSH channel configuration
Velodyne configuration command
Two way process republication + routing policy
RHCE day 3
Exercise 7-4 find out the elements that are not common to two arrays (20 points)
What is devsecops? Definitions, processes, frameworks and best practices for 2022
Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
Exercise 8-7 string sorting (20 points)
MySQL case
Ruby time format conversion strftime MS matching format
Fabric of kubernetes CNI plug-in
Rhsca day 11 operation
Mmclassification annotation file generation
2. Data type
2020-03-28
OSPF summary
How to teach yourself to learn programming
Kotlin: collection use
uniapp---初步使用websocket(长链接实现)
leetcode1229. Schedule the meeting