欢迎投稿

今日深度:

数据库入门,因为子查询SQL语句

数据库入门,因为子查询SQL语句


一、数据库介绍

  • 介绍

    • 数据库就是一个存储数据的仓库,能够长期存储数据且能够对数据进行增删改查
  • 常见的数据库

    • 关系型数据库:

      • mysql:开源免费,常用于中小型项目
      • sql server:微软开发的数据库,适用于微软项目
      • oreale:适用大型项目,比如银行
      • sqlite:主要用于移动应用
    • 非关系型数据库

      • redis:键值对的形式存储数据
      • mangodb:文档的形式存储数据
      • Hbase:结构化数据分布式存储系统
    • 两者之间的差别:关系型数据是以二维表的形式储存数据,二维表即excel中的表格

  • 数据库核心要素

    • 数据库:数据表的集合
    • 数据表:数据的集合
    • 字段:数据的列名
    • 数据行:一行数据
  • SQL介绍

    • SQL是一种结构化查询语言,可以通过SQL语言可以对数据库进行操作
    • sql语言分类
      • DQL:数据查询语言
      • DML:数据操作语言
      • TPL:事务处理语言
      • DCL:数据控制语言
      • DDL:数据定义语言
      • CCL:指针控制语言

我们主要学习MySQL数据库,其他的数据库了解即可,主要用到的sql语言有DQL、DML、DDL三种

二、MySQL介绍

  • 介绍

    • 由瑞典MySQL AB格式公司开发,后被Sun公司收购,接着Sun公司被Oreale公司收购,即MySQL现在数据Oreale的产品
  • 特点(主要)

    • 免费、开源
    • 可移植性好
    • 支持多操作系统
    • 支持多种编程语言
    • .....
  • 组成

    • MySQL服务器:存储数据并解析编译后的sql语句,将执行结果返回到客户端
    • MySQL客户端:下发用户要执行的sql语句,并显示服务器返回的执行结果
  • 连接mysql数据库方式

    • 命令行模式

      • 在控制台输入mysql -h ip地址 -p 端口号 -u 用户名 -p 回车后,输入密码即可进入到数据库
    • 工具连接

      • DBerver
      • Navicat

三、数据库基础理论知识

  • 数据类型以及约束

    • 数据类型

      1. int:整数类型,有符号整数范围(-2147483648~2147483647)

        1. unsigned:无符号整数范围(0~4294967295)
      2. varchar:字符串类型,范围在 0~65533

      3. decimal(5,2):小数,5为总位数,2为有两位小数

      4. datetime:时间

    • 约束

      • 默认值:即不填数据时,内容默认填写的值
      • 非空:该字段不能不输入数据
      • 主键:提高查询的效率
        • 唯一性:主键的值不能重复
        • 非空性:主键值不能为空
        • 单一性:一张表只能由一个主键
        • 不变性:主键的值不能随意更改
      • 外键:维护两个表之间的依赖关系
      • 唯一性:定义字段的值不允许重复

