数据库mysql

SQL分类

  • DDL:操作库和表的

  • DML:增删改查表中数据的

  • DQL:查询表中数据的

  • DCL:管理用户、授权

    • DBA:数据库管理员

DDL库的操作

show database mysql;查询所有库名 show create database mysql;数据库字符集查询; create database superman;创建supperman库 create database if not esists superman;创建库的同时看库是否存在; create database supperman character set 字符集类型:(utf8或者jdk)创建字符集 修改:Update alter database 数据库名 character set 字符集; 更改字符集类型 Delete:删除 drop database 数据库名称 drop database if exists superman;superman 库是否存在 select database();查询正在使用的库

表的操作

Retrieve查询 show tables:查询所有的表 desc 表名:查询表结构 Create创建 create table 表名(列名1 类型1,列名2 类型2,列名3 类型3) Delete:删除 drop table if exists 表名; --create table hero like superman --hero复制superman内容 Update 修改 1、alter table 表名 rename to 新表名;修改表名 2、alter table 表名character set 字符集名称;修改表的字符集 3、alter table 表名 add 列名 数据类型 ;添加一列 4、alter table 表名 change 列名 新列名 varcher(20); 5、alter table 表名 drop 列明;

DML表中数据的增删改

增:inset into 表名(列明1,列名2)values (值1,值2); 删:delete from 表名 where 条件 truncate table 表名;删除表明再创建一个一样的空表 修改:update 表名 set 列名1=值1,列名2=值2 where 【id=n】 查:select

oder by排序字段,排序方式

select * from student oder by 列名 {ASC:升序默认 DESC:降序}

聚合函数

count 个数 max 最大 min 最小 sum 和 avg 计算平均值 select count(列名) from student:查询所有个数 select count (if null(列名,0))from student :把空值转换为0 select count(id) from student;

分组查询 group by 字段

分组查询的字段:分组字段/聚合函数

按照性别分组,分别查询男、女同学的平均分、人数 select sex,avg(math),count(id) from student group by sex(以什么为基础) 按照性别分组,分别查询男、女同学的平均分、人数 要求:分数低于70分的人,不参与分组 select sex,avg(math),count(id) from student where math>70 group by sex; 按照性别分组,分别查询男、女同学的平均分、人数 要求:分数低于70分的人,不参与分组,人数大于两人 select sex,avg(math),count(id) from student where math>70 group by sex having count(id)>2;

分页查询

limit开始的索引,分页查询条数 select *from student limt o,3; 去除重复字段 select distinct address from student select name math english math+ifnull(english,0) from student:查询数学 英语 总分

条件查询

查询大于20小于30 select * from student where age>=20 and age<=30; select from student where age between 20 and 30 查询22、18、25 select from student where age= 22 or age=18 or age=25 select from student where in(22,18,25); 查询空值不能用 = select*from student where English is null;

模糊查询 like

占位符: _单个任意字符 %多个任意字符 select *from student where name like "马%"

约束

对表中的数据进行限定,表征数据的正确性,完整性

分类: 主键约束:primary key 非空约束:not null 唯一约束:unique 外键约束:foreign key

主键约束:primary key

含义:

1、非空且唯一

2、一张表只有一个字段为主键

3、主键是表中记录的唯一标识

唯一约束:unique

删除唯一约束

alter table 表名 drop index 列名

添加唯一约束

alter table 表名 modify 列名 vachar (20)unique;

非空约束:not null

 

 

外键约束 foreign key

 

让表与表产生关系,从而保证数据的正确性。 1、在创建表时,可以添加外键 create table 表名( 。。。外键列 constraint 外键名称 foreign key(外键列名称) references 主表名称(主表列名称) ); 2、删除外键 alter table 表名 drop foreign key 外键名称; 3、创建表之后,添加外键 alter table 表名 add constraint 外键名称 foreign key (外键字段名称) regerences 主表名称(主表列名称)

添加外键,设置级联更新on update cascade,级联删除on delete cascade;

alter table employee add constraint emp_dept_fk foreign key (dep_id) regerences depment(id)on update cascade;

级联操作

更改update employee set dep_id=null where dep_id=1;把1的值改为null,再更改另一张表,之后在改回来

1、添加级联

 

2、分类:

  • 级联更新on update cascade

  • 级联删除on delete cascade

多表之间的关系

一对一

一对多 外键实现

多对多 中间表来实现 至少有两个字段 分别是各个表的主键

 

数据库范式

第一范式:原子性

第二范式:消除部分依赖

第三范式:消除传递依赖

巴斯-科德范式

第五范式

第六范式

数据库的备份与还原

1、命令行:

  • MySQL -u用户名 - p密码 库名>保存路径

  • 还原:

    • 登录数据库

    • 创建数据库

    • 使用数据库

    • 执行文件——source文件路径

2、图形化工具:

多表查询

  • 查询语法:

selet 列名列表 from 表名列表 where 。。。

多表查询

--笛卡尔积:

  • 有两个集合A、B取这两个集合的所有组成情况。

  • 完成多表查询消除无用数据

