data YYYY-MM-DD
time HH:MM:SS
year YYYY
datetime YYYY-MM-SS HH:MM:SS
timestamp
datetime
create table t5(d date, t time, y year, dt datetime);
insert into t5 values("2020-06-17","09:15:39","2020","2020-06-17 09:15:39");
insert into t5 values(now(),now(),now(),now());
timestamp YYYYMMDDHHMMSS 可以自动更新时间,不需要手动写入,修改数据的时候,记录最后一次修改时间
create table t6(dt datetime, ts timestamp);
insert into t6 values(null,null)
insert into t6 values(20200617092430,20200617092430)
now 获取当前时间,select now()
"""记笔记,不要操作。你跟不上"""
unsigned 无符号
not null 不能为空
default 设置默认值
unique 唯一约束/索引 , 数据唯一不能重复
primary key 主键,标记某一条数据的唯一特征(唯一且不为空的数据)
auto_increment 自增加1 (一般配合主键使用,或者unique进行自增)
zerofill 0填充,配合int类型使用,比如int(6),位数不够6位,拿0来补充
foreign key 外键,把多张表通过一个关联字段,联合在一起
1.unsigned
create table t7(id int unsigned);
insert into t7 values(66);
insert into t7 values(-66); error
2.not null
create table t8(id int not null, name varchar(255))
desc t8
insert into t8 values(1,"xx");
select * from t8;
insert into t8 values(null,"xx"); error
insert into t8(name) values("xx"); error
3.default
create table t9(id int not null, name varchar(255) default "xboy");
desc t9;
insert into t9 values(1,null);
select * from t9;
insert into t9(id) values(1);
4.unique
"""
索引:相当于字典的目录,通过索引可以加快我们的查询速度;索引的创建会增加文件大小
适当的索引对我们有利
UNI:唯一索引,允许塞空值,所以我们出现了主键,主键不允许塞空值,且唯一
"""
create table t10(id int unique,name char(10) default "liuwei");
desc t10;
insert into t10(id) values(1);
select * from t10;
insert into t10(id) values(1);
select * from t10;
insert into t10(id) values(12);
insert into t10(id) values(null);
5.primary key 主键,标记某一条数据的唯一特征(唯一且不为空的数据)
create table t11(id int not null unique, name char(10) default "liuwei");
insert into t11 values(1,"nihao");
insert into t11 values(null,"nihaoa");
caeate table t12(id int primary key, name char(10) default "liuwei");
desc t12;
insert into t12 values(1,"aaa");
create table t13(id int primary key, name char(10) not null unique);
desc t13;
6.auto_increment
create table t14(id int primary key auto_increment, name char(10) default "xiechen");
desc t14;
insert into t14 values(1,"zhangsan");
select * from t14;
insert into t14 values(null,"wangwen");
insert into t14(id) values(null);
select * from t14;
insert into t14 values();
select * from t14;
delete from t14;
select * from t14;
insert into t14 values(null);
truncate table t14;
7.zerofill
create table t15(id int(6)zerofill);
desc t15;
insert into t15 values(2);
insert into t15 values(2222);
insert into t15 values(2222222222);
"""
MUL 代表普通索引
UNI 唯一索引、
PRI 主键索引
"""
create table t1_server(id int primary key auto_increment, server_name char(10) not null, ip char(15) not null, port int not null,unique(ip,port));
insert into t1_server values(null,"aaa","182.168.56.31",5000);
desc t1_server;
192.168.56.31 5000
192.168.56.31 6000
192.168.56.40 6000
192.168.56.31 5000
create table t2_server(id int, server_name char(10) not null, ip char(15), port int,unique(ip,port));
insert into t2_server values(1,"aaa","182.168.56.31",5000);
insert into t2_server values(1,"aaa",null,null)
desc t1_server;
create table t1_server(id int, server_name char(10) not null, ip char(15) not null, port int not null,unique(ip,port));
slter table t3_server add primary key(id);
unique(ip,port)
primary key(ip,port)
8.foreign key 把多张表通过一个关联字段,联合在一起
"""
外键的要求:要求关联的字段必须具有一个唯一属性(unique或者primarykey)
"""
student
id name age classname address
1 wang 80 py30 beijing1
2 lin 800 py30 dongbei
3 wen 18 py31 neimenggu
student1
id name age address classid
1 wang 80 beijing1 1
2 lin 800 dongbei 1
3 wen 18 neimenggu 2
class1:
id classname detetime
1 python30 2020-01-01 09:09:09
2 python31 2020-03-01 09:09:09
create table class1(id int, classname varchar(255));
alter table class1 add unique(id);
create table student1(
id int primary key auto_increment,
name varcahr(255)not null,
age int not null,
classid int,
foreign key(classid) references class1(id)
);
insert into class1 values(1,"python30");
insert into class2 values(2,"oython31");
insert into student1 values(null,"wang",80,2);
insert into student1 values(null,"xiao",800,1);
insert into student1 values(null,"wen",18,2);
delete from class1 where id = 1;
delete from student1 where id = 2;
delete from class1 where id = 1;
"""
练级删除 on delete cascade
联机更新 on update cascade
"""
create table class2(id int unique,classname varchar(255));
create table student2(
id int primary key auto_increment,
name varcahr(255)not null,
age int not null,
classid int,
foreign key(classid) references class2(id) on delete cascade on update cascade
);
insert into class2 values(1,"python30");
insert into class2 values(2,"oython31");
insert into student2 values(null,"wang",80,2);
insert into student2 values(null,"xiao",800,1);
insert into student2 values(null,"wen",18,2);
delete from class2 where id = 2;
update class2 set id = 100 where classname = "python";
1.一对一 表一很长,把表一切成2两个,然后关联字段,两张表凑在一起
2.一对多 一个班级里面可以对应多个学生,可以把学生作为主动关联的表,后面关联班级的字段
3.多对多 一个学生可以对应多个学科,一个学科也可以被多个学生学习
xueke 表1
id name
1 math
2 english
3 wuli
student 表2
id name
1 xboy
2 wei
3 wang
relation 表3 把xid 和 sid 设置成外键,用多的关联少的,把少的字段作为关联字段
关联xueke的id,和student的id
xid sid
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
存储引擎是存储数据的结构方式
show engines; 查看存储引擎
1.表级锁:如果有人修改当前这个表,会直接上表锁,其他人无法修改;在编辑数据时,速度慢,不能高并发
2.行级锁:如果有人修改当前表中的一条记录,只对当前这条记录数据上锁,不影响其他人修改其他条数据;速度快,允许高并发
3.事务处理:如果执行sql语句,在全部成功之后,再选择提交数据,有一条失败,立刻回滚,恢复到原来状态
begin:开始事务
commit:提交数据
rollback:回滚数据 删的表或者库回滚也不好使
InnoDB:5.6版本后的默认存储引擎,支持事务处理,行级锁,外键
MEMORY:把数据放在内存中,用作缓存
BLACKHOLE:黑洞,用来同步主从数据库中的数据,场景发生在服务器并发集群(主数据库:查询;从数据库:增删改)主从之间加个黑洞
MyISAM:5.6版本前的默认存储引擎,支持表级锁
create table myisam1(id int,name char()) engine = myisam;
create table innodb1(id int ,name char(10)) engine = innodb;
innodb1.frm 表结构
innodb1.ibd 表数据+表索引
负载均衡系统,动态分配服务器
底层算法,取余。保证每个服务器并发压力相对均衡