一、Int類型
/前後分別爲有符號和無符號大小
類型 | 字節 | 最小值 | 最大值 |
---|---|---|---|
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 |
1、有符號和無符號數據
create table test_unsigned(a int unsigned, b int unsigned);
insert into test_unsigned values(1, 2);
select b - a from test_unsigned;
select a - b from test_unsigned; --運行出錯
在日常使用中一般使用bigInt有符號數據
2、INT(N)是什麼?
- int(N)中的 N 是顯示寬度, 不表示 存儲的數字的 長度 的上限。
- zerofill 表示當存儲的數字 長度 < N 時,用 數字 0 填充左邊,直至補滿長度 N
- 當存儲數字的長度 超過 N 時 ,按照實際存儲 的數字顯示
create table test_int_n(a int(4) zerofill);
insert into test_int_n values(1);
insert into test_int_n values(123456);
select * from test_int_n;
結果爲 :0001、123456
3、小甜點(面試可能會問到)
// 會報錯,自增只能是主鍵
create table test_auto_increment(a int auto_increment);
// 正常
create table test_auto_increment(a int auto_increment primary key);
// 插入主鍵爲1的數據
insert into test_auto_increment values(NULL);
// 插入不成功
insert into test_auto_increment values(0);
// 可以插入成功
insert into test_auto_increment values(-1);
// 插入的結果爲1,10,100,101,102
insert into test_auto_increment values(null),(100),(null),(10),(null)
二、字符類型
字符的排序規則
// 結果爲1
select 'a' = 'A';
create table test_ci (a varchar(10), key(a));
insert into test_ci values('a');
insert into test_ci values('A');
// 結果爲a,A
select * from test_ci where a = 'a';
// 修改排序規則後變爲a
set names utf8mb4 collate utf8mb4_bin
三、時間類型
datatime 與 timestamp 區別
create table test_time(a timestamp, b datetime);
insert into test_time values (now(), now());
select * from test_time;
// 查詢時區
select @@time_zone;
// 修改時區
set time_zone='+00:00';
// 發現timestamp值改變了,它是和時區有關聯的,並且大小隻到2038年左右
select * from test_time;
四、Json類型
// 新建表
create table json_user (
uid int auto_increment,
data json,
primary key(uid)
);
//插入兩條數據
insert into json_user values ( null,
'"name":"lison", "age":18, "address":"enjoy"}' );
insert into json_user values ( null,
'{"name":"james", "age":28, "mail":"[email protected]"}');
1、json_extract提取函數
提取json串中的值
select
json_extract(data, '$.name'), json_extract(data, '$.address')
from json_user;
2、JSON_OBJECT將對象轉爲 json
insert into json_user values (
null,
json_object("name", "enjoy", "email", "enjoy.com", "age",35) )
3、json_insert插入數據
update json_user set data = json_insert(data, "$.address_2", "xiangxue") where uid = 1;
4、json_merge合併數據並返回
select
json_merge(
json_extract(data, '$.address'), json_extract(data, '$.address_2'))
from json_user where uid = 1;
其他函數:mysql官方地址
5、Json索引
JSON 類型數據本身 無法直接 創建索引,需要將需要索引的JSON 數據 重新生成虛擬列(Virtual Columns) 之後,對該列進行索引
create table test_inex_1(
data json,
gen_col varchar(10) generated always as (json_extract(data, '$.name')),
index idx (gen_col)
);
insert into test_inex_1(data) values ('{"name":"king", "age":18, "address":"cs"}'); insert into test_inex_1(data) values ('{"name":"peter", "age":28, "address":"zz"}');
select * from test_inex_1;
select json_extract(data,"$.name") as username from test_inex_1 where gen_col='"king"';