一. 为啥使用数据库?
 
 因为之前使用文件(Excel)管理数据, 但是数据量特别大的时候,使用Excel管理 的话, 就比较的麻烦了
 因此引入一个新的数据管理软件 : 数据库软件
二. 数据库的分类?
 
 关系型数据库
  
  1. 有约束
  2. 基于硬盘的存储 (就是将数据存储到硬盘上, 持久化 === 落地)
  
  典型代表:
   MySQL oracle(国企) sqlserver(微软)  sqllite  db2
  
 非关系型数据
 
  1. 没有约束 (key--->value)
  2. 基于内存存储 (将数据放入到内存中)
  
  典型代表:
   MemCache, redis(微博), mongodb
   
三. mySQL的架构::  
  
  客户端:
   socket客户端, 连接服务端, 发送指令 (SQL语句)
  服务端:
   socket服务端, 接收客户端的指令, 并将结果返回给客户端
  
  
四. MySQL 的安装
  版本: 5.5 以上  5.7 以下
  
  1. 可执行文件:
   .exe 或者 .msi文件
   点击下一步
   
  2. 压缩包
  
   解压, 进入目录 bin
   
   mysqld : 启动服务
   
   mysql  : 连接服务端
   
  3. 环境变量的配置
  
 ps:
  工作中, 线上的 MySQL 不需要开发自己装, 运维或者DBA装
  练习中, 需要自己手动安装MySQL
  
  
五. 初始化:
  mysqld --initialise-secure
  
 
 数据库 (文件夹):
  表 (文件)
     数据行 (文件中的一行内容)
