首页 > 数据库技术 > 详细

SQL查询

时间:2019-05-19 22:23:55      阅读:141      评论:0      收藏:0      [点我收藏+]

技术分享图片

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);
多表查询
  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

 

SQL查询

原文:https://www.cnblogs.com/zhaoyang1997/p/10890956.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!