zoukankan      html  css  js  c++  java
  • 第69节:Java中数据库的多表操作

    标题图

    第69节:Java中数据库的多表操作

    前言

    学习数据库的多表操作,去电商行业做项目吧!!!

    达叔,理工男,简书作者&全栈工程师,感性理性兼备的写作者,个人独立开发者,我相信你也可以!阅读他的文章,会上瘾!,帮你成为更好的自己。

    感谢!承蒙关照!

    字数

    数据库的创建:
    create database 数据库名 character set 字符集 collate 校对规则
    数据库的删除:
    drop database 数据库名
    修改
    alter database 数据库 character set 字符集
    查询
    show databases
    show create database 数据库的名字
    select database(); 当前数据库
    切换数据库
    use数据库名字

    表操作:

    create table 表名(
     列名 列的类型 列的约束,
     列名 列的类型 列的约束
    )
    

    列的类型:
    char/varchar

    列的约束
    primary key 主键的约束
    unique 唯一约素
    not null 非空约素

    自动增加 auto_increment

    删除: drop table表名
    修改: alter table表名(add, modify, change, drop)

    修改表名:
    rename table 旧表名 to 新表名
    alter table 表名 character set 字符集

    查询:
    show tables;查询所有表
    show create table 表名; 查询表的定义,表的创建语句
    desc用于查询表的结构

    表的插入:
    insert into 表名(列名,列名) values (值1,值2);
    表的删除:
    delete from 表名 [where 条件]
    表的修改:
    update 表名 set 列名='值', 列名='值' [where 条件];
    表的查询:
    select [distinct] * [列名1,列名2] from 表名 [where 条件];
    where的使用

    >, >=, <, <=, !=, <>
    is null is not null
    in
    between...and
    and or not
    
    like:
    _:代表单个字符
    %:代表多个字符
    
    分组: group by
    条件过滤: having
    
    聚合函数:
    sum();
    avg();
    count();
    max();
    min();
    

    排序:
    order by

    asc: 升序
    desc: 降序
    

    多表查询

    查看数据库
    show databases;
    创建数据库
    create database dashu;
    使用数据库
    use dashu;

    多表之间的约束

    多表间的关系如何维护:
    添加外键约束:
    forerign key

    alter table product add foreign key(cno) references category(cid);

    product添加到category里.con等于cid之间有关系.

    category
    product
    

    如果建立了表的外键约束,比如是产品,和产品分类,如果要删除产品分类,就要把产品分类对应产品的编码删除掉.

    一个项目建立一个数据库

    多表之间的关系:
    一对多:商品和分类
    分类是1,商品是多.

    添加外键指向主键;
    口诀:在多的一方添加一个外键,指向一的一方的主键.

    多对多: 老师和学生,学生选课
    多对多的关系很难表示?
    口诀:多对多建表,多建一张中间表,中间表至少要有两个外建,这两个外键分别指向原来的那张表.

    就是把多对多的关系弄成一对多的关系.

    中间表的关系就是一对多的关系,一个学生学课表,一对多.

    有两个外键以上,课表的编号对应学生.

    建表的原则是,多对多的关系,中间要建立一个中间表连接, 中间表的字段就是外键, 至少要有两个以上, 建表嘛, 至少两个字符, 将多对多的关系拆分成一对多的关系,然后分别指向原来的那两张表.

    一对一的关系: 公民和身份证

    在一对一里添加一个外键指向id主键,添加唯一约素,外键,m->1.一对一是当作一对多处理,在任意一张表中添加一个外键,并且指向另一张表.将两张表合并为一张,可以将两张表的主键建立起来,连接,并让两张表中的主键相等.

    拆表:

    用于将个人常用的信息和不常用的信息拆分出来,使得查询效率更好,提供更好用户的体验.在表关系里,多对多用得比较多,一对多,然后才一对一.

    用户表,订单表, 商品表,分类表

    用户表:
    用户ID
    用户名
    密码
    手机号

    订单表
    订单编号
    订单总价
    创建时间
    收货地址

    商品表
    商品编号
    商品名称
    商品价格

    商品分类
    分类ID
    分类名称
    分类描述

    用户id,地址

    分析

    用户表到订单表->1对多

    订单表创建一个外键,然后指向用户id.

    商品表->商品分类
    商品表中加外键->指向商品分类的id

    订单表 和 商品表 -> 中间表
    一个订单可以买多个商品
    一个商品可以被多个用户去买,可以被多个订单所选择
    m对n:

    中间表 -> 外键约束多 -> 指向订单表 和 商品表为一.

    订单表 商品表

    中间表-> 订单编号 商品编号

    // 用户表
    create table user (
     uid int primary key auto_increment,
     username varchar(31),
     password varchar(31),
     phone varchar(11)
    );
    
    insert into user(1,'zhangsan','12345','13344442333');
    
    // 订单表(订单编号,订单时间,总价,地址,外键
    create table orders(
     oid int primary key auto_increment,
     sum int not null,
     otime timestamp,
     address varchar(100),
     uno int,
     foreign key(uno) references user(uid)
    );
    
    insert into orders values(1,200, null, '广州',1);
    insert into orders values(2,300, null, '深圳',2);
    
    // 商品表
    // 商品id,商品名称,商品价格,外键
    create table product(
     pid int primary key auto_increment,
     pname varchar(10),
     price double,
     cno int,
     foreign key(cno) references category(cid)
    );
    
    // 添加商品
    insert into product values(null, 'xiaomi', 233, 1);
    
    
    // 商品分类表
    // 分类id,分类名称,分类描述
    create table category(
     cid int primary key auto_increment,
     cname varchar(15),
     cdesc varchar(100)
    );
    
    insert into category values(null, '手机', '电子');
    

    中间表,订单项

    // 中间表:订单id,商品id,商品数量,订单项总价
    create table orderitem(
     ono int, // -> oid
     pno int, // -> pid
    
     foreign key (ono) references orders(oid),
     foreign key (pno) references product(pid)
     count int,
      subsum double
    )
    
    select * from orders;
    select * from product;
    
    // 订单 商品 商品数量 商品总价
    insert into orderitem values(1,7,100,100);
    insert into orderitem values(1,8,100,100);
    
    // 添加商品
    insert into orderitem values(2,5,1,100);
    insert into orderitem values(2,3,100,100);
    

    小结

    // 多表之间关系的维护是由外键约束
    foreign key
    添加一个外键:
    alter table product add foreign key (con) references category(cid);
    foreign key (cno) references category (cid)
    
    建表原则:
    一对多:
    口诀: 在多的一方添加外键并指向另一张表
    
    多对多:
    口诀: 将多对多的情况变成一对多的关系中间有一张中间表
    
    一对一:多用于拆表
    将两张表合并为一张表,将两张表的主键建立起关系,原理将一对一变成一对多去处理
    

    主键和唯一约束

    唯一约素: 列里面的内容是唯一的,不能有重复的情况,但是可以为空.唯一约素是不能作为其它表的外键的,可以有多个唯一约素.

    主键约束是不能为空的,唯一.外键都是指向另外一张表的主键,一张表只能有一个主键.

    一对多:
    在多的一方添加一个外键指向另一方
    多对多:
    拆成一对多,中间创建一个表,至少有两个外键,指向原来的表
    一对一:
    合并一张表,将主键建立关系

    数据库软件
    SQLyog和下载mysql软件.
    架构设计器

    category
    orderitem
    orders
    product
    user
    
    // user 1->m
    uid
    username
    password
    phone
    
    // orders
    oid
    sum
    otime
    address
    uno
    
    // orderitem
    ono
    pno
    ocount
    subsum
    
    // product
    pid
    pname
    price
    cno
    
    //category
    cid
    cname
    cdesc
    

    多表查询

    交叉连接查询
    笛卡尔积:查出来的是两张表的乘积
    select * from product,category cno = cid;
    select * from product as p,category as c where p.cno = c.cid;
    select * from product  p,category  c where p.cno = c.cid;
    
    内连接查询
    隐式内连接:
    select * from product p, category c where p.cno = c.cid;
    显示内连接
    select * from product p inner join category c on p.con = c.cid;
    
    区别:
    1.在查询出结构的基础上去做的where条件过滤;
    2.是带条件去查询结果的,执行效率高
    
    // 添加数据
    insert into product values(null,'dashujava',10,null);
    
    左外连接
    select * from product p left outer join category c on p.cno = c.cid;
    左外连接会将左表中的所有数据都查出来,如果右表没有会用null代替.
    
    // 右category 分类
    insert into category values(100, '电脑', '详情');
    
    右外连接
    select * from product p right outer join category c on p.cno = c.cid;
    右外连接是只要右边有就显示
    右外连接会将右表的所有数据都查出来,如果左边没有会用null替代.
    

    分页查询

    limit

    select * from product limit 0,10;
    select * from product limit 0,3;
    select * from product limit 3,3;

    startindex = (index-1)*3

    计算启始索引

    左外连接

    select * from product
    // 查询分类名为手机
    select cid from category where cname='手机';
    select * from product where cno=1;
    子查询
    select * from product where cno=(select cid from category where cname='手机');
    
    // 查询信息
    左连接:
    select * from product  p left outer join category c on p.cno = c.cid;
    
    // 子查询 商品名称, 商品分类名称
    select pname, cno from product;
    select pname, (select cname c from category where p.cno = c.cid) from product p;
    
    select max(sal) from emp;
    select min(sal) from emp;
    
    select * from emp where sal = (select max(sal) from emp);
    select * from emp where sal = (select min(sal) from emp);
    

    如果看了觉得不错

    点赞!转发!

    达叔小生:往后余生,唯独有你
    You and me, we are family !
    90后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通
    简书博客: 达叔小生
    https://www.jianshu.com/u/c785ece603d1

    结语

    • 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
    • 小礼物走一走 or 点赞
  • 相关阅读:
    js正则表达式中的问号使用技巧总结
    380. Insert Delete GetRandom O(1)
    34. Find First and Last Position of Element in Sorted Array
    162. Find Peak Element
    220. Contains Duplicate III
    269. Alien Dictionary
    18. 4Sum
    15. 3Sum
    224. Basic Calculator
    227. Basic Calculator II
  • 原文地址:https://www.cnblogs.com/dashucoding/p/10284710.html
Copyright © 2011-2022 走看看