mysql-数据类型,类型约束,联合唯一约束,表与表之间的关系,存储引擎---day36

# ### char varchar(补充)
char     字符长度  255个
varchar  字符长度  21845个


# ### part1  数据类型 -时间
date YYYY-MM-DD  年月日(结婚纪念日,节假日)
time HH:MM:SS    时分秒(体育竞赛)
year YYYY        年份值(历史,1882年拉菲)
datetime YYYY-MM-DD HH:MM:SS 年月日 时分秒(登录时间,下单时间)

    create table t5(d date,t time,y year,dt datetime);
    insert into t5 values("2020-06-17","09:15:30","2020","2020-06-17 09:15:30");
    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);
    insert into t6 values(20990617092430,20990617092430)#error timestamp 不能超过2038年


#mysql 内置函数
now 获取当前时间  select now();

# ### part2 约束:对编辑的数据进行类型限制,不满足约束条件的直接报错
    unsigned    无符号
    not null    不为空
    default     设置默认值
    unique      唯一约束,数据唯一不能重复
    primary key 主键,标记数据的唯一特征(唯一且不为空的数据)
    auto_increment 自增加一(一般配合主键使用 或者 unique进行自增)
    zerofill    0填充(配合int类型使用),int(6),位数不够位,拿0来补充
    foreign key  外键,把多张表通过一个关键字段,联合在一起

#unsigned  无符号
    create table t7(id int unsigned);
    insert into t7 values(66);
    insert into t7 values(-66); error

#not null  不为空
    create table t8(id int not null,name varchar(255));
    insert into t8 values(1,"小林林");
    insert into t8 values(null,"小林林"); error
    insert into t8(name) values("小林林"); error

#default  设置默认值
    create table t9(id int not null,name varchar(255) default "王振");
    insert into t9 values(1,null);
    insert into t9(id) values(1);

#unique   唯一约束,数据唯一不能重复
'''
索引:相当于字典的目录,通过索引可以加快查询速度
UNI 唯一索引,允许塞null空值
'''
    create table t10(id int unique,name char(10) default "刘伟");
    insert into t10(id) values(1);
    insert into t10(id) values(1); #error 不能重复
    insert into t10(id) values(12);
    insert into t10(id) values(null);
    insert into t10(id) values(null);

#primary key 主键,标记数据的唯一特征(唯一且不为空的数据)[创建表: 字段 类型 约束 ...]
'''PRI主键 非空且唯一,在一个表里面,只能有一个字段是主键'''
create table t11(id int not null unique,name char(10) default"刘伟");
insert into t11 values(1,"你好");
insert into t11 values(null,"你好啊"); #error

#primary key 创建主键
create table t12(id int primary key,name char(10) default "刘伟");
insert into t12 values(1,"aaa");

#两者同时存在(优先显示primary key 作为主键,另外一个被标记成UNI 唯一索引)
create table t13(id int primary key,name char(10) not null unique);

#一个表里面只能有一个主键
create table t13(id int primary key,name char(10) primary key);#error


#auto_increment 自增加一(一般配合主键使用或者unique进行自增)
create table t14(id int primary key auto_increment,name char(10) default"刘伟");
insert into t14 values(1,"刘伟");
insert into t14 values(2,"刘伟");
insert into t14 values(null,"刘伟");
insert into t14(id) values(null);
#使用默认值进行插入
insert into t14 values();

#delete 单纯的删除数据,数据id号从上一个继续自增
delete from t14;
#turncate 删除所有数据,id从头开始(重置表)
truncate table t14;


#zerofill  0填充(配合int类型使用),int(6) 位数不够位,拿0来补充
create table t15(id int(6),zerofill);
insert into t15 values(2);
insert into t15 values(222);
insert into t15 values(22222222);


# ### part3
#1.联合唯一约束:unique(字段1,字段2,字段3...)把多个字段拼在一起表达唯一的数据
'''
MUL代表普通索引 UNI唯一索引  PRI主键索引
'''
#(1)联合唯一索引(都为非空的字段 显示的PRI,联合在一起做的主键,不是单个字段的主键)
create table t1_server(id int,server_name char(10) not null,ip char(15) not null,port int not null,unique(ip,port));
insert into t1_server values(1,"aaa","192.168.56.31",5000);
insert into t1_server values(1,"aaa","192.168.56.31",6000);
insert into t1_server values(1,"aaa","192.168.56.40",6000);

#(2)联合唯一索引(为空的字段,允许插入null,显示MUL)
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","192.168.56.31",5000);
insert into t2_server values(1,"aaa","192.168.56.31",6000);
insert into t2_server values(1,"aaa","192.168.56.40",6000);
insert into t2_server values(1,"aaa",null,null); #注意点,可以插入多个空值;


#(3)联合唯一索引 和主键是否可以同时存在?可以 primary key是真正的单个字段主键,联合唯一索引变成MUL;
'''一个时PRI  一个MUL'''
create table t3_server(id int,server_name char(10) not null,ip char(15) not null,port int not null,unique(ip,port));
alter table t3_server add primary key(id);
'''
unique(ip,port) 联合唯一索引
primary key(ip,port) 联合唯一主键
这两个用法是一样的,区别:前者可以继续添加一个主键,而后者不能继续添加主键
主键只能是单个字段,或者联合主键,如果加主键就会报错
'''


