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;
2、alter 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);

Python全栈 MySQL 数据库 (SQL查询、备份、恢复、授权)的更多相关文章

  1. Python全栈 MySQL 数据库(SQL命令大全、MySQL 、Python调用)

    为了梦想与了信仰    开局一张图   主要三个方面: 1.Linux终端命令 2.MySQL语句 3.Python调用   先删库 再跑路.....                         ...

  2. Python全栈 MySQL 数据库 (引擎、事物、pymysql模块、orm)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图     存储引擎(处理表的处理器)     基本操作:         ...

  3. Python全栈 MySQL 数据库 (简述 、安装、基本命令)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图     一个月的python已经结束了  下面就是数据库了   先说M ...

  4. Python全栈 MySQL 数据库 (索引、数据导入、导出)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图     表字段重命名(change)   alter table 表名 ...

  5. Python全栈 MySQL 数据库 (表字段增、删、改、查、函数)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图         查询SQL变量 show variables 1.表字 ...

  6. 巨蟒python全栈开发数据库攻略3:行记录的操作&单表查询3

    1.数据行的增删改 2.单表查询 select&where条件 3.group by&having&order by&limit

  7. Python全栈 MongoDB 数据库(概念、安装、创建数据)

    什么是关系型数据库?           是建立在关系数据库模型基础上的数据库,借助于集合代数等概念和方法来处理数据库中的数据,             同时也是一个被组织成一组拥有正式描述性的表格( ...

  8. MySql数据库 sql查询增加序号的伪列

    在查询数据库的时候,我们有时候需要对查询出来的数据加上序列,1,2,3,……n 例如:我们根据表的某个字段排序后,要对这些数据加上序列,这个时候序号常常不是我们建表时设置好的自增的主键id,怎么办呢? ...

  9. 巨蟒python全栈开发数据库前端6:事件onclick的两种绑定方式&&onblur和onfocus事件&&window.onload解释&&小米商城讲解

    1.回顾上节内容(JavaScript) 一.JavaScript概述 1.ECMAScript和JavaScript的关系 2.ECMAScript的历史 3.JavaScript是一门前后端都可以 ...

随机推荐

  1. An error occurred at line: 1 in the generated java file问题处理

    tomcat6启动后,加载jsp页面报错,提示无法将jsp编译为class文件,主要报错信息如下: An error occurred at line: 1 in the generated java ...

  2. HTML5<fieldset>标签

    1.<fieldset>标签对表单中的相关元素进行分组. 2.<fieldset>标签会在相关表单元素周围绘制边框. <!DOCTYPE html><html ...

  3. c++ 11 线程池的简单封装

    #include <condition_variable> #include <queue> #include <thread> #include <vect ...

  4. Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'name pattern' at character position 36

    例: <aop:config> <aop:pointcut expression="execution(* com.zsn.Service.Impl.*.*(..))&qu ...

  5. 基于mybatis设计简单信息管理系统2

    1.空指针异常 public class CanvasServlet extends HttpServlet { private CanvasService canvasService; privat ...

  6. 【转载】java 客户端链接不上redis解决方案 (jedis)

    本文出自:http://blog.csdn.net/lulidaitian/article/details/51946169 出现问题描述: 1.Could not get a resource fr ...

  7. 02 mysql 基础二 (进阶)

    mysql 基础二 阶段一 表约束 1.not null 非空约束 例子: create table tb1( id int, name varchar(20) not null ); 注意 空字符不 ...

  8. PAT (Basic Level) Practice 1021 个位数统计

    个人练习 给定一个 k 位整数 N=d​k−1​​10​k−1​​+⋯+d​1​​10​1​​+d​0​​ (0≤d​i​​≤9, i=0,⋯,k−1, d​k−1​​>0),请编写程序统计每种 ...

  9. 牛客暑假多校第二场J-farm

    一.题意 White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. T ...

  10. Diycode开源项目 如何解决InputMethodManager造成的内存泄漏问题

    1.内存泄漏的状况及原因 1.1.利用LeakCanary查看内存泄漏的状况 1.2.内存泄漏怎么产生的呢? InputMethodManager.mServicedView持有一个最后聚焦View的 ...