• 肖sir__面试就业课___数据库


    数据库:
    ddl 数据库定义语言(对表结构的操作)
    dml 数据库操作语言(增删改查对表数据)

    一、概括
    1、单表:
    (1)增 insert into values
    (2)删 delete (delete < truncat (3)改 update set
    (4)查 select
    语句: like % 模糊查询、limit 限制行数 、 order by desc 或asc 排序、group by having 分组、
    聚合函数:sum 、count 、max、min、avg 、distinct、

    2、多表:
    总结:
    一、多表:
    普通内连接:select * from 表1 inner join 表2 on 表 1.关联字段=表2.关联字段 (关联的数据显示,不关联的不显示)
    隐藏内连接:select * from 表1,表2 where 表 1.关联字段=表2.关联字段 (关联的数据显示,不关联的不显示)
    左连接:select * from 表1 left join 表2 on 表 1.关联字段=表2.关联字段 (左表全部显示,右表关联的数据关联,不关联的数据以null显示)
    右连接 :select * from 表1 right join 表2 on 表 1.关联字段=表2.关联字( 右表全部显示,左表关联的数据关联,不关联的数据以null显示)
    左独有数据:select * from 表1 left join 表2 on 表 1.关联字段=表2.关联字段 where 表2中的字段 is null
    右独有数据:select * from 表1 right join 表2 on 表 1.关联字段=表2.关联字段 where 表1 中的字段 isnull
    全外连接:union
    (1)左独有+右独有+内连接
    (2)左连接+右独有
    (3)右连接+左独有
    二、
    三表内连接合并:
    方法一:三表的隐藏内连接
    格式:
    SELECT * FROM 表1 as 别名, 表2 as 别名, 表3 sc as 别名 where 表1.关联字段1=表3.关联字段3 and 表2.关联字段2= 表3.关联字段3
    案例:SELECT * FROM student as a,course as b,sc as c where a.stu_no=c.stu_no and b.c_no=c.c_no
    方法二:三表普通内连接
    SELECT * FROM 表1 as 别名 inner join 表3 as 别名 on 表1.关联字段1=表3.关联字段3 inner join 表2 as 别名 on 表2.关联字段2= 表3.关联字段3
    案例SELECT * FROM student as a inner join sc as c on a.stu_no=c.stu_no INNER JOIN course as b on b.c_no=c.c_no
    方法三:三表左连接
    格式:
    SELECT * FROM 表1 as 别名 left join 表3 as 别名 on 表1.关联字段1=表3.关联字段3 left join 表2 as 别名 on 表2.关联字段2= 表3.关联字段3
    案例:SELECT * FROM student as a left join sc as c on a.stu_no=c.stu_no LEFT JOIN course as b on b.c_no=c.c_no
    方法四:三表右连接
    格式:SELECT * FROM 表1 as 别名 right join 表3 as 别名 on 表1.关联字段1=表3.关联字段3 right join 表3 as 别名 on 表2.关联字段2= 表3.关联字段3
    案例:SELECT * FROM student as a right join sc as c on a.stu_no=c.stu_no right JOIN course as b on b.c_no=c.c_no
    方法五:先合两个表在和合一个表的方法
    SELECT * FROM (SELECT c_no FROM student as a,sc as c where a.stu_no=c.stu_no ) s INNER JOIN course as b where s.c_no=b.c_no
    3、视图
    (1)优点:1、查询复杂的语句更方便 2、更安圈全、保密字段
    (2)创建视图:create view 视图名 as (sql语句)

    4、索引
    (1)索引优点:1、查询速度(group by ,order by) 2、保证数据的唯一性、3、实现表与表的参照性
    (2)主键索引(parmary key)、唯一索引(unique)、普通索引 (index)
    (3)建立索引的方式:1、建表 时创建 2、 alter table 添加字段或修改字段
    5、外键
    (1)让一张表记录的数据不要太过于冗余,
    (2)优点:保持数据的一致性
    (3)foreign key 外键
    一、建表时添加外键
    create table 表名 (字段 字符类型(字符宽度) primary key ,(字段2 字段类型2(字符宽度3),constraint 外键名 foreign key(子表字段) references 父表(父表的字段))engine=innodb default charset=utf8;
    二、建表后添加外键
    格式:alter table 表名 add constraint 外键名 foreign key(子表字段) references 父表( 父表字段)
    6、存储
    (1)造数据
    (2)存储格式:
    delimiter // 分隔符/定格符
    create procedure 存储过程名( in/out/inout)参数
    begin —开始
    sql语句 ; —执行的语句
    end —结束
    // 分隔符

    call 存储过程名称() 调用一个存储过程
    (3)案例:造数

    delimiter //
    drop table if exists money;
    drop procedure if exists pro4;
    create procedure pro4(in x int)
    begin
    declare i int default 0;# 变量名称i int 数据类型 默认为0
    drop table if exists money;
    create table money(id int primary key auto_increment,money int(10));
    while(i insert into money(money) values(100);
    set i=i+1;
    end while;
    select * from money;
    end
    //

    call pro4(20)

  • 相关阅读:
    eclipse新建maven项目:'Building' has encountered a problem. Errors occurred during the build.
    从头开始实现YOLOV3
    go语言---锁
    c++入门99题61-70
    二叉树之路径
    JAVA基础(三十九)——常用类之Arrays类
    SpringBoot项目--电脑商城【商品热销排名】
    GRE MGRE原理与配置
    dedecms织梦复制模型报错系统找不到你所指定的表
    数论学习笔记 - 同余及其拓展知识
  • 原文地址:https://blog.csdn.net/m0_47403059/article/details/126097985