MySql

数据库

数据库是按照数据结构来组织、存储和管理数据的仓库。

关系型数据库

RDBMS:建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。

特点:

  • 数据以表格形式出现
  • 每行为各种记录名称
  • 每列为记录名称所对应的数据域
  • 许多的行和列组成一张表单
  • 若干的表单组成数据库

术语:

  • 数据库:一些关联表的集合
  • 数据表:是数据矩阵
  • 列:包含了相同类型的数据
  • 行:一组相关的数据
  • 冗余:存储两倍数据,冗余降低了性能,但提高了数据的安全性。
  • 主键:唯一的
  • 外键:用于关联两张表
  • 复合键:将多个列作为一个索引键,一般用于复合索引
  • 参照完整性:要求关系中不允许用不存在的实体。于实体完整性是关系模型必须满足的完整性约束条件,目的是为了保证数据的一致性
    非关系型数据库

安装

  • Linux环境
    1. 安装前检测系统是否是自带安装MySQL
      1
      rpm -qa | grep mysql
    2. 如果系统有安装,可以选择卸载
      1
      2
      rpm -e mysql //普通删除
      rpm -e --nodeps mysql //强力删除
    3. 去官网找到yum下载资源地址下载
      1
      2
      3
      4
      5
      6
      wget 下载地址

      rpm -ivh mysql-community-release-e17-5.noarch.rpm

      yum update
      yum install mysql-sever
  • Windows环境

事务

  • 只有innodb数据库引擎支持数据库或表的事务操作。
  • 事务用来维持数据的完整性,要么全部成功,要么全部失败。
  • 事务用来管理增删改操作。

事务的特性

  • 原子性:一个事务中的操作,要么全部成功,要么全部失败。
  • 一致性:事务开始前和事务结束后,数据库的完整性没有被破坏。
  • 隔离性:数据库运行多个并发事务同时对其数据进行增删改查操作。可以防止事务之间保持隔离。
  • 持久性:事务结束后,对数据的修改是永久的。

事务的隔离级别

  • 读未提交(Read uncommitted):
  • 读提交(read committed):
  • 可重复读(repeatable read):
  • 序列化(Serializable):

索引

什么是索引?

实际上索引也是一张表,该表保存了主键于索引字段,并只想实体表的记录。

为什么要用索引?

提高MySQL检索的速度。
注意:虽然索引提高了查询速度,但同时也会降低更新表的速度,做增删改操作时不仅需要保存数,还需要保存索引文件。

索引怎么用?

索引的分类

  • 单列索引
  • 组合索引
  • 普通索引
  • 唯一索引

操作命令

MySQL服务操作

  • Windows

    • 开启MySQL服务
      net start mysql
    • 停止MySQL服务
      net start mysql``net start mysql
  • Linux

    • 开启MySQL服务
      1
      2
      3
      4
      //方式一
      service mysqld start
      //方式二
      systemctl start mysqld
    • 重启MySQL服务
      1
      2
      3
      4
      //方式一
      service mysqld restart
      //方式二
      systemctl restart mysqld
    • 停止MySQL服务
      1
      2
      3
      4
      //方式一
      service mysqld stop
      //方式二
      systemctl stop mysqld
    • 查看MySQL运行状态
      1
      2
      3
      4
      方式一
      service mysqld status
      方式二
      systemctl status mysqld
  • 验证MySQL安装

    1
    mysqladmin --version
  • 设置root用户的密码

    1
    mysqladmin -u root password "123456"
  • 连接MySQL服务器

    1
    mysql -uroot -p
  • 退出mysql

    1
    exit/quit

数据库操作

    • 创建数据库
      1
      create database 数据库名;
    • 删除数据库
      1
      drop database 数据库名;
    • 查询所有数据库
      1
      show databases;
  • 选择数据库
    1
    use 数据库名;