六. 数据库的操作: (********************************)
  
  1. 数据库:
  
    增加:
     SQL语句: 
      create database 数据库名称;
     例子:
      create database db1;
     
    删
     drop database 数据库名称;
     drop database db1;
     
    修改
  
     没有专门的修改指令 update
     
     删了重新建
     
    查询
     show databases;
    
    使用:
     use 数据库名;
     use db1;
  
  2. 数据表:
  
    新建:
     use db1;
     
     版本0:
      SQL语句:
       create table 表名 (
        列名1 列类型
       );
      
      例子:
       create table t1 (
        id  int,
        name char(32)
       );
      
      增加
       指令:
        insert into  表名 (列1, 列2) values (值1, 值2);
       
       例子:
        insert into  t1 (id, name) values (1, 'zekai');
        insert into  t1 (id, name) values (2, '你好');
     
     改进1:
      create table 表名 (
       列名1 列类型
      )engine=Innodb charset=utf8;
     
     ps:
      引擎: Innodb 和 MyIsam
      5.5 版本以上  默认是Innodb
      
      create table t2 (
       id  int,
       name char(32)
      )engine=Innodb charset=utf8;
      
      insert into  t2 (id, name) values (1, '你好');
      insert into  t2 (id, name) values (1, 'xxx');
     
     改进2:
     
      create table 表名 (
       列名1 列类型 auto_increment primary key
      )engine=Innodb charset=utf8;
      
      create table t4 (
       id  int auto_increment primary key,
       name char(32)  not null  default ''
      )engine=Innodb charset=utf8;
      
      auto_increment : 自增
      primary key : 主键索引 (作用: 加快查找的速度)
      not null : 不能为空
      default : 默认值
      
      注意: 后面一列写完之后, 不能加逗号  (*********)
      
      一种:
       insert into  t3 (id, name) values (1, '你好');
       insert into  t3 (id, name) values (2, 'xxx');
      二种:
       insert into  t3 (name) values ('hello');
       insert into  t3 (name) values ('xxx');
      
     
     -------------------------------------------------------------
     
     最终的格式:
      create table 表名 (
       列1 列属性 [是否为null 默认值],
       列2 列属性 [是否为null 默认值],
       .....
       列n 列属性 [是否为null 默认值]
      )engine = 存储引擎  charset = 字符集
     最终的例子:
      create table t4 (
       id  int auto_increment primary key,
       name char(32)  not null  default '',
       pwd  char(32)  not null  default ''
      )engine=Innodb charset=utf8;
     
     查看:
      指令:
       select 列名 from 表名;
      例子:
       select * from t1;
    
     
     列类型:
      
      a. 数值型
       create table t4 (
        id  unsigned mediumint auto_increment primary key,
        name char(32)  not null  default '',
        pwd  char(32)  not null  default ''
       )engine=Innodb charset=utf8;
       
       tinyint :
        范围: 
         有符号: -128到127
         无符号: 0 到 255  unsigned
       smallint
        范围: 
         有符号: -32768到32767
         无符号: 0 到 65535  unsigned
       
       mediumint
        范围: 
         有符号: -8388608到8388607
         无符号: 0 到 16777215  unsigned
       int
       bigint
       
       区别:
        a. 取值范围不一样, 根据自己公司的业务来去选择
        b. 无符号和有符号的意思
       
       float(M,D) 浮点型
       decimal(M,D) 定点型 比float更加的精准
        
        例如: 3.1415151519868789789
        float: 3.141515000000000000
        decimal : 3.1415151519868789789
        
        126.35
        
        M:小数总共多少位 decimal(5, )
        D:小数点后面几位 decimal(5, 2)
        
        使用场景:
         比如 说存 salary 工资 : 6000.23 decimal(,2)
         
      b. 字符串类型
       
       char : 定长 char(32)  这一列的值就是32  优点: 速度快  缺点: 浪费
       varchar : 变长 varchar(32)              优点: 不浪费, 节省空间  缺点: 速度慢
       
       根据自己公司的业务来去选择:
        
        create table userinfo (
         id  unsigned mediumint auto_increment primary key,
         name varchar(128)  not null  default '',
         pwd  char(32)  not null  default '',
         create_time  datetime not null default  '1970-01-01 00:00:00'
        )engine=Innodb charset=utf8;
        
       一般情况下, 如果没有100%的把握, 都是用varchar()
       
       
       text: 文本  范围比较大, 如果存储大量字符的话, 可以使用这个字段
           
      c. 时间类型
        date 2019-6-12
        
        推荐使用datetime
    
    
    删
     指令:
      drop table 表名;
      
      连带着将数据表中的所有数据都会删掉
      
      ps: 工作中, 线上数据库, 这个命令根本不会让你用到
      
     实例:
      drop table t1;  
    
    查询
     show tables;
     
     desc 表名;   :  查看表的结构
     
     show create table 表名 :  查看表的创建过程
      
    关于主键自增: (不是重点)
     show session variables like 'auto_inc%';
     set session auto_increment_increment = 2;
     
     show global  variables like 'auto_inc%';
     set global auto_increment_increment = 2;
     
    
    修改
     create table t4 (
      id  int auto_increment primary key,
      name char(32)  not null  default '',
      pwd  char(32)  not null  default ''
     )engine=Innodb charset=utf8;
     
     修改字段:
      alter table 表名(t3) change 原列名(name) 新列名(username varchar(32) not null default '');
     
     新增字段:
      alter table 表名(t3)  add  新列(pwd char(32)  not null  default '');
      
     删除字段:
      alter  table 表名(t3) drop 列名(pwd);
  
  3. 数据行:
    
    增
     insert into  t3 (id, name) values (1, '你好');
    
    查询
     
     select * from t3; : 将表中的 所有的列全部列出
     select 列名, 列名, 列名 from t3 : 将某一列的值查出
    
    删
     
     delete from 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, 继续会延续上一个 ID
     
     truncate 表名(t3);    将表中的所有的 数据删除掉, 再次添加的时候, ID 会重新开始
     
     truncate 速度快
     
     ps: 工作中, 线上数据库, 这个命令根本不会让你用到
     
     delete from 表名(t3) where name = 'xxxxx';
     
     
    修改
    
     update t3 set username='zekai';
     
     update t3 set username='xxxx'  where  id=3;
  
     update t3 set username='xxxx', pwd='xxxxx'  where  id=3;
  
  
  
七. 外键: (*******************************************************************)
  
  缺点:
   1. 数据重复
   2. 如果 部门过长的话, 太占用空间
   
  
  解决方法:
   
   重新设计一张表, 这张表 中存放部门的相关信息
  
  
  部门表:
  
   create table department (
    id  int auto_increment primary key,
    depart_name varchar(32)  not null  default ''
   )engine=Innodb charset=utf8;
   
   insert into department (depart_name) values ('公关'), ('关关'),('关公');
   
   create table userinfo ( 
    id  int auto_increment primary key,
    name varchar(32) not null default '',
    depart_id int not null  default 1,
    
    # constraint 外键名(fk_userinfo_depart) foreign key (列名(depart_id)) references 表名(department)(关联的列名(id)),
    constraint fk_userinfo_depart foreign key (depart_id) references department(id)
   
   )engine=Innodb charset=utf8;
   
   
   insert into userinfo (name, depart_id) values ('root1', 1);
   insert into userinfo (name, depart_id) values ('root2', 2);  错误的
   
   注意:
    创建多个外键的时候, 名称不能一样
   
   =====> 一对多
posted on 2019-06-13 08:54  郝俊连城  阅读(131)  评论(0编辑  收藏  举报