MySQL强化

-- 查询类型 cate_name 为 '超级本' 的商品名称 name 、价格 price

select name,price from goods where cate_name = "超级本";

 

-- 显示商品的种类

-- 1 分组的方式( group by )

select cate_name from goods group by cate_name;



 

-- 2 去重的方法( distinct )不要去记

select distinct cate_name from goods;



 

-- 求所有电脑产品的平均价格 avg ,并且保留两位小数( round )

select round( avg(price),2) from goods ;


 

-- 显示 每种类型 cate_name (由此可知需要分组)的 平均价格

select cate_name,avg(price) from goods group by cate_name;


 

-- 查询 每种类型 的商品中 最贵 max 、最便宜 min 、平均价 avg 、数量 count

select cate_name,max(price),min(price),avg(price),count(*) from goods group by cate_name;



 

-- 查询所有价格大于 平均价格 的商品,并且按 价格降序 排序 order desc

--1.平均价格

select avg(price) from goods;



 

--2.大于平均价格

select * from goods where price > (select avg(price) from goods) order by price desc;


 

-- 查询每种类型中最贵的电脑信息(难)

 

-- 1 查找 每种类型 中 最贵的 max_price 价格

 

select cate_name ,max(price) from goods group by cate_name;


 

-- 2 关联查询

select goods.* from goods inner join (select cate_name ,max(price) as price_max from goods group by cate_name) as goods_max

on goods.cate_name = goods_max.cate_name and goods.price = goods_max.price_max;



 

--第四部 修改表结构(重点是思路)

 

-- 创建表格的格式

--create table if not exists 表名 (

--id int unsigned primary key auto_increment, 这个是主键

--name varchar(40) not null); 这个名称


 

--第一步 创建 "商品种类表" -goods_cates

--id跟名称

create table goods_cates(id int unsigned primary key auto_increment ,name varchar(50) not null);


 

--第二步 创建 "商品品牌表" -goods_brands

--id跟名称

create table goods_brands(id int unsigned primary key auto_increment,name varchar(50) not null);


 

--第三步 更新种类信息表

--1. 要把类型查询出来

select cate_name from goods group by cate_name;


 

--2. 批量插入数据

--insert into goods (name) values('123');

insert into goods_cates(name) (select cate_name from goods group by cate_name);


 

--第四 更新品牌信息表

--1. 要把品牌信息查询出来

select brand_name from goods group by brand_name;


 

--2. 批量插入数据

insert into goods_brands(name) (select brand_name from goods group by brand_name);



 

--第五 更新种类信息成种类信息表中的id

--1. 种类信息跟要替换成表进行内联

select * from goods inner join goods_cates on goods.cate_name = goods_cates.name;



 

--2. 通过特殊的update 进行更新

-- update (select * from goods inner join goods_cates on goods.cate_name = goods_cates.name) set goods.cate_name = goods_cates.id;


 

update (goods inner join goods_cates on goods.cate_name = goods_cates.name) set goods.cate_name = goods_cates.id;


 

要注意一下这个批量更新是特殊语句;

--第六 更新品牌信息成品牌信息表的id

--1. 把要更新的表两张表进行内联

 

select * from goods inner join goods_brands on goods.brand_name = goods_brands.name;

 

--2.通过特殊的update语句进行更新

 

update (goods inner join goods_brands on goods.brand_name = goods_brands.name) set goods.brand_name = goods_brands.id;

 

    

--第七步 修改字段(注意类型必须跟外键的主键类型一致)

-- alter table 表名 change 旧字段 新字段 类型

alter table goods change cate_name cate_id int unsigned;

alter table goods change brand_name brand_id int unsigned;

 

-- 外键的使用(了解)

 

-- 向goods表里插入任意一条品牌数据"老王牌拖拉机"

-- 你会发现我们外键不能创建,因为没有对应的品牌


 

-- 约束 数据的插入 使用 外键 foreign key

-- alter table 主表名 add foreign key (主表的外键) references 外键表(外键主键);

alter table goods add foreign key(cate_id) references goods_cates(id);


 

-- 创建表的同时设置外键 (注意 goods_cates 和 goods_brands 两个表必须事先存在)

-- foreign key (主表的外键) references 外键表(主键)

