目录
string types 字符串类型
char() 针对固定长度
varchar() 针对变长 -- 用户名、密码 usually varchar(50) 地址之类-- varchar(255)
MediumBlob 最大长度 16777215 个字节(2^24-1) 16MB 16*1024*1024
LongText 最大长度4294967295个字节 (2^32-1) 4GB 4*1024*1024*1024
numeric types 数值类型
integer types 整数类型
Fixed-point and floating-point types 定点数类型和浮点数类型
decimal() 小数型 ;精确值-- 小数点后有固定位数的数字,比如货币值,
替代词: dec 、numeric、fixed
用于科学计算 ; 近似值
float 4b
double 8b
boolean types 布尔类型
enum and set types 枚举和集合类型 – 限定字符串选择范围 – 少用,可以单独建立一个查询表解决需求
enum('small','medium','large')
set()
date and time types
常使用时间戳timestamp 4b 记录某行插入或最近更新的时间
Bolb types
Json types
JSON(JavaScript Object Notation) 是一种通过网络存储和传输数据的轻量级 数据交换格式。易于人阅读和编写。同时也易于机器解析和生成
语法
Json 使用 {} 定义 对象
{
-- key为字符串
"key":value,
}
在products 表中添加一列为 properties 格式为 json
update products
set properties = '{
"dimensions" : [1,2,3],
"weight":10,
"manufacturer":{"name":"sony"}
}'
where product_id = 1
或
update products
set properties = '{
"dimensions" : [1,2,3],
"weight":10,
"manufacturer":{"name":"sony"}
}'
where product_id = 1
select product_id,
json_extract(properties,'$.weight') as weight
-- json_extract(json_obj,path)
-- $ 表示当前json文档 ;(.)访问单独的属性或键
from products
where product_id = 1
select product_id,
properties -> '$.weight' as weight,
properties -> '$.dimensions' as dimensions,
properties -> '$.manufacturer' as manufacturer,
properties -> '$.manufacturer.name' as name,
-- '->>' 克服字符串输出的引号
properties ->> '$.manufacturer.name' as name_no
from products
where product_id = 1;
select product_id from products
where properties ->> '$.manufacturer.name' = 'sony'
update products
set properties = json_set( -- 更新或添加新属性
properties,
'$.weight',20,
'$.age',10 -- 新增属性
)
where product_id = 1;
update products
set properties = json_remove(
properties,'$.age'
)
where product_id = 1;
传送门
Sql Server之数据类型详解
SQL 数据类型
MySQL中tinytext、text、mediumtext和longtext等各个类型详解
SQL:BLOB 和 TEXT
JSON 语法