zoukankan      html  css  js  c++  java
  • Python全栈 MySQL 数据库 (SQL查询、备份、恢复、授权)

    ParisGabriel
     
     
             每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰
     
      开局一张图
     
    今天接着昨天的说
     
    索引有4种:
         普通 索引 :index 
         唯一索引:unique
         主键索引:primary key
         外键索引:foreign key
    索引查询命令:
          show index from 表名G;
             Non_Unique:1   :index
             Non_Unique:0  :unique
     
     
    外键索引(foreign key):
        定义:让当前字段的值在另一个表的范围内选择
        语法:
           foreign key(参考字段名)
           references 主表(被参考字段名)
           on delete 级联动作
           on update 级联动作
        使用规则:
           主表从表字段数据类型要一致
           主表被参考字段一般是:主键
        删除外键:
          alter table 表名 drop foreign key 外键名;
          外键名查询:show create table 表名;
        级联动作:
          cascade
             级联删除、更新(只限于参考字段)
          restrict(默认)
            从表有相关记录不允许主表操作
          set NULL
            主表删除、更新从表关联记录字段值为NULL
        已有表添加外键:
           alter table 表名 add
           foreign key(参考字段)reference主表(被参考字段)
           on delete ...
           on update ...
    表的复制
      复制表:
        create table 表名 select... from where 表名;
      表结构:
        create table 表名 select * from 表名 where false;
      注意:
        复制表的时候不会把原表的 键(key)属性复制过来
     
     
    SQL 查询(高级)
     
    嵌套查询(子查询):
           定义内层查询结果作为外层的查询条件
       语法格式:
         select ...from 表名 where 条件(select...);
    多表查询:
      两种方式
      1.
          select 字段名列表 from 表名列表;(笛卡尔积
          select * from t1,t2
          select t1.name,t2.name from t1,t2;
      2.
         select t1.name,t2.name from t1,t2
          where 条件
    链接查询:
       1.内链接
         select 字段名 from 表1 
         inner join 表2 on 条件
         inner join 表3 on 条件...;
       2.外链接
          1.左链接
            以左表为主显示查询结果
    select 字段名 from 表1 
    left join 表2 on 条件
    left join 表3 on 条件...;
          2.右链接
            以右表为主显示查询结果
    select 字段名 from 表1 
    right join 表2 on 条件
    right join 表3 on 条件...;
    数据备份:
        mysqldump  在Linux终端操作
        完全备份:
           mysqldump -u用户 -p源库名 > ~/xxx.sql
           --all-databases  备份所有库
           库名             备份单个库
           -B 库1 库2..     备份多个库
           库名 表1 表2...  备份指定库指定表
    数据恢复:
        恢复单个库
              mysql -uroot -p < 目标库名 xxx.sql
        从所有库备份中恢复某一个库(-one-database)
              mysql -uroot -p --one-database 目标库名 < xxx.sql
         注意:
           1.恢复如果恢复到原库会将表中数据覆盖新增表不会删除
           2.数据恢复时如果恢复库不存在,则必须先创建空库
    MySQL的账户管理:
          1.开启mysql的远程连接
            sudo -i
    cd /etc/mysql/mysql.conf.d/
    subl mysql.cnf
            #bind-address = 127.0.0.1  注释掉
            /etc/init.d/mysql restart
     
        2.添加授权用户
            用root用户登录mysql
       mysql -uroot -p123456
            授权:
     grant 授权列表 on 库.表 to “用户名“@”%”
     identified by “密码” with grant option
             权限列表:
        all privileges、select、insert
        库.表: *.*  所有库所有表
    示例:
        1、添加授权用户tiger,密码123,对所有库的所有表有所有权限
          grant all privileges on *.* to "tiger"@"%" identified by "123" with grant option;

     

    综合性练习:

    综述:两张表,一张顾客信息表customers,一张订单表orders
    1、创建一张顾客信息表customers,字段要求如下:
    c_id 类型为整型,设置为主键,并设置为自增长属性
    c_name 字符类型,变长,宽度为20
    c_age 微小整型,取值范围为0~255(无符号)
    c_sex 枚举类型,要求只能在('M','F')中选择一个值
    c_city 字符类型,变长,宽度为20
    c_salary 浮点类型,要求整数部分最大为10位,小数部分为2位

    在表中任意插入3条记录,c_name为"Zhangsan","Lisi","Wangwu", c_city尽量 写"Beijing","Shanghai" ......

    insert into customers values
    (1,"Zhangsan",25,"M","Beijing",8000),
    (2,"Lisi",30,"F","Shanghai",10000),
    (3,"Wangwu",27,"M","Shenzhen",3000);

    2、创建一张订单表orders,字段要求如下:
    o_id 整型
    o_name 字符类型,变长,宽度为30
    o_price 浮点类型,整数最大为10位,小数部分为2位
    设置此表中的o_id字段为customers表中c_id字段的外键,更新删除同步

    在表中任意插入5条记录(注意外键限制)
    o_name分别为"iphone","ipad","iwatch","mate9","r11",其他信息自己定
    insert into orders values
    (1,"iphone",5288),
    (1,"ipad",3299),
    (3,"mate9",3688),
    (2,"iwatch",2222),
    (2,"r11",4400);

    3、返回customers表中,工资大于4000元,或者年龄小于29岁,满足这样条件的前2条记录

    4、把customers表中,年龄大于等于25岁,并且地址是北京或者上海,这样的人的工资上调15%

    5、把customers表中,城市为北京的顾客,按照工资降序排列,并且只返回结果中的第一条记录

    6、选择工资c_salary最少的顾客的信息

    7、找到工资大于5000的顾客都买过哪些产品的记录明细

    8、删除外键限制

    9、删除customers主键限制
    1、删除自增长属性
    2、删除主键限制

    综述:两张表,一张顾客信息表customers,一张订单表orders
    1、创建一张顾客信息表customers,字段要求如下:
      c_id 类型为整型,设置为主键,并设置为自增长属性
      c_name 字符类型,变长,宽度为20
      c_age 微小整型,取值范围为0~255(无符号)
      c_sex 枚举类型,要求只能在('M','F')中选择一个值
      c_city 字符类型,变长,宽度为20
      c_salary 浮点类型,要求整数部分最大为10位,小数部分为2位
    create table customers( c_id int primary key auto_increment, c_name varchar(20), c_age tinyint unsigned, c_sex enum("M","F"), c_city varchar(20), c_salary float(12,2) ); 在表中任意插入3条记录,c_name为"Zhangsan","Lisi","Wangwu", c_city尽量 写"Beijing","Shanghai" ...... insert into customers values (1,"Zhangsan",25,"M","Beijing",8000), (2,"Lisi",30,"F","Shanghai",10000), (3,"Wangwu",27,"M","Shenzhen",3000); 2、创建一张订单表orders,字段要求如下: o_id 整型 o_name 字符类型,变长,宽度为30 o_price 浮点类型,整数最大为10位,小数部分为2位 设置此表中的o_id字段为customers表中c_id字段的外键,更新删除同步
    create table orders( o_id int, o_name varchar(30), o_price float(12,2), foreign key(o_id) references customers(c_id) on delete cascade on update cascade ); 在表中任意插入5条记录(注意外键限制) o_name分别为"iphone","ipad","iwatch","mate9","r11",其他信息自己定
    insert into orders values (1,"iphone",5288), (1,"ipad",3299), (3,"mate9",3688), (2,"iwatch",2222), (2,"r11",4400); 3、返回customers表中,工资大于4000元,或者年龄小于29岁,满足这样条件的前2条记录 select * from customers where c_salary > 4000 or c_age < 29 limit 2; 4、把customers表中,年龄大于等于25岁,并且地址是北京或者上海,这样的人的工资上调15% update customers set c_salary=c_salary*1.15 where c_age >= 25 and c_city in ("Beijing","Shanghai"); 5、把customers表中,城市为北京的顾客,按照工资降序排列,并且只返回结果中的第一条记录 select * from customers where c_city="Beijing" order by c_salary desc limit 1; 6、选择工资c_salary最少的顾客的信息 select * from customers where c_salary = (select min(c_salary) from customers); 7、找到工资大于5000的顾客都买过哪些产品的记录明细 select * from orders where o_id in (select c_id from customers where c_salary > 5000); 8、删除外键限制 1、show create table orders; 2alter table orders drop foreign key orders_ibfk_1; 9、删除customers主键限制 1、删除自增长属性 alter table customers modify c_id int; 2、删除主键限制 alter table customers drop primary key; 10、增加customers主键限制c_id alter table customers add primary key(c_id);
  • 相关阅读:
    图像膨胀
    图像腐蚀
    C#多线程与异步
    matplotlib画图总结--多子图布局
    matplotlib画图总结--常用功能
    STM32 MCU一次计算优化和提速
    数字麦克风PDM信号采集与STM32 I2S接口应用--笔记目录
    数字麦克风PDM信号采集与STM32 I2S接口应用(三)
    数字麦克风PDM转PCM与STM32 I2S接口应用----重要文档列表
    数字麦克风PDM信号采集与STM32 I2S接口应用(二)
  • 原文地址:https://www.cnblogs.com/ParisGabriel/p/9410701.html
Copyright © 2011-2022 走看看