create table if not exists goods_key(

id int primary key auto_increment not null,

name varchar(40) default '',

price decimal(5,2),

cate_id int unsigned,

brand_id int unsigned,

is_show bit default 1,

is_saleoff bit default 0,

   

   foreign key (cate_id) references goods_cates(id)

);


 

-- 如何取消外键约束

-- 需要先获取外键约束名称,该名称系统会自动生成,可以通过查看表创建语句来获取名称

--show create table goods;

 

-- 获取名称之后就可以根据名称来删除外键约束

--alter table students drop foreign key students_ibfk_1;\

alter table goods drop foreign key goods_ibfk_1;

-- 什么是视图?

    -- 通俗的讲,视图就是一条SELECT语句执行后返回的结果集。

    -- 所以我们在创建视图的时候,主要的工作就落在创建这条SQL查询语句上。

    

-- 视图的特点

    -- 视图是对若干张基本表的引用,一张虚表,查询语句执行的结果,

    -- 不存储具体的数据(基本表数据发生了改变,视图也会跟着改变);

    

-- 视图的最主要的作用

    -- 如果数据库因为需求等原因发生了改变,为了保证查询出来的数据与之前相同,

    -- 则需要在多个地方进行修改,维护起来非常麻烦,这个时候使用视图就可解决这个问题

    

-- 视图的定义方式

    -- create view 视图名称(一般使用v开头) as select语句;

    select * from goods inner join goods_cates on goods.cate_id = goods_cates.id;

 

    create view v_goods as select goods.*,goods_cates.id as c_id,goods_cates.name as c_name from goods inner join goods_cates on goods.cate_id = goods_cates.id;

        -- 相当于python中

        v_goods = select goods.*,goods_cates.id as c_id,goods_cates.name as c_name from goods inner join goods_cates on goods.cate_id = goods_cates.id;



 

    create view v_goods as select name as name from goods;

--具体场景使用


 

-- 解决数据库发生改变 python程序也需要改变的问题


 

    

-- 删除视图

    -- drop view ;

    drop view v_goods;



 

-- 注意

    -- 视图只能用于查询

    

-- 视图作用总结

 

-- 1 提高了重用性,就像一个函数

-- 2 对数据库重构,却不影响已经编写好的程序运行

-- 3 让数据更加清爽


 

-- 视图最主要解决的问题

    -- 程序对数据库操作,一旦数据库发生变化,程序需要修改,这时如果使用视图就可以解决这个问题






 

-- 事物(ACID)

    

    -- 原子性 一致性

        第一步 打开 终端1 终端2

        第二步 终端1 打开事物 begin/start transaction

             终端1 update 表名 set 字段="xxx" where ...;

             终端1 select * from 表名; 发现数据改变

        第三步 终端2 select * from 表名;

             发现数据其实并没有改变 其实这个时候对数据的相关操作信息存在缓存中,

             当commit之后,这些操作才会一次性的完成

        第四步 终端1 commit 数据数数据真的改变

             终端2 select * from 表名,数据改变了

            

    -- 隔离性

        第一步 打开 终端1 终端2

        第二步 终端1 打开事物 begin

             终端1 update 表名 set 字段="xxx" where ...;

        第三步 终端2 update 表名 set 字段="yyy" where ...;

             发现 处于阻塞状态

        第四步 终端1 commit

             终端2 阻塞状态解除 数据修改成 yyy

            

    -- 回滚(rollback)

        第一步 打开 终端1 begin

        第二步 终端1 update 表名 set 字段="xxx" where ...;

        第三步 rollback 数据返回最开始的原始值

 

    

    

    -- 持久性

        -- 一旦事务提交,则其所做的修改会永久保存到数据库

        

    -- 注意

        -- innodb能使用事物

        -- 使用python操作数据库的时候 默认开启事物的

        -- 但是python对数据库进行增删改的时候 需要手动commit

        

        -- 使用终端操作数据库(也就是mysql的客户端)的时候 也是默认开始事物的

        -- 只是在回车确认操作的时候 终端会默认的commit 所以我们不需要commit

        

        

-- 事物最主要解决的问题

    -- 某些事情需要一次性完成 中途不允许出现中断 例如银行取钱 事物可以解决这种问题

        

        

        


 

        

        

