MySql multi-table operation

MySql multi table

Today's task:

  • Extract the entities of the mall for table building analysis

teaching objectives

  • Master the relationship between multiple tables

  • Master multi-table queries

  • Understand the use of the MYSQL visual interface

1. Introduction to visualization software

Second, the relationship between multiple tables

1. Why dismantle the watch

1.1 Preparation of the table

​ Create a classification table (category id, category name. Note: category id is the primary key and grows automatically)

​ Create a star table (star id, star name, star worth, star age, star gender, star category. Note: star id is the primary key and grows automatically)

2. Referential integrity

There is a relationship between tables and tables, but who needs to maintain and constrain this relationship?

2.1 Foreign key constraints

​ Foreign key role: to ensure referential integrity, that is to say the accuracy of the data

​ Foreign keys need to pay attention to:

​ 1. The foreign key must point to the primary key! ! ! Primary key has foreign key

​ 2. The type of the foreign key column must be the same as the type of the referenced primary key;

  • Add foreign key statement: alter table table add [CONSTRAINT foreign key name] foreign key (field) references table (field);

  • Statement to delete a foreign key: alter table table name drop foreign key foreign key constraint name

  • Add a foreign key to the star table

    alter table star add [constraint fk_star] foreign key(type) references category(cid);

  • Delete the foreign key of the star table: alter table star drop foreign key fk_star;

2.2 Practice creating a product table and a classification table corresponding to the product

  CREATE TABLE product(
      pid int primary key auto_increment,
      pname varchar(50) not null,
      pnum int,
      ptype int);
  create table category(cid int primary key auto_increment,cname varchar(30));添加外键方式:ALTER TABLE product ADD  FOREIGN KEY(ptype) REFERENCES category(cid);    
  
      
      
  
  
      

Notice:

  • Only if there is a primary key can there be a foreign key, the foreign key of the foreign key table must point to the primary key!!! And the type of the foreign key must be the same as the pointed primary key

3. Multi-table relationship

3.1 One-to-many (master)

  • Create a field on the many side as a foreign key, pointing to the primary key on the one side

3.2 Many-to-many (Mastering)

  • Create a new third-party table with at least two fields, both as foreign keys, pointing to their respective primary keys

3.3 One-on-one (understanding)

  • First treat it as one-to-many, and add a unique constraint to the foreign key field.

3.4. Exercise

  • Extract the relationship between the mall entity analysis table and simulate the creation of the table

Three, multi-table query (emphasis in the focus)

1. Cross query (understand)

​ Several tables are joined together without conditions

  
  select a.*,b.* from a,b;
  或者 select *from a,b;

Note:

  • Cross query is actually an error. The data has a lot of useless data, called Cartesian product.

  • Assuming set A={a,b}, set B={0,1,2}, then the Cartesian product of the two sets is {(a,0),(a,1),(a,2),( b,0),(b,1),(b,2)}. Can be extended to the case of multiple collections.

2. Inner join query (emphasis)

  • 交叉查询产生这样的结果并不是我们想要的,那么怎么去除错误的,不想要的记录呢,当然是通过条件过滤。通常要查询的多个表之间都存在关联关系,那么就通过关联关系去除笛卡尔积。

2.1 隐式内连接

  
  select a.*,b.* from a,b where 连接条件
  或者:
  select * from a, b where 连接条件

2.2 显示内连接

  
  select a.*,b.* from a [inner] join b on 连接条件
  或者:
  select * from a [inner] join b on 连接条件 where 其它条件

注:

  • 使用主外键关系做为条件来去除无用信息.抓住主外键的关系,用主外键作为连接条件 b表里面的外键=a表里面主键

  • 显示里面的,on只能用主外键关联作为条件,如果还有其它条件,后面加where

练习:查询所有类别下的商品,并且商品价格大于4000,如果该类别下没有商品则不显示

以左边的表为主表,如果满足条件则显示,不满足则不显示。

4.外连接(重点)

4.1左外连接

