三十四、字段类型
字段类型:
整型
浮点型
字符类型
日期类型
枚举与集合类型
约束条件
primary key
unique
not null
default
存储引擎
不同的数据应该有不同的处理机制
mysql存储引擎
Innodb:默认的存储引擎 查询速度较myisam慢 但是更安全
myisam:mysql老版本用的存储引擎
memory:内存引擎(数据全部存在内存中)
blackhole:无论存什么 都立马消失(黑洞)
研究一下每个存储引擎存取数据的特点
show engines;
创建表的完整语法:
#语法: create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型[(宽度) 约束条件] );
在创建表的时候有三点注意地方:
1.在同一张表内字段名不能重复 2.字段和类型必须要有的,而宽度和约束条件是可选 3.最后一个字段后面不能加逗号
#宽度:对存储数据的限制
create database day38; use day38; create table t1(name char); insert into t1 values('jason'); #1.能够成功但是显示的时候只显示一位 #2.第二种情况,直接报错 select * from t1; # 约束条件 create table t2(name char(16) not null); 字段类型限制的是存储数据的类型 约束条件是基于字段类型之上的附加额外限制
整型
分类:TINYINT SMALLINT MEDIUMINT INT BIGINT 作用:年龄,等级等信息 TINYINT 默认有正负号 create table t3(id tinyint); create table t4(id tinyint unsigned); # 无符号 int类型 create table t5(id int); 创建表为id 为int类型 默认带负号 alter table t5 modify id int unsigned; 修改为无符号 #给整型设置宽度 create table t6(id int(8)); 特例:整型字段在设置宽度的时候,限制不是存储宽度而是显示宽度 如果存入的数字够8位 正常显示 ,如果不够8位默认用空格填充 char_length() 获取字段数据对应的长度 select char_length(id) from t6; create table t6(id int(8) zerofill); 不够8位默认用0填充 在创建整型字段的时候,不需要指定字段宽度(整型显示宽度)
模糊匹配
mode like 模糊查询 % 匹配任意数量的任意字符 _ 匹配单个数量的任意字符 show variables like '%mode%';
#设置严格模式
set session 当前窗口有效 set global 全局有效,终生有效 set global sql_mode='STRICT_TRANS_TABLES'; 设置完成后需要退出客户端,不需要重启
浮点型
分类: float double decimal 作用:体重,身高,薪资 浮点类型(5,3) #总长度为5位,其中小数位是3位,整数位2位 float(255,30) double(255,30) decimal(65,30) 三者区别: 精度不同: 精度由低到高:float double decimal create table t8 (id float(255,30)); create table t9 (id double(255,30)); create table t10 (id decimal(65,30));
字符类型
char:定长 varchar:变长 create table t11(name char(4)); #name字段固定长度,如果超了直接报错,如果不够默认用空格填充 create table t12(name varchar(4)); #name字段如果超了直接报错,如果超了直接报错,如果不够按实际长度存储; insert into t11 values('a'): #'a ' insert into t12 values('a'): #'a' char_length(name) # 统计字段值所对应的的长度 select char_length(name) from t11; select char_length(name) from t12; # 这个是修改操作 严格模式 set global sql_mode='strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH';
char 与 varchar
char(4) 定长 优点:存取速度快 缺点:浪费空间 varchar(4)变长 给varchar类型的数据自动加上报头用来标识数据的长度,报头通常占1~2bytes 优点:节约空间 缺点:存取速度慢
日期类型
date 年月日 time 时分秒 datetime 年月日时分秒 year 年 create table t13(id int,birth date ,class_time time, reg_time datetime,years year); insert into t13 values(1,'2019-8-20','15:12:11','2019-8-20 15:12:11','2019');
枚举与集合
枚举(enum):多选一 create table t14(id int,name char(16),gender enum('male','female','others')); insert into t14 values(1,'engon','male'); 集合(set):多选多 create table t15(id int,name char(16),gender enum('male','female','others'),hobby set('read','girl','made love')); insert into t15 values(1,'engon','male','girl,made love');
约束条件
约束条件: primary key (PK) 标识该字段为表的主键,可以唯一的标识记录 foreign key (FK) 标识该字段为该表的外键 not null 标识该字段不能为空 unique key (UK) 标识该字段的值是唯一 auto_increment 标识该字段的值自动增长(整数类型,而且为主键) default 为该字段设置默认值 UNSIGNED 无符号 zerofill 使用0填充
not null 限制字段不能为空 create table t16(id int,name char(4) not null);
default: 设置默认字段 create table t17(id int,name char(4) not null,gender enum('male','female','others' ) default 'male'); insert into t17 values(1,json); # 报错 需要指定 insert into t17(id ,name) values(1,'json'); # 指定传两个参数 通常情况下not null 和default 是配合使用
单列唯一 unique 某个字段对应的值在当前表中是唯一的 create table t18(id int unique,name char(16)); insert into t18 values(1,'zhugou'),(1,'json'); # id会报错,唯一的 联合唯一 服务器ip和端口port create table t19(id int unique,host char(16),port int,unique(host,port)); insert into t19 values(1,'127.0.0.1',8080),(2,'127.0.0.1',8081); insert into t19 values(3,'127.0.0.1',8080),(4,'127.0.0.1',8080); #报错
primary key
主键:相当于一本新华字典里面目录,方便快速查询某个信息 单从限制条件上来说,它就相当于not null +unique create table t21(id int primary key,name char(16))engine=innodb; innodb引擎中,所有的表都必须有且只有一个主键,他是innodb引擎用来组织数据的依据
强调(*********):
1.一张表中必须有且只有一个主键,如果你没有主键,那么会从上到下搜索直到遇到一个非空且唯一字段自动将其设置为主键 create table t20(id int,name char(16),age int not null unique,addr char(16) not null unique)engine =innodb; 2.如果表里面没有指定任何可以设置为主键的字段,那么innodb会采用自己默认的一个隐藏字段, 作为主键,隐藏意味着你在查询的时候无法根据这个主键字段加速查询了 索引:类似于书的目录,没有主键相当于一页一页翻着查 3.一张表中通常都应有一个id字段,并且通常将id字段作成主键
auto_increment
自增 (主键id写完之后自动加1) create table t22(id int primary key auto_increment,name char(16)); insert into t22 values('json'),('tank');
联合主键
create table t23(ip char(16),port int,primary key(ip,port)); desc t23; #删除后主键,后面添加数据会跳过该id号,往后添加,可以truncate t23;清空表里面所有内容 # Mysql面试题 https://www.cnblogs.com/lijiasnong/p/9963905.html