-- 索引

    -- 注意

    -- 要注意的是,建立太多的索引将会影响更新和插入的速度,因为它需要同样更新每个索引文件。

    -- 对于一个经常需要更新和插入的表格,就没有必要为一个很少使用的where字句单独建立索引了,

    -- 对于比较小的表,排序的开销不会很大,也没有必要建立另外的索引。

 

    -- 建立索引会占用磁盘空间


 

-- 索引最主要解决的问题

    -- 当数据非常庞大时,并且这些数据不需要经常修改,为了加快查询速度,我们会使用索引

    

-- 创建一张表

    create table test_index(title varchar(10));

-- python插入10条数据


 

--测试步骤

    1. 开启运行时间监测:

        set profiling=1;

    2. 查找第1万条数据ha-99999

        select * from test_index where title='ha-99999';

    3. 查看执行的时间:

        show profiles;

    4. 为表title_index的title列创建索引:

        create index 索引名称 on 表名(字段名称)

        

            create index my_index on test_index(title);

    5. 执行查询语句:

        select * from test_index where title='ha-99999';

    6. 再次查看执行的时间

        show profiles;

 

    --查看索引

        -- show index from 表名;

        show index from test_index;

 

    --删除索引

 

        ---drop index 索引名 on 表名;

        drop index my_index on test_index;

 

    

    

    

    

-- 权限管理(了解) 对用户的管理

 

    -- 查看有哪些账户

        1 使用root账户登录

        2 使用mysql数据库

        3 用户的信息存放在 user 表中

            -- select host,user,authentication_string from user;

                Host表示允许访问的主机 % localhost

                User表示用户名

                authentication_string表示密码,为加密后的值

            select host,user,authentication_string from user;

                

    -- 创建账户、授权

        

        -- 案例一

        1 使用root账户登录

        

        2 创建账户并授予所有权限(部分权限)

        -- grant 权限列表 on 数据库 to '用户名'@'访问主机' identified by '密码';(语法格式)

        grant select on jing_dong.* to 'python13'@'localhost' identified by '123';

            grant select on jing_dong.* to 'p13'@'%' identified by '123';

            

            -- 注意

            -- 1 访问主机通常使用 百分号% 表示此账户可以使用任何ip的主机登录访问此数据库

            -- 2 访问主机可以设置成 localhost 或具体的ip,表示只允许本机或特定主机访问

            

        -- 查看用户有哪些权限

        -- show grants for 用户@访问主机;

        show grants for python13@'localhost';

    

            

        3 退出root的登录 使用oldyang账户登录

        -- 使用查询操作是可以的,(插入也可以)

        

        -- 使用其他操作是不可以的

            

            

        

        -- 案例二

        1 使用root账户登录

        

        2 创建账户并授予所有权限(所有权限) all privileges

        --grant 权限列表 on 数据库 to '用户名'@'访问主机' identified by '密码';(语法格式)

        -- 注意 访问链接设置成 % 十分危险 不要使用

            grant all privileges on jing_dong.* to 'python'@'%' identified by '123';

        

        

    -- 修改权限(增加)

        1 使用root账户登录

        

        2 修改用户权限

        -- grant 权限名称 on 数据库 to 账户@主机 with grant option;(语法格式)

        grant update on jing_dong to 'python13'@'localhost' with grant option;

        

        3 刷新权限

            flush privileges;

            

    -- 修改密码

        1 使用root账户登录

        

        2 选择mysql数据库

        

        3 使用password()函数进行密码加密 对user表进行修改

        -- update user set authentication_string=password('新密码') where user='用户名';(语法格式)

        

        例:update user set authentication_string=password('222') where user='python13';

 

        4 刷新权限

            flush privileges;

        

    -- 删除用户

        1 使用root账户登录

        

        2 删除用户

            第一种方式 drop user '用户名'@'主机';(语法格式) 卸载

            

                        例:drop user 'python13'@'%';

                        

                        

            第二种方式   delete from user where user='用户名';(语法格式) 手动删除

            

                        例:delete from user where user='laowang';

                        

                        -- 操作结束之后需要刷新权限

                        flush privileges

                        

        -- 推荐使用语法1删除用户, 如果使用语法1删除失败,采用语法2方式

    

    

    -- 远程登录(谨慎使用)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值