表操作

    • 创建表
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      #语法:
      create table if not exists 表名(字段,字段类型 [属性]);

      # 例:创建一张用户表,存放用户信息:用户名(非空)、密码(非空),年龄(最大不超过3位)及主键(无符号整数自增),使用innodb引擎,默认字符集为utf-8
      create table if not exists `user`(
      `id` int unsigned auto_increment,
      `username` varchar(10) not null,
      `password` varchar(6) not null,
      `age` int(3),
      primary key (`id`)
      )engine=innodb default charset=utf8;

      # 解析:
      if not exists:判断如果表是否已存在,如果不存在就创建表。
      unsigned:无符号的。
      not null:字段不能为空。
      auto_increment:设置主键自增。
      primary key:设置主键。
      engine:设置存储引擎。
      charset:设计编码。
    • 增加表字段
      1
      alter table 表名 add 字段 类型;
    • 删除表
      1
      drop table 表名;
    • 删除字段的默认值
      1
      alter table 表名 alter 字段 drop default;
    • 删除字段
      1
      alter table 表名 drop 字段;
    • 修改字段类型及名称
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      #方式一:modify
      alter table 表名 modify 字段 新类型;

      #列:在user表中,把age的类型int(3)改为int(4)
      alter table user modify age int(4);

      #方式二:change
      alter table 表名 change 原字段名 新字段名 类型;

      #列:在user表中,把age的类型int(4)改为int(3),age改为uage
      alter table user change age uage int(3);
    • 修改字段默认值
      1
      alter table 表名 alter 字段 set default 值;
    • 修改表名
      1
      alter table 原表名 rename to 新表名;
    • 查询所有的表
      1
      show tables;
    • 显示表的详细信息
      1
      show columns from 表名;
    • 显示数据表详细索引信息
      1
      show index from 表名;

数据操作

    • 插入数据
      1
      2
      3
      4
      5
      #插入数据方式一:
      insert into 表名(字段1,字段2…字段n) values(值1,值2,…值n),(值1,值2,…值n),(值1,值2,…值n)…;

      #插入数据方式二:
      insert into 表名 values(值1,值2,…值n),(值1,值2,…值n),(值1,值2,…值n)…;
    • 删除数据
      1
      2
      3
      4
      delete from 表名 [where 条件];

      #列:在user表中删除姓k且年龄等于100的用户
      delete from user where username like "k%" and age=100;
    • 修改字段的值
      1
      2
      3
      4
      5
      6
      7
      update 表名 set 字段1=1,……,字段n=值n [where 条件]

      #列:修改user表中所有用户的密码为123456
      update user set password="123456";

      #列:修改user表中姓张或年龄<50的用户的密码为123abc,年龄为50
      update user set password="123abc",age="50" where username like "张%" or age<50;
    • 查询表中所有字段数据
      1
      2
      3
      4
      select 字段1…字段n from 表名;

      #使用通配符
      select * from 表名;
    • 使用where语句设定条件查询
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      select 字段1…字段n from 表名 where 条件

      #列:在user表中查询所有年龄大于50岁的用户
      select * from user where age>50;

      #逻辑符:and
      #列:在user表中查询所有用户名为张1且年龄大于50岁的用户名和age
      select username,age from user where username="张1" and age>50;

      #逻辑符:or
      #列:在user表中查询所有用户名为张1或年龄大于50岁的用户名和age
      select username,age from user where username="张1" or age<50;
    • 使用like模糊查询
      1
      2
      3
      4
      5
      6
      7
      select * from 表名 where 字段名 like 条件;

      #列:在user表中查询所有姓张且年龄小于50的用户
      select * from user where username like "张%" and age<50;

      #列:在user表中查询所有姓张且姓名有2个字,年龄小于5的用户
      select * from user where username like "张_" and age<5;
    • 使用order by排序查询
      1
      2
      3
      4
      5
      6
      7
      select 字段1,字段2…字段n from1,表2…表n order by 字段1 [asc/desc],[字段2…][asc/desc]

      #列:在user表中,按年龄从小到大排序查询
      select * from user order by age asc;

      #列:在user表中,查询姓张的用户,按年龄从大到小排序
      select * from user where username like "张%" order by age asc;
    • 使用group by分组查询
      1
      select 字段,function(字段) from 表名 where 字段 operator 值 group by 字段;