#foreign key  外键,把多张表通过一个关联字段,联合在一起
'''
外键的要求:要求关联的字段必须具有唯一属性(uniques或者primary key)
'''
student
    id   name       age  classname  address
    1    wangzhen   80   python30   北京市天安门阁楼里
    2    xiaolin    90   python30   东北老革命工业基地
    3    wangwen    18   python31   内蒙古呼和浩特蒙古包

#为了避免出现过多的字段,可以采用分表的形式,来提升效率,减少数据的冗余
    student1
        id  name      age  address               classid
        1   wangzhen  80   北京市天安门阁楼里    1
        2   xiaolin   90   东北老革命工业基地    1
        3    wangwen  18   内蒙古呼和浩特蒙古包  2
            
    class1:
        id  classname  datetime
        1   python30   2020-01-01 09:09:09
        2   python31   2020-02-01 09:09:09
    
    #创建class1表
    create table class1(id int,classname varchar(255));
    
    #被关联的字段至少需要具有唯一属性
    alter table class1 add unique(id);
    
    #创建student1学生表
    create table student1(
    id int primary key auto_increment.
    name varchar(255) not null,
    age int not null,
    classid int,
    foreign key(classid) references class1(id)
    );
    
    insert into class1 values(1,"python30");
    insert into class1 values(2,"python31");
    insert into student1 values(null,"liuwei",80,2);
    insert into student1 values(null,"xiaolin",90,1);
    insert into student1 values(null,"wangwen",18,2);
    
    #删除class1如果这条数据在其他表里存在,直接删会报错,因为外键的关联限制
    delete from class1 where id  =1;
    #先把关联的数据都删了之后,才可以真正删掉这条数据
    delete from student where id  =2;
    delete from class1 where id =1;
    
    
    
    #联级删除 联级更新(谨慎操作)
    '''
    联级删除 on delete cascade
    联级更新 on update cascade
    '''
    
    #创建class2
    create table class2(id int unique,classname varchar(255));
    #创建student2
    create table student2(
    id int primary key auto_increment,
    name varchar(255) not null,
    age int not null,
    classid int,
    foreign key(classid) references class2(id) on delete cascade on delete update cascade
    );
    
    insert into class2 values(1,"python30");
    insert into class2 values(2,"python31");
    insert into student2 values(null,"liuwei",80,2);
    insert into student2 values(null,"xiaolin",90,1);
    insert into student2 values(null,"wenwen",18,2);
    
    #联级删除
    delete from class2 where id =2;
    #联级更新
    update class2 set id=100 where classname = 'python30';


# ### part4 表与表之间的关系
(1) 一对一 表1:id z1 z2 z3 表2:id z4 z5 z6关联字段(表1中的id)
(2) 一对多 或者 多对一:一个班级里可以对应多个学生,把学生作为主动关联的表,设置一个外键,去存储班级表的关联字段中的数据
(3)多对多:一个学生可以对应多个学科,一个学科也可以被多个学生学习
            一本书可以对应多个作者,一个作者写多本书

xueke(表1)
id  name
1   math
2   english
3   wuli

student(表2)
id  name
1   wangwen
2   weiyilin
3   wangyingqian


#表达多对多关系时,需要第三张关系表
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


# ### part5 存储引擎:存储数据的结构方式
show engines; 查看存储引擎

#概念理解:
表级锁:如果有人修改了当前这个表,会直接上表锁,其他人无法修改,在编辑数据时候,速度慢,不能高并发(MyISAM)
行级锁:如果有人修改了当前这个表中的一条记录,当前这个数据记录会上锁,其他数据仍然可以正常修改,速度快,允许更高的并发(InnoDB)
支持事务处理:如果执行sql语句,在全部成功之后,在选择提交数据,有一条失败,立刻回滚,恢复成原来状态
begin:开始事务
commit:提交数据
rollback:回滚数据

InnoDB:5.6版本后的默认存储引擎,支持事务处理,行级锁,外键
MyISAM:5.6版本前默认存储引擎,支持表级锁
MEMORY: 把数据放在内存中,用做缓存
BLACKHOLE:黑洞,用来同步主从数据库中的数据,场景发生在服务器并发集群,用在主从数据库当中[主数据库:增删改,从数据库:查询]
D:\MySQL5.7\mysql-5.7.25-winx64\data\db0617
    create table myisam1(id int,name char(10)) engine = myisam;
    myisam1.frm 表结构
    myisam1.MYD 表数据
    myisam1.MYI 表索引
    
    create table innodb1(id int,name char(10)) engine = innodb;
    innodb1.frm 表结构
    innodb1.ibd 表数据+表索引
    
    create table memory1(id int,name char(10)) engine=memory;
    memory1.frm 表结构
    没有表数据文件,因为把数据存放在内存中了。
    
    create table blackhole1(id int,name char(10)) engine =blackhole;
    blackhole1.frm 表结构

 

posted @ 2020-06-17 21:37  我在路上回头看  阅读(235)  评论(0编辑  收藏  举报