对数据类型的约束 - 联合索引 - 存储引擎
1.对数据类型的约束
# ### 1. 约束 : 对数据的一种限制,不满足约束条件的数据会报错
unsigned : 无符号
not null : 不为空
default : 默认值
unique : 唯一值 唯一索引
primary key : 主键
auto_increment : 自增加一
zerofill : 0填充
foreign key : 外键
# unsigned : 无符号
# 顺序 : 字段名 字段类型 字段约束
create table t3(id int unsigned); #不能为负数
create table t3_1(id int);
insert into t3 values(-100); error
insert into t3_1 values(-100);
# not null : 不为空
create table t4(id int not null , name varchar(100));
insert into t4 values(1,"卓培峰");
insert into t4 values(null,"卓培峰"); # error
insert into t4 (name) values("卓培峰"); # error 默认添空
# default : 默认值
create table t5(id int not null , name varchar(255) default "李炯辉");
insert into t5 values(1 , null); #null也是一个值
insert into t5(id) values(2); #只有什么都不写的时候,填写默认值
# unique : 唯一值 ,默认创建一个唯一的[索引]
#作用:加快查询速度,适当的加索引可以加快速度,加的太多适得其反.
"""key = UNI : 当前这个字段唯一,不能有重复值 , 但是可以为null"""
create table t6(id int unique , name char(255) default "非非");
insert into t6(id) values(1);
insert into t6(id) values(1); # error Duplicate entry 重复输入
insert into t6(id) values(null); # success
insert into t6(id) values(null); # success
# primary key : 主键 [唯一 + 不能空] 一个表里只能有一个主键
"""key = PRI : 当前这个字段是主键,标记一条记录的唯一性"""
create table t7(id int primary key );
insert into t7(id) values(1);
insert into t7(id) values(1); error
insert into t7(id) values(null); error
# 主键 <=> [unique + not null]
create table t8(id int unique not null ); #相当于主键
# 主键 + [unique + not null] 同时存在时,会把id2显示成主键
create table t9(id1 int unique not null , id2 int primary key);
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id1 | int(11) | NO | UNI | NULL | |
| id2 | int(11) | NO | PRI | NULL | |
+-------+---------+------+-----+---------+-------+
# 一个表里面只能有一个字段为单个主键
create table t10(id1 int primary key , id2 int primary key);
# error Multiple primary key defined 定义了多个主键
# auto_increment : 自增加一 (特指: 配合主键,和唯一索引使用)
create table t11(id int primary key auto_increment );
# 自动插入数据,
insert into t11 values(); #从1开始插入 前面没有数据
insert into t11 values(null);
insert into t11 values(10);
insert into t11 values(); # 从11开始插入 前面有数据就自动加1插入
# 删除数据
delete from t11; # 不能重置自动加一效果
# 删除数据 + 重置id
truncate table t11;
# zerofill : 0填充 只对整型有用
create table t12(id int(8) zerofill );
insert into t12 values(123);
+----------+
| id |
+----------+
| 00000123 |
+----------+
# 字符串类型中,括号中数字是有意义的,限制字符长度
char(255) max <= 255
varchar(21845) max < 21845 #(内部会占用一定空间保留数据的长度)
# ### 额外补充
# 关于约束的添加和删除
# 1 添加/删除 约束 not null
#alter table 表名 modify 字段名 类型
alter table t1 modify id int not null
alter table t1 modify id int
# 2 添加/删除 unique 唯一索引
# alter table 表名 add unique(id)
alter table t1 add unique(id)
alter table t1 drop index id
# 3 添加/删除 primary key
# alter table 表名 add primary key(id);
alter table t1 add primary key(id);
alter table t1 drop primary key;
# 4 添加/删除 foreign key 外键 (先通过查看建表语句 找到外键名字,然后再删)
show create table student1; # 查看建表语句
# CONSTRAINT `student1_ibfk_1` FOREIGN KEY (`classid`) REFERENCES `class1` (`id`)
alter table student1 drop foreign key student1_ibfk_1; #删除
alter table student1 add foreign key(classid) references class1(id) #添加
2.联合索引-外键
# 1.联合唯一索引
"""unique(字段1,字段2,字段3 .... ) 多个字段合在一起表达该数据的唯一性"""
# key = PRI
create table t1(ip varchar(15) not null , port int not null , unique(ip,port) )
insert into t1 values("192.168.107.128",3306);
insert into t1 values("192.168.107.129",80);
insert into t1 values("192.168.107.129",80); # error
insert into t1_server values(null,null); # error
insert into t1_server values(); # error
# key = MUL
create table t2_(ip varchar(15) , port int , unique(ip,port) )
insert into t2 values(null,null); # success
insert into t2 values(); # success
show create table t2 ; #查看建库语句 UNIQUE KEY `ip` (`ip`,`port`)
# 2.联合主键索引 key = PRI
"""primary key(字段1,字段2,字段3 .... ) 把几个字段合在一起表达数据的唯一性"""
create table t3 (ip varchar(15) , port int , primary key(ip,port) )
insert into t3 values("192.168.107.128",3306);
insert into t3 values("192.168.107.128",20);
insert into t3 values(); # error 联合字段自动加 (not null + unique)
show create table t3 # 查看建库语句 PRIMARY KEY (`ip`,`port`)
# 3.联合普通索引 key = MUL
"""index(字段1,字段2,字段3 .... ) 把几个字段合在一起,加快查询速度"""
create table t4 (ip varchar(15) , port int , index(ip,port) )
show create table t4 ; #查看建库语句 KEY `ip` (`ip`,`port`)
# 4.foreign key 外键:把多张表通过一个关联字段联合在一起,这样的字段可以设置成外键(好处:可以联级更新,联级删除)
# 语法 : foreign key(字段1) references 表(字段)
# 条件 : 带有关联关系的字段,套上外键的约束;
# 要求 : 被关联的字段,必须具有唯一性
student1:
id name age classid
1 wangwen 18 1
2 heye 80 1
3 wangyongjuan 15 2
class1:
id classname
1 python33
2 python34
# (1).只关联,不联级更新删除
# 创建class1
create table class1(id int , classname varchar(255));
# 临时更改id 为unique
alter table class1 add unique(id);
# 插入数据
insert into class1 values(1,"python33");
insert into class1 values(2,"python34");
# 创建student1
create table student1(
id int primary key auto_increment,
name varchar(255) ,
age int ,
classid int ,
foreign key(classid) references class1(id) #把classid与class1表中的id相关联
)
# 插入数据
insert into student1 values(null,"wangwen",18,1);
insert into student1 values(null,"heye",80,1);
insert into student1 values(null,"wangyongjuan",15,2);
delete from class1 where id = 1; 删不掉 (需要先删掉带有关联关系的数据)
delete from student1 where id = 1;
delete from student1 where id = 2;
# (2). 联级更新和删除 (谨慎使用)
"""
联级更新 : on update cascade
联级删除 : on delete cascade
"""
# 创建 class2
create table class2(id int primary key , classname varchar(255));
# 插入数据
insert into class2 values(1,"python33");
insert into class2 values(2,"python34");
# 创建 student2
create table student2(
id int primary key auto_increment,
name varchar(255) ,
age int ,
classid int ,
foreign key(classid) references class2(id) on update cascade on delete cascade
)
# 插入数据
insert into student2 values(null,"wangwen",18,1);
insert into student2 values(null,"heye",80,1);
insert into student2 values(null,"wangyongjuan",15,2);
# 可以直接联级删除
delete from class2 where id = 1; #student2 中 classid = 1 的数据也跟着同步删除了
# 可以连接更新
update class2 set id=900 where classname = "python34";
# student2 中 classid 的数据也跟着同步更新
# (3) 表之间的关系
(1) 一对一 : 把表1 id name sex guanlian 表2 id height bloodtype
(2) 一对多(多对一) : 班级和学生之间, 班级是1,学生是多
(3) 多对多 : 学员可以学习很多学科, 一个学科也可以被很多学员学习 (多对多关系表一定要创建第三张关系表)
3.储存引擎 : 储存数据的方式
# 存储引擎 : 存储数据的方式
"""
主数据库: 增删改 用户访问量较少
从数据库: 查询 用户访问量较大
"""
# show engines 查看存储引擎
表级锁 : 只有一个线程做数据的更改,就会锁表
行级锁 : 只对当前这行修改的数据上锁,其他行数据仍可使用
事务处理 : 在执行sql语句时,必须全部成功,才最后commit提交数据,否则rollback回滚数据;
begin : 开启事务处理
commit : 提交数据
rollback : 回滚
MyISAM : 表级锁 (mysql5.5之前 默认引擎)
InnoDB : 行级锁 , 事务处理 , 外键 (mysql5.5之后 默认引擎)
MEMORY : 把数据存储在内存中,应用在缓存;
BLACKHOLE : anything you write to it disappears #写入的数据都会消失
作用: 不存储数据,主要用来同步主从数据库,转发binlog日志;
create table myisam1(id int ,name varchar(255) ) engine=myisam;
myisam1.frm 表结构
myisam1.MYD 表数据
myisam1.MYI 表索引
create table innodb1(id int ,name varchar(255) ) engine=innodb;
innodb1.frm 表结构
innodb1.ibd 表数据 + 表索引
create table memory1(id int ,name varchar(255) ) engine=memory;
memory1.frm 表结构
create table blackhole1(id int ,name varchar(255) ) engine=blackhole;
blackhole1.frm 表结构