zoukankan      html  css  js  c++  java
  • mysql的事物,外键,与常用引擎

    ### part1

    时间类型

    date YYYY-MM-DD 年月日 (出现日期)
    time HH:MM:SS 时分秒 (竞赛时间)
    year YYYY 年份值 (红酒年份 82年矿泉水)
    datetime YYYY-MM-DD HH:MM:SS 年月日时分秒 (登录时间,下单时间)

    create table t5(d date,t time,y year,dt datetime);
    insert into t5 values("2019-7-30","09:08:55","2019","2019-10-01 17:13:33");
    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(20190730091420,20190730091420);
    insert into t6 values(20190730091420,20390730091420); error 超过了时间戳的范围;

    mysql 内置函数

    now() 获取当前时间

    ### part2

    约束: 对插入数据库的值进行限制,不满足条件的不让操作

    unsigned 无符号整型
    not null 不能为空
    default 设置默认值
    unique 唯一约束,数据唯一不重复.
    primary key 主键,唯一不为空的值,用于表达数据的唯一性
    auto_increment 自增加1 [一般是对primary key 或者 unique 进行设置]
    foreign key 外键,把多张表通过一个字段关联在一起
    zerofill 零填充,int(10),位数不够的时候前面补0,前导零

    unsigned 无符号整型

    create table t66(id int unsigned);
    insert into t66 values(3);
    insert into t66 values(-3); error
    

    not null 不能为空

    create table t7(id int not null,name varchar(255));
    insert into t7 values(1,"333")
    insert into t7 values(null,"2") error 
    

    default 设置默认值

    create table t8(id int not null ,name varchar(255) default "李毅");
    insert into t8 values(1,null)
    insert into t8(id) values(2);
    

    unique 唯一约束(索引),数据唯一不重复. UNI

    """索引是为了加快查询速度的,相当于字典当中的目录"""
    create table t9(id int unique,name char(12) default '曾文');
    insert into t9(id) values(1);
    insert into t9(id) values(1); error 不能插入重复值
    insert into t9(id) values(null);
    insert into t9(id) values(null); success 可以连续插入空值

    primary key 主键,唯一不为空,用于表达数据的唯一性 PRI

    # 原型:
    create table t10(id int not null unique,name char(6) default '陈学斌');
    # 主键
    create table t11(id int primary key ,name char(6) default '陈学斌');
    insert into t11 values(1,"力争")
    insert into t11 values(null,"力争") error 不能为空
    

    auto_increment 自增加1 [一般是对primary key 或者 unique 进行设置]

    create table t12(id int primary key auto_increment, name varchar(11) default "王伟")
    insert into t12 values(null,"李杰"); # 通用写法
    insert into t12 values(5,"李杰2");
    insert into t12(name) values("重汽彩");
    

    删除:

    # (1)delete from 表 where 条件
    delete from t12 where id = 6
    delete from t12 
    # (2)truncate table 表名  所有数据全部删除,id号重置(速度更快)
    truncate table t12
    

    ### part3

    1.联合唯一约束 unique(字段1,字段2,...)

    (1) 联合唯一主键

    """单独的ip 或者端口都可以重复,但是联合在一起不能重复,针对于多个字段来讲的"""
    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(id,server_name,ip,port) values(4,"aa","192.168.75.128",3306);
    insert into t1_server(id,server_name,ip,port) values(4,"aa","192.168.75.128",3307);
    insert into t1_server(id,server_name,ip,port) values(4,"aa","192.168.75.128",3306); error
    

    (2) 联合唯一索引 MUL 代表普通索引

    '''如果要创建联合字段约束,最好设置非空,否则的话连续插入null 是默认允许的;'''
    create table t2_server(id int,server_name char(10) not null ,ip char(15),port int ,unique(ip,port))
    insert into t2_server(id,server_name,ip,port) values(4,"aa","192.168.75.128",3306);
    insert into t2_server(id,server_name,ip,port) values(4,"aa","192.168.75.129",3306);
    insert into t2_server(id,server_name,ip,port) values(4,"aa",null,null);
    insert into t2_server(id,server_name,ip,port) values(4,"aa",null,null);
    

    (3) 如果两种类型(主键,联合主键)如果都在同一个表里,如何显示? 优先显示主键为PRI ip显示MUL 普通索引

    alter table t1_server add primary key(id);
    # (了解) primary key(ip,port) 与 unique 写法用法相似,区别在于不能再继续添加主键了.
    

    zerofill 零填充,int(10),位数不够的时候前面补0,前导零

    create table ceshi01(id int(5) zerofill);
    insert into ceshi01 values(1)
    

    foreign key 外键,把多张表通过一个字段关联在一起

    """外键要求: 主动关联的表字段用foreign key , 被关联的字段必须唯一 (unique 或 primary key,一般设置为主键)"""
    student1:
    	id name    age  classname
    	1  wangwen 18   python6期
    	2  liyi    38   python6期
    	3  zengwen 99   python6期
    	4  chenyu  80   python6期
    	5  xuebin  87   python7期
    	
    # 为了避免过多的出现冗余数据,开始进行分表操作,利用外键关联不同的表
    student1:
    	id name    age  classid
    	1  wangwen 18    1
    	2  liyi    38    1
    	3  zengwen 99    1
    	4  chenyu  80    1
    	5  xuebin  87    2
    	
    class1:
    	id classname
    	1  python6期
    	2  python7期
    	
    	
    # 创建class1表
    create table class1(id int,classname varchar(255));
    # 创建学生表 foreign key(字段) references class1(字段)
    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) );								
    
    # 设置id为unique
    alter table class1 add unique(id);
    	
    # 插入数据
    insert into class1 values(1,"python6期");
    insert into class1 values(2,"python7期");
    insert into student1 values(null,"liyi",90,1);
    insert into student1 values(null,"xuebin",90,2);	
    insert into student1 values(null,"wangwen",18,2);
    
    # 删除class1中的数据
    delete from class1 where id = 1   # 删不掉的,因为被动关联其他表	
    delete from student1 where id = 1 # 先删除学生表中所有跟这个班级关联的数据,然后在删除class1表中的对应班级
    
    
    # 外键的联级操作
    """
    on update cascade  联级更新
    on delete cascade  联级删除	
    """
    create table class2(id int unique,classname varchar(255));
    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 update cascade on delete cascade );								
    
    
    insert into class2 values(1,"python6");
    insert into class2 values(2,"python7");
    	
    insert into student2 values(null,'zengwen',100,1);
    insert into student2 values(null,'lizeng',101,2);
    insert into student2 values(null,'xuebin',103,2);	
    	
    # 删除class2表 对应的学生表中与他相关的所有数据都会被删除
    delete from class2 where id  = 2;
    # 更新class2表的 id号 , student2中的classid 也会被更新. 操作的是被关联的那张表. 1对多的那个1
    update class2 set  id = 10 where classname = "python6";
    
    
    # 表与表之间的关系:
    (1) 一对一: 一个人对应一个身份证号 在外键上要添加2个约束 unique + foreign key 
    (2) 一对多或者多对一: 一个班级对应多个学生,在多个学生的那个表里设置外键,被关联的另外一张表设置unique 或者 primary key 表达唯一.
    (3) 多对多:一个学生对应多个学科,一个学科可以被多个学生学生,一本书可以对应多个作者,一个作者可以出版多本书.
    	把xid 和 sid 设置成外键,关联xueke 的id 和 student 的id 这两个id设置成主键
    
    xueke (表1)
    id xueke_name
    1  math
    2  huaxue
    3  english
    4  wuli
    
    student (表2)
    id name
    1  王文
    2  李杰
    3  王伟
    
    
    relation (关系表3)
    xid  sid
    1     1
    1     2
    1     3
    2     1
    2     2
    2     3
    

    ### part4

    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 外键

    # student1 删除它的外键 第一步先用show create table student1 看一下外键名字
    alter table student1 drop foreign key  student1_ibfk_1 				# 删除
    alter table student1 add foreign key(classid) references class1(id) # 添加
    

    ### part5

    事务: 在操作一些列sql语句的时,只有都执行成功才算最终成功,但凡有一个失败,就回滚,恢复到最初的数据状态;

    begin 开始事务处理
    commit 提交数据
    rollback 回滚

    存储引擎:

    """
    show enginesG
    """

    概念理解:

    行级锁: 有一个人再修改这张表中的一条记录,这条记录就会上锁,其他人在上锁期间改不了,保证数据的安全性.(允许更大的并发和更快的速度)
    表级锁: 有一个人在修改这张表,就会上锁,其他人修改不了
    外键foreign key : 把多张表通过一个字段关联在一起

    存储引擎的种类

    InnoDB : 5.6版本之后 默认的存储引擎
    特点: 支持事务, 行级锁, 外键 , 内存开销大

    MyISAM : 5.6版本之前 默认的存储引擎
    特点: 表级锁,不支持并发.内存开销小

    MEMORY : 把数据存储在内存当中,也可以把这种存储形式叫做缓存.
    特点: 速度快,但是不能进行持久化存储.

    BLACKHOLE : 黑洞 ,用作同步数据的存储引擎方法(数据库的主从复制)
    特点: 所有数据都不会真正写入,但是都会提示成功.

    create table innodb2(id int ,name char(2)) engine = innodb;
    innodb2.frm  表结构
    innodb2.ibd  表数据
    
    create table myisam(id int , name char(3)) engine = myisam;
    myisam.frm 表结构
    myisam.MYD 表数据
    myisam.MYI 表索引
    
    create table memory1(id int , name char(3)) engine = memory;
    memory.frm 表结构,只是单纯的存储结构,数据都放在内存中
    
    create table blackhole1(id int , name char(3)) engine = blackhole;
    blackhole1.frm  表结构 负责生产binlog日志,舍弃数据.
  • 相关阅读:
    Azure 认知服务 (3) 计算机视觉API
    Azure 认知服务 (2) 计算机视觉API
    Azure 认知服务 (1) 概述
    Azure PowerShell (13) 批量设置Azure ARM Network Security Group (NSG)
    Azure SQL Database (22) Azure SQL Database支持中文值
    HighCharts设置图表背景透明
    跨域资源共享(CORS)--跨域ajax
    "Ext 4.1 Grid 'el.dom' 为空或不是对象"问题的解决
    Ant编译utf-8非法字符:/65279 解决方法
    lvs 隧道模式请求没有回应的解决
  • 原文地址:https://www.cnblogs.com/caiwenjun/p/11945292.html
Copyright © 2011-2022 走看看