四、SQL语句

  • 创建数据库语句

    create database 数据库名 charset=utf8;
    
  • 删除数据库

    drop database 数据库名;
    #在不知数据库是否存在的情况可以使用is exists
    drop database is exists 数据库名;
    
  • 创建数据表

    create table 表名(
    	字段1 类型,
        字段2 类型,
        字段3 类型,....
    );
    
    
  • 删除数据表

    • drop 删除数据表

      drop from 表名;
      
    • truncate 清空数据表结构

      truncate table 表名;
      #自增长字段会初始化
      
    • delete 删除数据但不删除表结构

      delete from 表名称 where 字段名=值;
      #该命令不会清空表结构,比如id是主键自增长,删除其中一条数据时,id不会发生变化
      #若是没有条件则会直接删除表中所有的数据,但不会删除表结构,id自增长不会初始化
      
  • 插入数据

    • insert into

      #单行全字段插入  注意:存在主键自增长则赋值0 or null,值和字段位置要一一对应
      insert into 表名 values(值1,值2,值3,...);
      #单行选择字段插入
      insert into 表名(字段1,字段2) values(值1,值2);
      
      #多行全字段插入  一个括号代表一个数据
      insert into 表名 values(值1,值2,值3,...),(值1,值2,值3,...),(值1,值2,值3,...),....;
      #多行选择字段插入
      insert into 表名(字段1,字段2) values(值1,值2),(值1,值2),(值1,值2),....;
      
  • 修改数据/更新数据

    • update

      
      update 表名 set 字段1=值1,字段2=值2,字段3=值3,... where 字段=值;
      
  • 查询数据

    • 基本语法

      select * from tableName;#查询所有信息
      select 字段 from tableName;#查询指定字段
      
      
    • 条件查询

      • 比较运算符

        #查询学生表中学号为1的学生信息
        select * from student where studentNo = 1;
        
        #查询学生表中成绩大于等于60的学生信息
        select * from student where score >= 60;
        
        #查询学生表中成绩小于等于60的学生信息
        select * from student where studentNo <= 60;
        
        #查询学生表中成绩不等于60的学生信息
        select * from student where studentNo != 60;
        
      • 逻辑运算符

        • and

          #查询学生表中所有高于60分的男生信息
          select * from student where score > 60 and sex = '男';
          
        • or

          #查询学生表中所有高于60分或班级为1班的学生信息
          select * from student where score > 60 or class = '1班';
          
        • not

          #查询学生表中班级除1班以外班级的学生信息
          select * from student where not class = '1班';
          
      • 去重

        • distinct

          #查询学生表中,学生都来自于哪些省份
          select distinct(hometown) from students;
          
      • 模糊查询

        • like

          #查询学生表中姓孙的学生
          select * from students where name like '孙%';
          #查询学生表中名字最后一个字为白的学生
          select * from students where name like '%白';
          #查询学生表中姓孙名字两个字的学生
          select * from students where name like '孙_';
          #查询学生表中姓名两个字的学生
          select * from students where name like '__';
          #查询学生表中名字两个字最后一个字为白的学生
          select * from students where name like '_白';
          
      • 范围查询

        • in

          #查找学生表中姓名为张三、李四、王五的同学的信息
          select * from students where name in ('张三','李四','王五');
          
        • between ... and ...

          #查找学生表中年龄在18到20的所有学生信息
          select * from students where age between 18 and 20;
          
      • 空判断

        • is null

          #查找身份证号为空的学生信息
          select * from students where card is null;
          
        • is not null

          #查找身份证号不为空的学生信息
          select * from students where card is not null;
          
    • 排序

      • order by

        #默认升序
        select * from students order by 字段;
        select * from students order by 字段 asc;
        
        #降序
        select * from students order by 字段 desc;
        
    • 聚合函数

      #最大值
      select max(字段) from students;
      
      #最小值
      select min(字段) from students;
      
      #平均值
      select avg(字段) from students;
      
      #统计
      select count(字段) from students;
      
      #求和
      select sum(字段) from students;
      
      • 一个sql语句可以使用多个聚合函数,使用 , 分割即可
      • where 条件中不能使用聚合函数
    • 分组查询

      • group by

        select 分组字段,... from studnets group by 字段;
        
      • having

        select 分组字段,... from studnets group by 字段 having 字段=值;
        
        • having可以使用聚合函数
    • 分页

      • limit

        select * from students limit 开始的行数,显示的条数;
        
    • 联表查询

      • 内连接

        • 内连接是取两个表共有的数据

          select * from 表1 s1 inner join 表2 s2 on s1.关联字段=s2.关联字段; 
          
      • 外连接

        • 左连接:共有数据+左边特有数据+右边以空(null)填充

          select * from 表1 s1 left join 表2 s2 on s1.关联字段=s2.关联字段;
          
        • 右链接:共有数据+右边特有数据+左边以空(null)填充

          select * from 表1 s1 right join 表2 on s1.关联字段=s2.关联字段;
          
    • 自关联

      • 使用场景:当表存在等级关系时,查询其中数据需要使用自关联
        • 比如:省,市,县
        • 比如:总经理,部门主管,组长,组员
      • 自关联思路:
        1. 把一张表当成多张表来使用
          • 说明:将表定义为不同的别名
        2. A表的字段与B表的字段进行关联
          • 强调:一定是表中不同的字段
      select * from 表1 s1 inner join 表1 s2 on s1.关联字段=s2.关联但不相同字段;
      
    • 子查询

      • 前置介绍:涉及到了表之间的关联,可以使用表连接,也可以使用子查询

        • 强调:如果能用表连接,绝对不用子查询。因为子查询SQL语句执行效率很低
      • 子查询,充当条件分类

        • 标量子查询:一行一列
          • 最简单的子查询
        • 列子查询:一列多行
        • 行子查询:一行,多列
        #标量子查询:即返回一个数据
        select * from students where age = (select age from students where id = 1); 
        
        #列子查询:返回多个相同字段的值
        select * from students where age in (select age from students); 
        
        #行子查询:返回一行多个字段值
        select * from students where (age,class) = (select age,class from students where id = 1); 
        
      • 充当数据源

        select * from (select * from students);
        

www.htsjk.Com true http://www.htsjk.com/Mysql/47117.html NewsArticle 数据库入门,因为子查询SQL语句 一、数据库介绍 介绍 数据库就是一个存储数据的仓库,能够长期存储数据且能够对数据进行增删改查 常见的数据库 关系型数据库: mysql:开源免费,...
评论暂时关闭