当前位置:网站首页>MySQL (II) - MySQL data type
MySQL (II) - MySQL data type
2022-06-23 07:16:00 【The bad guy is stupid】
One 、INT type
| type | byte | minimum value ( A signed / Unsigned ) | Maximum ( A signed / Unsigned ) |
|---|---|---|---|
TINYINT | 1 | -128 / 0 | 127 / 255 |
SMALLINT | 2 | -32768 / 0 | 32767 / 65535 |
MEDIUMINT | 3 | -8388608 / 0 | 8388607 / 16777215 |
INT | 4 | -2147483648 / 0 | 2147483647 / 4294967295 |
BIGINT | 8 | -9223372036854775808 / 0 | 9223372036854775807 / 18446744073709551615 |
notes : Usual use BIGINT Are signed .
1、 Create a table with unsigned fields
CREATE TABLE test_unsigned (a INT UNSIGNED,b INT UNSIGNED);
insert into test_unsigned values(1,2);
select a-b from test_unsigned; # Due to the use of unsigned types ,a-b=-1, Beyond the int Type precision , So this SQL Report errors 2、INT(N) What is it? ?
create table test_int_n(a int(4) zerofill);
insert into test_int_n values(1);
insert into test_int_n values(123456);
mysql> select * from test_int_n;
+--------+
| a |
+--------+
| 0001 |
| 123456 |
+--------+
#######################
int(N) Medium N It's the display width , Does not mean Stored digital length Upper limit .
zerofill Represents when the stored number length < N when , use Numbers 0 Fill left , Until the full length N
When storing the length of a number exceed N when , according to Actual storage The digital display of
3、 Self increasing
Are there any mistakes in the following grammar ?
create table test_auto_increment(a int auto_increment); # error , Auto increment must be added to the primary key
create table test_auto_increment(a int auto_increment primary key);# correct
following SQL What is the result of the operation ?
insert into test_auto_increment values(NULL); # 1
insert into test_auto_increment values(0);
insert into test_auto_increment values(-1); #-1
insert into test_auto_increment values(null),(100),(null),(10),(null);
#####
If the insert value is 0 or null Is self increasing , If it is a negative number, you can insert .
Two 、 Character
| type | explain | N The meaning of | Whether there is a character set | Maximum length |
| CHAR(N) | Fixed length characters | character | yes | 255 |
VARCHAR(N) | Variable length characters | character | yes | 16384 |
BINARY(N) | Fixed length binary bytes | byte | no | 255 |
VARBINARY(N) | Variable length binary bytes | byte | no | 16384 |
TINYBLOB(N) | Binary big object | byte | no | 256 |
BLOB(N) | Binary big object | byte | no | 16K |
MEDIUMBLOB(N) | Binary big object | byte | no | 16M |
LONGBLOB(N) | Binary big object | byte | no | 4G |
TINYTEXT(N) | Big object | byte | yes | 256 |
TEXT(N) | Big object | byte | yes | 16K |
MEDIUMTEXT(N) | Big object | byte | yes | 16M |
LONGTEXT(N) | Big object | byte | yes | 4G |
1、 Sort rule ,bin Store every character in the string in binary data , Case sensitive ,cici Case insensitive ,ci by case insensitive Abbreviation , That is, the case is not sensitive ;
select 'a' ='A';
create table test_ci(a varchar(10),key(a));
insert into test_ci values('a');
insert into test_ci values('A');
select * from test_ci where a = 'a'; -- What's the result ?
set names utf8mb4 collate utf8mb4_bin
select * from test_ci where a = 'a'; -- What's the result ?
3、 ... and 、 The date type
| The date type | Occupancy space | Scope of representation |
DATETIME | 8 | 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59 |
DATE | 3 | 1000-01-01 ~ 9999-12-31 |
TIMESTAMP | 4 | 1970-01-01 00:00:00UTC ~ 2038-01-19 03:14:07UTC |
YEAR | 1 | YEAR(2):1970-2070, YEAR(4):1901-2155 |
TIME | 3 | -838:59:59 ~ 838:59:59 |
notes :datetime No time zone ,timestamp Time zone
create table test_time(a timestamp,b datetime);
insert into test_time values(now(),now());
select * from test_time;
set time_zone='+00:00';
select * from test_time;Four 、json type
1、JSON Use of type
create table json_user (uid int auto_increment,data json,primary key(uid));
insert into json_user values (null, '{"name":"li","age":18,"address":"beijing"}' );
insert into json_user values (null,'{"name":"zhang","age":28,"mail":"[email protected]"}');
2、JSON Type dependent functions
json_extract: extract
select json_extract(data, '$.name'),json_extract(data, '$.address') from json_user;
+------------------------------+---------------------------------+
| json_extract(data, '$.name') | json_extract(data, '$.address') |
+------------------------------+---------------------------------+
| "li" | "beijing" |
| "zhang" | NULL |
+------------------------------+---------------------------------+
JSON_OBJECT: Convert object to json
insert into json_user values ( null,json_object("name", "wangwu", "email", "[email protected]", "age",35) );
json_insert: insert data
json_merge: Merge the data and return
Other functions :https://dev.mysql.com/doc/refman/5.7/en/json-function-reference.html
3、JSON Index of type
JSON Type data itself cannot be indexed directly , Need to index JSON Data regenerates virtual columns (Virtual Columns) after , Index the column .
create table test_inex_1(
data json,
gen_col varchar(10) generated always as (json_extract(data, '$.name')),
index idx (gen_col)
);
边栏推荐
- 295. median data flow
- [game theory] basic knowledge
- 900. RLE 迭代器
- Advanced drawing skills of Excel lecture 100 (VIII) -excel drawing WiFi diagram
- In depth learning series 46: face image super score gfp-gan
- npm下载报错npm ERR code ERESOLVE
- Nacos适配oracle11g-修改Nacos源码
- Traversal of binary tree and related knowledge
- TP6 安装拓展
- 896. 单调数列
猜你喜欢

深度学习系列46:人脸图像超分GFP-GAN

406-双指针(27. 移除元素、977.有序数组的平方、15. 三数之和、18. 四数之和)

Learning and using quartz scheduling framework

407-栈与队列(232.用栈实现队列、225. 用队列实现栈)

GIS实战应用案例100篇(七十九)-多规整合底图的制作要点

Analysis of personalized learning progress in maker Education

Tp6+redis+think-queue+supervisor implements the process resident message queue /job task

初始化层实现

U-Net: Convolutional Networks for Biomedical Image Segmentation

junit单元测试报错org.junit.runners.model.InvalidTestClassError: Invalid test class ‘xxx‘ .No runnable meth
随机推荐
The List
MySQL mvcc multi version concurrency control
[system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed
什么是分布式?
In depth learning series 47:stylegan summary
[AI practice] xgb Xgbregression multioutputregressor parameter 2 (GPU training model)
MySQL(五) — 锁及事务
深度学习系列47:styleGAN总结
pspnet完整代码实现
Arthas thread command locates thread deadlock
paddle版本问题
深度学习系列47:超分模型Real-ESRGAN
899. ordered queue
MYSQL牛客刷题
‘latin-1‘ codec can‘t encode characters in position 103-115: Body (‘一串中文‘) is not valid Latin-1
Paddle version problem
C # how to obtain DPI and real resolution (can solve the problem that has been 96)
312. 戳气球
闫氏DP分析法
【AI实战】XGBRegressor模型加速训练,使用GPU秒级训练XGBRegressor