--消除无用的数据

  • 多表查询分类

    • 内连接查询:就是

    • select * from表一,表二 where条件;
      • 隐式内连接:使用where条件消除

      • 显式内连接: 语法:select 字段列表 from 表名1 (inner可以省略) join 表名2 on 条件

        • select*from emp inner join dept on emp.'dep_id'=dept.'id';

        • select*from emp join dept on emp.'dep_id'=dept.'id';

      • 内连接查询注意事项:

        • 从那些表查询

        • 条件是什么

        • 查询那些字段

    • 外连接查询:

      • 左外连接 语法:select 字段列表 from 表1 left (outer可以省略) join表2 on 条件 查询的是左表所有数据以及其交及部分

      • 右外连接

    • 子查询:查询中嵌套查询

    • 子查询情况

      • 子查询的结果是单行单列的:子查询作为条件

      •  查询比平均值小的情况用运算符
        select*from emp where emp.salary<(select avg(salary)from emp);
      • 子查询的结果是多行单列的

      • 财务部和市场部员工信息
        select
        *
        from
        dept
        where
        name='财务部'or name ='市场部';
        select
        *
        from
        empt
        where
        dep_id=2 or dep_id=3;

        select
        *
        from
        empt
        where
        dep_id in
        (elect
        *
        from
        dept
        where
        name in(财务部,市场部));
      • 子查询是多行多列的

        • 子查询可以作为一张虚拟表。

        • select*from dept t1,(select*from emp where emp.'join_date'>'2011-11-11') t2 where t1.id=t2.dept_id;

事务

  • 基本介绍:如果一个包含多个步骤的业务操作,被事务管理,要么同时成功,要么同时失败。

    • 操作:

      • 开启事务:start transaction

      • 回滚:rollback

      • 提交:commit

      • 在msql数据库中事务默认提交

        • 事务提交的两种方式:

          • 自动提交:

            • MySQL就是自动提交的

            • 一条DML(增删改)语句会自动提交一次事务

          • 手动提交:

            • Oracle数据库默认是手动提交事务

            • 须先开启事务,在提交

        • 修改事物的的提交方式

          • 查看事物的默认提交方式:SELECT @@autocommit;——1代表自动提交 0 代表手动提交

          • 修改默认提交方式:set @@autocommit = 0;

  • 事务的四大特征:

    • 原子性:是不可分割的最小单位,要么同时成功,要么同时失败。

    • 持久性:当事务提交或回滚红,数据库会永久保存。

    • 隔离性:多个事务之间相互独立。

    • 一致性:实务操作前后,数据总量不变

  • 事务隔离级别:

    • 脏读

    • 不可重复读

    • 幻读

DCL

管理用户,授权

  • 管理用户

    • 添加用户

    ——创建用户
    create user'用户名'@'主机名' identified '密码';
    • 删除用户

    drop user '用户名'@'主机名';
    • 修改用户密码

    update user set password = password('新密码') where user ='用户名';
    set password for '用户名'@'主机名' = password('新密码');
    mysql中忘记root用户密码?
    1、cmd--->net stop mysql 停止MySQL服务(需管理员运行cmd)
    2、使用无验证方式启动MySQL服务:mysql--skip-grant-tables
    3、打开新的cmd窗口,直接输入MySQL命令,敲回车,可以登录成功
    4、use mysql;
    5、update user set password = password('新密码') where user ='用户名';
    6、关闭窗口
    7、打开任务管理器,手动结束MySQLd.exe的进程
    8、启动MySQL服务
    9、使用新密码登录。
    • 查询用户

    切换用户
    use mysql;
    查看user表
    select * from user;

     

索引(index)

索引在数据库的表的字段上添加的,是为了提高效率存在的一种机制。

一张表的一个字段可以添加一个索引,多个字段也可以添加索引。

索引相当于一本书的目录,是为了缩小扫描范围而存在的一种机制。

索引是一个B-Tree数据结构,遵循左小右大原则存放,采用中序遍历方式便利取出数据。

在mysql当中,主键上、unique字段上都会自动添加索引

什么条件添加索引?

1、数据量庞大

2、该字段经常出现在where后面,总被扫描

3、该字段很少的DML操作。(DML之后,索引需要重新排序)

建议不要随便添加索引,因为索引也需要维护的,太多的话反而会降低系统性能。

建议通过主键查询,建议通过unique约束的字段进行查询,效率比较高。

  • 创建索引:create index emp_ename_index index on emp(ename);

给emp表的emame字段添加索引,起名:emp_ename_index

  • 删除索引:drop index emp_ename index on emp;

将emp表上的emp_ename_index索引对象删除。

  • 检查是否有索引:explain select*from 表名 where 字段名='king';

索引失效

1、模糊查询中以%开头,索引失效

2、使用or失效,要求两边都有索引

3、(两个字段,或者更多字段联合起来)复合索引,没有使用左侧的列

4、在where索引列参加运算

5、在where当中使用了函数

优化的时候优先考虑索引,索引是重要手段

  • 单一索引:一个字段上添加索引

  • 复合索引:两个字段或者更多字段上添加索引

  • 主键索引:主键上添加索引

  • 唯一索引:具有unique约束的字段上添加索引

注意:唯一性比较弱的字段上添加索引用处不大。

posted @ 2020-12-29 18:34  MrOldJiang  阅读(90)  评论(0)    收藏  举报