MySQL数据库基础笔记

时间:2019-11-18
本文章向大家介绍MySQL数据库基础笔记,主要包括MySQL数据库基础笔记使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

数据库

数据库就是存储和管理数据的仓库,用户可以对数据库中的数据进行增删改查等操作。

数据库的分类

  • 关系型数据库(Oracle、MySQL、SQLite等)

  • 非关系型数据库(Redis、MongoDB等)

MySQL简介

MySQL是一个关系型数据库,由MySQLAB公司开发,目前属于Oracle旗下。

特点:开源、支持大型数据库、使用标准SQL、适用于多种操作系统以及提供多种编程语言接口。

安装(Ubuntu中):

sudo apt-get install mysql-server
sudo apt-get install mysql-client

MySQL客户端连接服务端:

mysql -uusername -ppassword

退出:exit、quit

常用数据类型

int、bit、decimal、varchar、char、date、time、datetime、enum、text

常用数据约束

主键primary key、非空not null、唯一unique、默认default、外键foreign key

数据库设计三范式

  1. 原子性

  2. 满足1,表必须有主键,非主键字段必须完全依赖于主键

  3. 满足2,非主键必须直接依赖于主键

E-R模型

实体-关系模型,E-R模型就是描述数据库存储数据的结构模型

  • 一对一

  • 一对多

  • 多对多

常用SQL语句

  1. 基本语句

    • 查看所有数据库 show databases;

    • 创建数据库 use database_name;

    • 使用数据库 use database_name;

    • 查看当前使用的数据库 selecet database();

    • 删除数据库 drop database database_name;

    • 查看当前库中所有表 show tables;

    • 建表 create table table_name(id int unsigned primary key auto_increment, name varchar(30) not null, age tinyint unsigned not null);

    • 修改表

      • 添加字段 alter table table_name add sexy bit not null;

      • 修改字段类型 alter table table_name modify sexy tinyint unsigned not null;

      • 修改字段名 alter table table_name change sexy gender tinyint unsigned not null;

      • 删除字段 alter table table_name drop gender;

      • 设置外键 alter table table_name add foreign key(cls_id) references classes(id);

      • 删除外键 alter table table_name drop foreign key cls_id;

    • 查看建表(库)SQL语句 show create table(database) table_name(database_name);

    • 删除表 drop table table_name

  2. 表数据的基本增删改查:

      • insert into table_name values();

      • insert into table_name(name) values('a');

      • insert into table_name values(),(),();

    • 删:delete from table_name where id=33;

      • 逻辑删除,设置一个bit类型字段表示是否删除

    • 改: update table_name set name='c', age=18 where id=222;

      • select * from table_name;

      • select name,age from table_name;

  3. as关键字(起别名)

    • select t.name,t.age from table_name as t;

  4. distinct关键字(去重)

    • select distinct name from table_name;

  5. where条件查询

    • select * from table_name where id<10 and age>18;

  6. 模糊查询

    • select * from table_name where name like '郭%' or name like '郭威_';

  7. 范围查询

    • select * from table_name where id between 5 and 10 or id in (3, 19);

  8. 空判断查询

    • select * from table_name where gender is null;

    • ifnull(age,18)判断字段是否为空,为空则使用提供值

  9. 排序查询

    • select * from table_name order by age desc;

      desc表示倒序排序,ase升序排序,默认升序

  10. 分页查询

    • select * from table_name limit start,count;

  11. 聚合函数查询

    count()、max()、min()、sun()、avg()、round(avg(age),保留小数位)

    • select avg(age) from table_name where gender=0;

  12. 分组查询

    • select gender,avg(gender) from table_name group by gender with rollup;

      with rollup 在最后新增一行查询的记录

    • select gender,count(*) from table_name group by gender having count(*)>5;

      having 过滤分组数据,只用于group by

  13. 连接查询

    • 内连接

      • select name from table1 inner join table2 on table1.age = table2.age;

    • 左连接

      • left join

    • 右连接

      • right join

    • 自连接

      • select a.id,a.name from table as a inner join table as b where a.pid=b.id;

  14. 子查询

    • select * from table where age > (select avg(age) from table);

      性能较差

  15. 将查询结果插入其他表

    • insert into country(name) select hero_country from hero group by hero_country;

  16. 连接更新

    • update hero h inner join country c on h.country=c.name set h.country=c.id;

  17. 建表同时添加数据

    • create table country(id int unsigned primary key auto_increment,name varchar(30) not null)select hero_country from hero group by hero_country;

事务

事务就是一系列sql操作作为一个单元执行,要么全部执行,要么全部不执行。

  1. 事务的四大特性

    • 原子性

    • 一致性

    • 隔离性

    • 持久性

  2. MySQL中使用事务需选择InnoDB存储引擎,其他引擎不支持事务

  3. 开启事务

    • begin;

    • start transaction;

    • set autocommit=0;

      MySQL默认自动提交,该设置关闭默认提交功能

  4. 提交事务

    • commit;

  5. 回滚事务

    • rollback;

索引

  • 创建索引 alter table table_name add index name_index(name);

  • 联合索引 alter table table_name add index name_index(name,age);

  • 删除索引 alter table table_name drop index name_index;

引擎

原文地址:https://www.cnblogs.com/mumuxin-gv/p/11882603.html