103 The data type of the field in sql

1. Number
Integer:
tinyint,
int, integer The width specified in the parentheses is only the display width, the storage width is fixed, other types are the storage width
bigint

小数:create table t7(x float(255,30),y double(255,30),decimal(65,30))
float,小数位最多30,
double, 小数位最多30,整数位225
decimal 小数位最多30,整数位35
虽然都是支持30位,但float和double小数点的精度不准

2. String
char(10): fixed length, fill in spaces if it is not enough; waste space, fast reading speed
varchar: variable length, reserve 1-2bytes to store the length of the real data; it seems to save space and read speed Slow
ps: When the amount of stored data just reaches the storage width limit, varchar actually costs more space.
Summary: In most cases, the amount of data stored does not reach the width limit, so in most cases, varchar saves more space
but saves space Not the key, the key is the improvement of io efficiency brought by space saving, which in turn improves query efficiency

验证:
create table t10(x char(5));
create table t11(x varchar(5));

insert t10 values("我爱你 ");
insert t11 values("你爱他 ");

t10字符个数 5 字节个数 11
t10字符个数 4 字节个数 10

select char_length(x) from t10;  --查看字符个数:3 他加一个空格但查长度时,他又给拿走了
select char_length(x) from t11;  --查看字符个数:4

set sql_mode="pad_char_to_full_length";  --这样t10就显示长度为5了

select length(x) from t10;  --查看字节数:11
select length(x) from t11;  --查看字节数:10

3. Time type
year range (1901/2155)
time hour: minute: second ('-838:59:59/838:59:59')
date year: month: day
datetime year: month: day hour: minute: second (8 bytes, you can add not null to make it not empty, default now() on update now()
timestamp year: month: day hour: minute: second (4 bytes, it is not allowed to be empty, if you pass in empty Value, put the current time by default, if this record is updated, the time will be automatically updated

实例:
create table student(
id int,
name char(10),
born_year year,
birthday date,
reg_time datetime);

insert student values(2,"Gimgong","1998","1998-01-12",now());

create table t9(
id int,
name varchar(16),
-- update_time datetime not null default now() on update now(),
update_time timestamp,
reg_time datetime not null default now());

4. Enumeration type (gender, choose one more than one) and collection type (multiple selection and more)
Enum type enum("m", "f", "t") one more
collection type set("m", "f ","T") Multiple select multiple

Guess you like

Origin blog.csdn.net/qq_40808228/article/details/108347958