• mysql 第二课 DML操作


    DML 数据操纵语句:INSERT UPDATE DELETE SELECT 主要用来对数据库的数据进行一些操作;

    DCL 数据定义语句:CREATE ALTER DROP  主要是用在定义或改变表的结构,数据类型,表之间的链接和约束等初始化工作上;

    DCL 数据控制语句:GRANT,DENY,REVOKE 主要是用来设置或更改数据库用户或角色权限的语句.

    先创建两个表 一个学生表 一个成绩表

     1 create table student(
     2 code int primary key auto_increment,
     3 sname varchar(20)
     4 )engine=InnoDB default charset=utf8 auto_increment=1;
     5 
     6 
     7 insert into student (code,sname) values(1,"wang");
     8 insert into student (code,sname) values(2,"lin");
     9 
    10 create table score(
    11 studentid int,
    12 score int,
    13 courseid varchar(20)
    14 )engine=INNODB DEFAULT CHARSET=UTF8;
    15 
    16 insert into score (studentid,courseid,score) values (1,'java',50);
    17 insert into score (studentid,courseid,score) values (1,'DB',60);
    18 insert into score (studentid,courseid,score) values (2,'java',70);
    19 insert into score (studentid,courseid,score) values (2,'DB',80);
    View Code

    函数的使用:

    1.取平均值:

    SELECT studentid,avg(score) FROM score GROUP BY studentid;  #取平均值

    2.求和,统计

    select studentid,AVG(score) as average,SUM(score) as sum,COUNT(1) from score GROUP BY studentid;#求和

    3.替换元素

    update score set courseid=REPLACE(courseid,'a','A');#替换

    4.显示某几行

    select * from score LIMIT 2,2;#limit (从第2行,查询2行) 默认第一行是0

    5.内连接

    select student.sname,score.courseid,score.score from student,score where student.code=score.studentid; #内连接

    6.外连接

    select sname,courseid,score from student t LEFT JOIN score s ON t.code=s.studentid; #外连接

    内外连接还是有点懵。但是大意就是内连接: 只连接匹配的行
    左外连接: 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行
    右外连接: 包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边表中全部匹配的行

    其实就是用其中一个表去匹配另一个表中的值来达成构成一个表来显示同一个事物的所有信息。

  • 相关阅读:
    在spring boot中三分钟上手apache顶级分布式链路追踪系统skywalking
    Spring Boot Admin 2.1.0
    Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)
    Spring Cloud Sleuth + Zipkin 链路监控
    guava布隆过滤器
    红包算法
    java国际化之时区问题处理
    SpringCloud2.0 Hystrix Feign 基于Feign实现断路器
    SpringBoot 自定义线程池,多线程
    基于Redisson+SpringBoot的Redission分布式锁
  • 原文地址:https://www.cnblogs.com/LiuOOP/p/10983429.html
Copyright © 2020-2023  润新知