​ 以join左边的表为主表,展示主表的所有数据,根据条件查询连接右边表的数据,若满足条件则展示,若不满足则以null显示

  
  select a.*,b.* from a left [outer] join b on 条件
  或者:
  select *from a表 left [outer] join b表 on 条件

练习:查询所有类别下的商品信息.

4.2右外连接(了解)

​ 以join右边的表为主表,展示它的所有数据,根据条件查询join左边表的数据,若满足则展示,若不满足则以null显示

  
  select a.*,b.* from a right [outer] join b on 条件
  或者:
  select *from 表a right [outer] join 表b on 条件

练习:查询所有商品所对应的类别信息

5.子查询(重点!!!)

5.1什么是子查询

  • 一个select语句中包含另一个完整的select语句。

  • 子查询就是嵌套查询,即SELECT中包含SELECT,如果一条语句中存在两个,或两个以上SELECT,那么就是子查询语句了。

5.2步骤解析

eg:查询身价大于宝宝的的明星信息

  • 第一步:查询出宝宝的身价

    • select price from star where name="宝宝"

  • 第二步:以宝宝的身价去查询身价大于这个数的明星信息

    • select * from star where price>5000

  • 两步合并成一步

    • select * from star where price>(select price from star where name="宝宝")

5.4练习

  • 查询和霆锋是同一类别的明星信息

    • 第一步:查询霆锋的类别

      • select type from star where name="霆锋"

    • 第二步:以霆锋的类别,去查询类别等于这个的所有明星信息

      • select * from star where type=1

    • 两步合并成一步,如果查询到的结果不要包含霆锋

        
        SELECT * FROM star WHERE TYPE=(SELECT TYPE FROM star WHERE NAME="霆锋") AND NAME != "霆锋"
  • 查询类别是宝剑队的所有明星信息

  • SELECT * FROM star WHERE TYPE=(SELECT cid FROM category WHERE cname="宝剑队")

6.联合查询(了解,几乎用不到)

6.1作用

  • 合并结果集就是把两个select语句的查询结果合并到一起

6.2语法

  • UNION:用于合并两个或多个 SELECT 语句的结果集,并消去表中任何重复行。UNION 内部的 SELECT 语句必须拥有相同数量的列,列也必须拥有相似的数据类型。

  • UNION ALL:不去除重复记录,其它的和union一样

6.3练习

  
  create table a(
      name varchar(10),
      score int
  );
  
  create table b(
      name varchar(10),
      score int,
      num int
  );
  
  insert into a values('a',10),('b',20),('c',30);
  insert into b values('a',10,30),('b',20,40),('d',40,50);

7 分页查询(掌握,重点),分页更在排序后面!

(客户端向服务器端传页数)

  • limit a,b; (a不是代表页数!!!)

  • a:从哪个下标开始查(从0开始计数的)

  • b:查询多少个数据,一页显示数据的数量 固定值 前端或者安卓,ios

    目标:将客户端传过来的页数curPage转换成a(开始的下标)

    a = (curPage-1)*b;// curPage当前页数 ,b表示每页显示的数据条数

一页显示3条数据: 第1页: a = 0; b = 3;

​ 第2页: a = 3; b = 3;

​ 第3页:a = 6; b = 3;

​ 第四页:a=9,b=3.

​ 练习: 查询商品表前两条记录

​ 查询商品表第三条和第四条记录

​ 查询商品表中最低价格所对应的商品名

场景: 百度分页,各种网站上信息显示分页

​ 安卓、IOS里面下拉刷新,上拉加载更多

客户端发送请求给服务器,请求中包含要请求的页数

四,数据库的备份和恢复

作业

1.今日语句。(至少两遍)

2.将商城案例中的user、订单、商品、分类表建立好,并用外键进行关联。

3.往商城中的四张表中插入数据,并进行连接查询(两张表之间的连接查询)。

4.资料中的多表练习

5.预习JDBC的东西(重要)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324809871&siteId=291194637