MySQL Crash Course #07# Chapter 15. 关系数据库. INNER JOIN. VS. nested subquery

时间:2022-03-12 03:26:20

索引

我发现MySQL 的官方文档里是有教程的。

SQL Tutorial - W3Schools

The SQL Tutorial for Data Analysis | SQL Tutorial - Mode Analytics

Understanding Relational Tables

The key here is that having multiple occurrences of the same data is never a good thing, and that principle is the basis for relational database design. Relational tables are designed so information is split into multiple tables, one for each data type. The tables are related to each other through common values (and thus the relational in relational design).

书上举了一个产品表和供应商表的例子,一个供应商可以对应很多的产品,不把供应商的信息放在每一行产品的理由有如下几点:

  1. 多个产品的供应商是一致的,重复相同的信息很浪费空间
  2. 如果供应商的信息改变,你不得不更新每一条该供应商相关的产品记录
  3. 很大概率出现数据不一致的情况

所以产品和供应商应该分两张表存,两张表都应该有 primary key , 供应商表专门存供应商的信息,而产品表专门存产品的信息,每一个产品记录除了包含一个供应商的 id 属性不应该包含任何供应商的其他信息,这个属性对应的字段叫做  foreign key (和供应商表的 primary key 相关系)这么做有如下几个好处:

  1. 没有重复数据,节省时间和空间
  2. 需要修改供应商信息时只需要修改一处就好了
  3. 因为数据没有被重复,很好的保证了数据一致性

Why Use Joins?

As just explained, breaking data into multiple tables enables more efficient storage(高效存储), easier manipulation(易于操作), and greater scalability(极高的可扩展性). But these benefits come with a price.

If data is stored in multiple tables, how can you retrieve that data with a single SELECT statement?

The answer is to use a join.

It is important to understand that a join is not a physical entity in other words, it does not exist in the actual database tables. A join is created by MySQL as needed, and it persists for the duration of the query execution.

- maintaining referential integrity 是说 MySQL 只允许合法的数据(foreign key 的值在主表中存在的数据)插入到关系表中。

Creating a Join

SELECT vend_name, prod_name, prod_price
FROM vendors, products
ORDER BY vend_name, prod_name;
SELECT vend_name, prod_name, prod_price
FROM vendors INNER JOIN products
ON vendors.vend_id = products.vend_id;
  1. 虽然默认就是 inner join (看这个),但是最好还是用 INNER JOIN ON 语句,这样你就再也不会忘记 JOIN 的类型了。
  2. 无条件的inner join是笛卡儿积,有条件的才是取交集(看这个
  3. JOIN 是在运行时临时做的,关联的表越多越消耗资源,所以不必要就不要乱联表

It Pays to Experiment As you can see, there is often more than one way to perform any given SQL operation. And there is rarely a definitive right or wrong way. Performance can be affected by the type of operation, the amount of data in the tables, whether indexes and keys are present, and a whole slew of other criteria. Therefore, it is often worth experimenting with different selection mechanisms to find the one that works best for you.

联表快还是子查询快取决于具体情况,所以在必要时候可以进行测试。。 。问题 在于 。。 如何测试?? -- > 待更新

MySQL Crash Course #07# Chapter 15. 关系数据库. INNER JOIN. VS. nested subquery的更多相关文章

  1. MySQL Crash Course #08# Chapter 16. Using Different Join Types

    记文档还是相当重要的! 索引 假名的三个用途 自交(Self Joins) 自然交(Natural Joins) Outer Joins Using Table Aliases Using alias ...

  2. MySQL Crash Course #11# Chapter 20. Updating and Deleting Data

    INDEX Updating Data The IGNORE Keyword Deleting Data Faster Deletes Guidelines for Updating and Dele ...

  3. MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询

    索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...

  4. MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables

    之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...

  5. MySQL Crash Course #10# Chapter 19. Inserting Data

    INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...

  6. MySQL Crash Course #04# Chapter 7. 8 AND. OR. IN. NOT. LIKE

    索引 AND. OR 运算顺序 IN Operator VS. OR NOT 在 MySQL 中的表现 LIKE 之注意事项 运用通配符的技巧 Understanding Order of Evalu ...

  7. MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance

    终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...

  8. MySQL Crash Course #20# Chapter 28. Managing Security

    限制用户的操作权限并不是怕有人恶意搞破坏,而是为了减少失误操作的可能性. 详细文档:https://dev.mysql.com/doc/refman/8.0/en/user-account-manag ...

  9. MySQL Crash Course #18# Chapter 26. Managing Transaction Processing

    InnoDB 支持 transaction ,MyISAM 不支持. 索引: Changing the Default Commit Behavior SAVEPOINT 与 ROLLBACK TO ...

随机推荐

  1. Redis 5种数据结构使用及注意事项

    1优缺点 非常非常的快,有测评说比Memcached还快(当大家都是单CPU的时候),而且是无短板的快,读写都一般的快,所有API都差不多快,也没有MySQL Cluster.MongoDB那样更新同 ...

  2. 如何在XAMPP中设置多个网站

    xampp 是一个非常方便的本地 apache + php + mysql 的调试环境,在本地安装测试 WordPress 等各种博客.论坛程序非常方便.今天我们来给大家介绍一下,如何使用 XAMPP ...

  3. google login page

    </pre><pre name="code" class="html"><div class="container&qu ...

  4. 小细节--Extjs中,renderTo 和applyTo的区别

    说到web前端框架,extjs绝对算是非常优秀的一个. extjs中,两个方法很像,renderTo和applyTo,我在网上也搜了很多相关的内容,在这里举例为大家进行区分,欢迎大家交流指正. 主要区 ...

  5. Cookie的简单用法

    ASP.NET初学者使用cookie的时候会感觉很陌生,在学习的过程中掌握cookie对象的增删改查非常有必要,,下面是我学习的时候经常用到的这些方法 写入和读取Cookie都需要用户Respone对 ...

  6. &lbrack;20181124&rsqb;关于降序索引问题2&period;txt

    [20181124]关于降序索引问题2.txt --//链接:blog.itpub.net/267265/viewspace-2221425/,探讨降序索引中索引的键值.--//实际上使用函数sys_ ...

  7. &lbrack;离散时间信号处理学习笔记&rsqb; 7&period; z变换

    z变换及其收敛域 回顾前面的文章,序列$x[n]$的傅里叶变换(实际上是DTFT,由于本书把它叫做序列的傅里叶变换,因此这里以及后面的文章也统一称DTFT为傅里叶变换)被定义为 $X(e^{j\ome ...

  8. SFTP搭建&commat;windows using freeSHHd&amp&semi;FileZilla

    转自:http://blog.163.com/ls_19851213/blog/static/531321762009815657395/ Windows  xp 下 搭建 基于  ssh 的sftp ...

  9. UNITY2018 真机开启deepprofiling的操作

    手机上运行游戏并开启deepprofiling的命令如下 命令一:adb shell am start -n com.szyh.YHP1.kaopu/com.szyh.YHP1.kaopu.MainA ...

  10. 【Android】ADB常用指令与logcat日志

    ADB命令简介 ADB是一个功能强大的命令行工具.通过它可以直接和模拟器或真机进行交互.它是一个具有客户端和服务器端的程序. 它主要由三个部分组成: 客户端,它运行在你的开发机上,你可以通过执行adb ...