zoukankan      html  css  js  c++  java
  • SQL查询

    SQL查询
      1、distinct语句
        sanguo表中有哪些国家
        select distinct country from sanguo;
        distinct和from之间所有字段都相同才去重
      2、查询时做数学运算
        + - * / %
        查询时所有英雄攻击力翻倍
        select name,gongji*2 as gj from sanguo;
    嵌套查询(子查询)
      1、把内层的查询结果,作为外层查询的条件
      2、把攻击值小于平均攻击值的英雄名字和攻击值显示一下
        select name,gongji from sanguo where gongji<(select avg(gongji) from sanguo);
      3、找出每个国家攻击力最高的英雄的名字和攻击值
        select name,gongji from sanguo where (country,gongji) in (select country,max(gongji) from sanguo group by country);

      create table sheng(
      id int primary key auto_increment,
      s_id int,
      s_name varchar(15)
      )default charset=utf8;
    
      insert into sheng values
      (1, 130000, '河北省'),
      (2, 140000, '陕西省'),
      (3, 150000, '四川省'),
      (4, 160000, '广东省'),
      (5, 170000, '山东省'),
      (6, 180000, '湖北省'),
      (7, 190000, '河南省'),
      (8, 200000, '海南省'),
      (9, 200001, '云南省'),
      (10,200002,'山西省');
    
      create table city(
      id int primary key auto_increment,
      c_id int,
      c_name varchar(15),
      cfather_id int
      )default charset=utf8;
    
      insert into city values
      (1, 131100, '石家庄市', 130000),
      (2, 131101, '沧州市', 130000),
      (3, 131102, '廊坊市', 130000),
      (4, 131103, '西安市', 140000),
      (5, 131104, '成都市', 150000),
      (6, 131105, '重庆市', 150000),
      (7, 131106, '广州市', 160000),
      (8, 131107, '济南市', 170000),
      (9, 131108, '武汉市', 180000),
      (10,131109, '郑州市', 190000),
      (11,131110, '北京市', 320000),
      (12,131111, '天津市', 320000),
      (13,131112, '上海市', 320000),
      (14,131113, '哈尔滨', 320001),
      (15,131114, '雄安新区', 320002);
    
    
      create table xian(
      id int primary key auto_increment,
      x_id int,
      x_name varchar(15),
      xfather_id int
      )default charset=utf8;
    
      insert into xian values
      (1, 132100, '正定县', 131100),
      (2, 132102, '浦东新区', 131112),
      (3, 132103, '武昌区', 131108),
      (4, 132104, '哈哈', 131115),
      (5, 132105, '安新县', 131114),
      (6, 132106, '容城县', 131114),
      (7, 132107, '雄县', 131114),
      (8, 132108, '嘎嘎', 131115);
    View Code

    多表查询
      1、笛卡尔积 :多表查询不加where条件
        select 字段名 from 表1,表2;
        select t1.t1_name,t2.t2_name from t1,t2;
      2、显示省市县信息
        select sheng.s_name,city.c_name,xian.x_name from

    sheng,city,xian where sheng.s_id=city.cfather_id and city.c_id=xian.xfather_id;
    连接查询
      1、内连接(inner join)
        select XXX from 表1 inner join 表2 on 条件
        inner join 表3 on 条件;
        显示省市县信息
        select sheng.s_name,city.c_name,xian.x_name from sheng inner join city on sheng.s_id=city.cfather_id inner join xian on city.c_id=xian.xfather_id;
      2、外连接
        1、左连接(left join)
          以左表为主显示查询结果
        2、右连接(right join)
          以右表为主显示查询结果
      3、显示省市县信息,要求市都显示
        select sheng.s_name,city.c_name,xian.x_name from sheng right join city on sheng.s_id=city.cfather_id left join xian on city.c_id=xian.xfather_id;
    约束 :保证数据的完整性、一致性、有效性
      1、非空约束(not null)
        name varchar(20) not null,
      2、默认约束(default)
        sex enum('M','F','S') not null default 'S'
    索引
      1、定义 :对数据库表的一列或者多列的值进行排序的一种结构(BTree方式)
      2、优点
        加快数据的检索速度
      3、缺点
        占用物理存储空间
        当对表中数据更新时,索引需动态维护,消耗系统资源,降低数据维护速度
      4、哪些字段适合创建索引
        经常用来查询字段(select后)
        经常用来做条件判断字段(where)
        经常用来排序的字段(order by)
    索引重要性
      1、开启运行时间检测:set profiling=1;
      2、执行查询语句(无索引):
        select name from t1 where name='xxx';
      3、在name字段创建索引
        create index name on t1(name);
      4、再执行查询语句(有索引)
        select name from t1 where name='xxx';
      5、查看执行时间
        show profiles;
    普通索引(index)&&唯一索引(unique)
      1、使用规则
        可设置多个字段
        index :无约束,KEY标志为 MUL
        unique:字段值不允许重复,但可为NULL, UNI
      2、创建index/unique
        create table 表名(
        ...,
        index(字段名),
        index(字段名),
        unique(字段名)
        )charset=utf8;
        create [unique] index 索引名 on 表名(字段名);
      3、查看索引
        desc 表名;
        show index from 表名;
      4、删除(一个一个删)
        drop index 索引名 on 表名;
    主键索引(primary key)
      自增长属性(auto_increment)
      1、使用规则
        只能有1个主键字段
        约束 :不允许重复,且不为NULL
        KEY标志 :PRI
        通常设置记录编号字段id,能唯一锁定1条记录
      2、创建
        id int primary key auto_increment,
        alter table 表名 add primary key(id);
      3、删除
        alter table 表名 modify id int;
        alter table 表名 drop primary key;
    如何对密码进行加密(sha1或md5)
      导模块 :from hashlib import sha1
      创建sha1加密对象 :s = sha1()
      进行加密,参数一定为bytes数据类型
      s.update('密码字符串'.encode())
      获取十六进制的加密结果
      pwd = s.hexdigest()
      pwd的值:356a192b7913b04c54574d18c28d46e6395428ab

  • 相关阅读:
    09_ssh服务器的安装和使用
    08_linux下的文件压缩和解压
    38-自定义异常类
    37-手动抛出异常对象
    DataGrip 2020.1 安装与激活
    36-异常的处理
    35-异常
    node+ajax实战案例(1)
    ajax前后端交互原理(7)
    ajax前后端交互原理(5)
  • 原文地址:https://www.cnblogs.com/zhaoyang1997/p/10890956.html
Copyright © 2011-2022 走看看