zoukankan      html  css  js  c++  java
  • 数据库

    内容回顾2

    day37

    并发回顾

    # 1.进程 线程 协程之间的相同点和不同点
        # 进程 : 内存隔离 操作系统级别 开销大 可以利用多核 计算机中资源分配的最小单位
        # 线程 : 内存共享 操作系统级别 开销中 Cpython解释器下不能利用多核 计算机中CPU调度的最小单位
        # 协程 : 内存共享 用户级别     开销小 不能利用多核
    
        # 协程 : 生产者消费者模型  进程模块提供的队列joinablequeue
            # from queue imort Queue
        # 相同点 : 都能够帮助我们实现并发操作,规避IO时间,提高程序执行效率
    
    # 2.进程内存之间是否共享,如何实现通信
        # 不共享
        # 基于文件
            # 队列 管道 manager
        # 基于网络
            # 第三方工具(redis kafka memcache rabbitMQ) socket
    
    # 3.在python中是否线程安全
        # 不安全
        # python你写的python代码未来都会转换成机器码,机器码执行的过程中
        # 如果有一个非原子性操作,那么就会导致线程数据不安全
        # 需要手动加锁来解决问题
    
    # 4.协程的本质是什么
        # 多个任务在一条线程上能够实现切换
    
    # 5.线程池开启任务
        # 如何开启线程池,如何提交任务,获取返回值
    # from concurrent.futures import ThreadPoolExecutor
    # def func(arg):
    #     return arg * 20
    #
    # tp = ThreadPoolExecutor(5)
    # ret_l = []
    # for i in range(100):
    #     ret = tp.submit(func,i)
    #     ret_l.append(ret)
    # for r in ret_l:
    #     print(r.result())
    #
    # # 二
    # from concurrent.futures import ThreadPoolExecutor
    # def func(arg):
    #     return arg * 20
    #
    # tp = ThreadPoolExecutor(5)
    # ret_l = tp.map(func,range(100))
    # for r in ret_l:
    #     print(r)
    
    # 协程复习 :
    # 数据是否安全  :  绝对安全
    # a = 1
    # def func1():
    #     global a
    #     a += 1
    #
    # def func2():
    #     global a
    #     a += 1
    
    # 和线程的关系
        # 本质就是一条线程
    
    
    # gevent模块
        # g1 = spawn(协程函数,参数)
        # g1.join()
        # gevent.joinall([g1])
        # monkey.patch_all()
    
    
    # greenlet switch
    # 并没有减少io操作
    
    
    # import time
    # from gevent import monkey
    # monkey.patch_all()
    #
    # print(time.sleep)
    # <built-in function sleep>
    # <function sleep at 0x0000023FEBF62378>
    

    数据库

    # day1.介绍数据库安装基础的命令
    # day2.数据库的表操作
    # day3.数据库的数据操作,查询(单表多表)
    # day4.查询和其他内容的拾遗
    # day5.索引原理和python操作mysql
    
    # 今天的内容
        # 介绍数据库安装基础的命令
    
    # 数据库在开发的过程中占据着什么样的位置?
    # 在整个项目中又有什么意义?
    
    # 我们把数据存储在文件里
        # 写 write
        # 读 read
        # 改 读->写->删->改
    
    # 数据库 : 能够更加简单的 使用 存储在文件中的数据
    #         能够更好的解决并发问题
    #         数据的统一问题
    
    # 数据
    # 1,alex,alex3714  # 一行内容就是一条数据
    # 2,python,19800,6 months   # 一条数据
    
    # 数据库 DataBase DB
    # 存储数据的地方,我们把所有的数据都存储在一个固定的地方,那么这个地方就是数据库
    
    # 数据库管理系统 DBMS
    # 负责管理数据仓库中存储的所有文件中的内容
    # 能够更好(简单 高效 安全)的帮助我们完成数据的增删改查
    
    # 数据库服务器
        # 什么是服务器 : 本质就是一台计算机
                       # 当一台计算机上安装了某个软件能够对外提供服务的时候,那么这台机器就成为服务器
        # 数据库服务器
            # 当这台机器上安装的服务是一个数据库的server端的时候,我们就得到了一台数据库服务器
    
    # 数据库管理员 DBA
        # 专门帮助我们管理数据库 并且优化数据库的工作人员
    
    
    # mysql就是一个DBMS(能够管理硬盘上数据文件的一个软件)
        # 通过一些固定的简单的指令 帮助我们完成从文件中查找对应数据的软件
        # oracle 也是一个DBMS
    
    # 查 name,id 从 userinfo 条件 age = 83
    # 查 number 从 userinfo 条件 name='wusir'
    
    # 数据库管理系统的作用:
        # 关系型数据库
        # 非关系型数据库
    
    # mysql 开源的软件 - 小公司 各种互联网公司(二次开发之后的mysql)
    # oracle 付费的 - 金融行业 国企事业单位
    # sql server 在学校里教学使用的
    

    day38 基础操作

    回顾

    # 补充的知识点
    # server端肯定是确定下来的
    # mysql的客户端
        # mysql.exe 直接在命令行就可以运行的 (学习阶段用)
        # navicat等可视化的客户端,是第三方开发的客户端  (开发辅助)
        # python代码的客户端  (主要用)
    
    # 内容回顾
        # 数据库管理系统 DBMS
            # 数据库 DB
            # 表   table
            # 数据 data
        # 数据库管理员 DBA
    
    # 数据库的类型:
        # 关系型数据库 : mysqloraclesql server
        # 非关系型数据库 : redis
    
    # SQL语句
        # DDL : 定义 表库
            # 创建库
            # 定义表
            # 修改表结构
            # 删除表库
        # DML : 数据的增删改查    - 重中之重
            # insert 增     次多
            # delete 删除   绝对少
            # update 修改   相对少
            # select 查     最多
        # DCL : 和用户权限相关的
            # select user(); 查看当前的用户
            # 创建用户
            # create user '用户名'@'ip地址' identified by '密码'
            # 给用户授权(包括了创建用户)
            # grant 权限 on 库.表名 to '用户名'@'ip地址' identified by '密码'
    

    day39 多表查询

    # 1.多积累使用工具的经验
    # 2.尽量多练习
        # 1.多练几种类型
        # 2.不要照着写好的sql敲,要自己组织语言
    
    # 内容回顾
    # 存储引擎
        # innodb : 外键  行级锁(并发修改)  事务(客户管理系统)
        # myisam : 表级锁 不支持外键事务行级锁
        # memory : 只能在内存中存储数据 重启server数据丢失
    # mysql中的基础数据类型
        # 数字
            # int         id/age/部门编号
            # float(8,2)  salary
        # 字符串
            # char    定长  越是长度固定char越节省空间    读写速度快
                        # 名字 部门名
            # varchar 变长  越是长度不固定varchar越节省空间  读写速度慢
                        # 部门描述
        # 时间
            # year
            # date       入职日期  离职 开学 毕业
            # time
            # datetime   出生日期 交易记录 打卡时间
            # timestamp
        # enum 和 set
            # enum 单选
                # enum('male','female')
            # set 多选(去重)
    # 完整性约束
        # id int unsigned
        # id int default 0
        # id int not null
        # id int unique
        # auto_increment 相当于非空+自增且只能用于整数类型
            # id int unique auto_increment
            # id int primary key auto_increment
        # 非空 + 唯一
            # id int unique not null 如果没有主键,第一个设置非空唯一的就是主键
        # 联合唯一
            # id int,
            # name char(12),
            # unique(id,name)
        # primary key  主键 一张表只能有一个主键
            # id int primary key
        # foreign key  外键
            # id int,
            # name char(12),
            # tid int,
            # foreign key(tid) references 外表(字段名) on update cascade on delete cascade
    # 表的操作
        # 创建表
            # create table 表名(
            #   字段名 类型(长度约束)  其他约束,
            #   字段名 类型(长度约束)  其他约束,
            #   字段名 类型(长度约束)  其他约束);
        # 删除表
            # drop table 表名
        # 修改表
            # alter table 表名 rename 新表名;
            #                  add    新字段  类型(长度约束)  其他约束 first;
            #                  drop   字段 ;
            #                  modify 原字段名  新类型(新长度) 新约束 after 某字段;
            #                  change 原字段名  新字段名 新类型(新长度) 新约束;
        # 查看表结构
            # desc 表名 == describe 表名
            # show create table 表名; 查看详细表结构,存储引擎 编码 更复杂的约束条件
    
    # 默写
        # 员工id  姓名 性别 年龄 入职日期 部门 部门描述 部门编号 薪资
        # create table staff(
        #     id int primary key auto_increment,
        #     name char(12) not null
        #     sex enum('male','female') default 'male',
        #     age int,
        #     hire_date date,
        #     post_name char(12),
        #     post_comment varchar(255),
        #     post_id int,
        #     salary float(8,2) unique
        # )
    
        # 拆分表
    # create table post(
    #     post_id int primary key auto_increment,
    #     post_name char(12),
    #     post_comment varchar(255)
    # )
    # create table staff(
    #     id int primary key auto_increment,
    #     name char(12) not null
    #     sex enum('male','female') default 'male',
    #     age int,
    #     hire_date date,
    #     salary float(8,2) unique
    #     post_id int,
    #     foreign key(post_id) references post(post_id)
    # )
    

    day 40

    # 数据的增删改查
    # 插入数据
        # insert into 表 values (值1,值2...)
        # insert into 表(指定字段名1,字段名2) values (值1,值2...)
    # 删除数据
        # delete from 表名 where 条件
    # 更新数据
        # update 表名 set 字段=新的值 where 条件
    # 查询数据
        # select * from 表
            # where 条件
            # group by 分组  having 过滤条件(可以使用聚合函数)
            # order by 排序(默认是升序asc,降序desc)
            # limit m,n (默认m=0,从m+1开始,取n个)
                # limit n offset m(与上面的写法完全一致)
    

    day41

    # 多表查询
    # 联表查
        # 内连接 左右两表中能连上的行才被保留
            # 表1 inner join 表2 on 表1.字段1=表2.字段2
        # 外连接
            # 左外连接 表1中所有的项都会被保留,而表2中只有匹配上表1的项才会被保留
                # 表1 left join 表2 on 表1.字段1=表2.字段2
            # 右外连接 刚好和左外连接相反right join
    # 子查询
        # select 字段 from 表 where 字段 = (select 子句)
        # select 字段 from 表 where 字段 > (select 子句)
        # select 字段 from 表 where 字段 < (select 子句)
        # select 字段 from 表 where 字段 in (select子句查出的结果可以是多个)
    
    # 修改的问题 :
    # 对于条件非常多或者非常复杂的修改操作,都应该先根据条件查,再修改
    
    # 1.'三年二班'的所有同学姓名
    # select cid from class where caption = '三年二班';
    # select sname from student where class_id = (select cid from class where caption = '三年二班');
    
    # 2.男生人数和女生人数
    # select gender,count(*) from student group by gender;
    
    # 3.生物最高分
    # select cid from course where cname = '生物'
    # select max(number) from score where couse_id = (select cid from course where cname = '生物');
    
    # 4.体育课平均分
    # select cid from course where cname = '体育'
    # select avg(number) from score where couse_id = (select cid from course where cname = '体育');
    
    
    # 5.既选体育 又选物理的人名
    # 找到体育的course_id
    # select cid from course where cname = '体育'
    # 找到物理的course_id
    # select cid from course where cname = '物理'
    # 找到选择体育的人,也在找物理的人名中
    # select student_id from score where course_id = (select cid from course where cname = '体育');
    # select student_id from score where course_id = (select cid from course where cname = '物理');
    #
    # select student_id from (
    # select student_id from score where course_id = (select cid from course where cname = '体育')) as t1
    # inner join
    # (select student_id from score where course_id = (select cid from course where cname = '物理'))  as t2
    # on t1.student_id = t2.student_id)
    
    # select * from studnet where sid in  (
    # select student_id from (
    # select student_id from score where course_id = (select cid from course where cname = '体育')) as t1
    # inner join
    # (select student_id from score where course_id = (select cid from course where cname = '物理'))  as t2
    # on t1.student_id = t2.student_id)
    # );
    
    
    # SELECT student.sname,GROUP_CONCAT(course.cname) FROM
    #                   course INNER JOIN score INNER JOIN student
    #                   ON course.cid=score.course_id AND student.sid=score.student_id
    #                  WHERE course.cname IN ('体育', '物理')
    #                   GROUP BY student.sname HAVING COUNT(course.cname)=2;
    
    # select s1.sname from student as s1
    # inner join
    # (select * from score as s inner join course as c on s.course_id = c.cid where c.cname  in ('物理','体育')) as s2
    # on s1.sid = s2.student_id
    # group by s1.sname having count(s1.sid) = 2 ;
    
    
    # 索引原理
    # b+树
    # 1.b 是balance 表示的是这个树最终是能够达到平衡的
    # 2.数据不是平铺直叙的存储在硬盘山
    # 3.影响查询速度的最重要的因素是树的高度
    # 4.我们要做的事情,或者我们想要加速查询 降低树的高度
        # 1.让索引的字段尽量的短
        # 2.让索引的字段区分度高
    # 5.b+树和普通的b树比起来有什么区别
        # 1.b+树只在叶子节点存数据
            # 有利于降低树的高度
            # 稳定查询所有数据的io次数
        # 2.在所有的叶子节点之间添加了双向链表
            # 导致了所有的范围查询b+树的效率比b树高
    # 6.mysql中 innodb  myisam的索引都是由b+树完成的
        # innodb 支持 聚集索引(叶子节点存具体的数据) + 辅助索引(叶子节点存地址)
            # 聚集索引也叫 聚簇索引
        # myisam 只支持辅助索引,也叫非聚集索引
    
  • 相关阅读:
    js人工智能对话框
    html 实现相册
    thinkphp5 三种重定向(跳转)
    thinkphp5 分页实现
    常用的Mysql数据库操作语句大全
    FormData之file图片上传
    FormData对象
    input file 上传图片时限制格式
    form 中Enctype=multipart/form-data 的作用
    thinkphp5 不刷新退出
  • 原文地址:https://www.cnblogs.com/Doner/p/10855927.html
Copyright © 2011-2022 走看看