Advanced SQL: Relational division in jOOQ
Relational algebra has its treats. One of the most academic features is the relational division. It is hardly ever used, but comes in handy every now and then. And when you need it, you’ll probably hate yourself for having slept during the relevant classes at the university.
What is relational division?
Relational division is the inverse of a cross join operation. The following is an approximate definition of a relational division:
Assume the following cross join / cartesian product
C = A × B Then it can be said that
A = C ÷ B
B = C ÷ A
What does it mean, typically?
Let’s have a look at the sample provided on Wikipedia:
Wikipedia example of a relational division
This looks sensible. The division of Completed ÷ DBProject leads to a list of students that have completed all projects.
Now how to phrase that in SQL??
That’s not so simple as it looks. The most commonly documented solution involves a doubly-nested select statement using anti-joins. In human language (using double negative), it is like Fred and Sarah saying “there is no DBProject that we have not Completed“. Or in SQL:
|
1
2
3
4
5
6
7
8
9
|
SELECT DISTINCT "c1".Student FROM Completed "c1"WHERE NOT EXISTS ( SELECT 1 FROM DBProject WHERE NOT EXISTS ( SELECT 1 FROM Completed "c2" WHERE "c2".Student = "c1".Student AND "c2".Task = DBProject.Task )) |
Now, no one sane wants to remember this just for the 1-2 times in a SQL developer’s life that they actually need it. So they use jOOQ, which wraps up the above monster in a concise syntax:
|
1
2
3
4
5
|
create.select().from( Completed.divideBy(DBProject) .on(Completed.Task.equal(DBProject.Task)) .returning(Completed.Student)); |
Note that from the above SQL statement, it is immediately clear that proper indexing is of the essence. Be sure to have indexes on all columns referenced from the on(…) and returning(…) clauses.
More information
For more information about relational division and some nice, real-life examples, see
Advanced SQL: Relational division in jOOQ的更多相关文章
- Advanced SQL
Top number of records SELECT column_name FROM table_name LIMIT 5; Like/Not Like SELECT * FROM Custom ...
- 转载:10 Easy Steps to a Complete Understanding of SQL
10 Easy Steps to a Complete Understanding of SQL 原文地址:http://tech.pro/tutorial/1555/10-easy-steps-to ...
- 10 Easy Steps to a Complete Understanding of SQL
原文出处:http://tech.pro/tutorial/1555/10-easy-steps-to-a-complete-understanding-of-sql(已经失效,现在收集如下) Too ...
- 实例讲解 SQL 注入攻击
这是一篇讲解SQL注入的实例文章,一步一步跟着作者脚步探索如何注入成功,展现了一次完整的渗透流程,值得一读.翻译水平有限,见谅! 一位客户让我们针对只有他们企业员工和顾客能使用的企业内网进行渗透测试. ...
- PHP+MYSQL网站SQL Injection攻防
程序员们写代码的时候讲究TDD(测试驱动开发):在实现一个功能前,会先写一个测试用例,然后再编写代码使之运行通过.其实当黑客SQL Injection时,同样是一个TDD的过程:他们会先尝试着让程序报 ...
- HP+MYSQL网站SQL Injection攻防
WebjxCom提示:程序员们写代码的时候讲究TDD(测试驱动开发):在实现一个功能前,会先写一个测试用例,然后再编写代码使之运行通过.其实当黑客SQL Injection时,同样是一个TDD的过程: ...
- SQL注入备忘单
Find and exploit SQL Injections with free Netsparker SQL Injection Scanner SQL Injection Cheat Sheet ...
- jOOQ
jOOQ http://www.jooq.org/ jOOQ是个更不错的SQL解决方案. 你可以在Java中以一种类型安全的方式来书写SQL语句: // Typesafely execute the ...
- Qt SQL Programming 部分翻译
简介: Qt SQL 是 Qt 的重要模块之一,为了方便,Qt 对 SQL 进行了一系列的封装,并将 SQL API 分为如下三层: (1)驱动层 (2)SQL API ...
随机推荐
- Tomcat 配置连接池
1. Tomcat 配置 JNDI 资源 JNDI(Java Naming and Directory Interface), Java 命名和目录接口; JNDI 作用: 在服务器上配置资源, 然后 ...
- Nuxt使用element-ui
废话不多说,在Nuxt中引入Nuxt其实很简单,分下面几步 一.安装element-ui依赖 打开nuxt项目以后,在Terminal中通过 npm i element-ui -S 即可安装eleme ...
- 插叙LTE
- jdbc PreparedStatement 防止sql注入的关键代码片段
mysql-connector-java-5.1.38.jar PreparedStatement 的 setString(int parameterIndex, String x) 方法 for ( ...
- 我的Android进阶之旅------>android中service的onStartCommand()方法中intent为null的问题
今天在维护公司的一个APP的时候,突然爆了空指针异常, Caused by: java.lang.NullPointerException: Attempt to invoke virtual met ...
- linux安装jdk_1.8
转载自http://blog.csdn.net/ldl22847/article/details/7605650 1.下载jdk的rpm安装包,这里以jdk-8u141-linux-x64.rpm为例 ...
- boost之内存池
讲到内存池我们会想到对对象进行动态分配的过程new包含三个过程 1.使用operator new分配内存 2.使用placement new 初始化 3.返回内存地址. 分配内存可以分解成分配内存和获 ...
- 【算法题 14 LeetCode 147 链表的插入排序】
算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...
- SQL SREVER, ORACLE数据库连接字符串
采用windows身份验证模式 Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
- 流量分析系统----讲解-echarts模拟迁移(结合china.js)
百度 Echarts 地图->模拟迁徙,实现自动切换地图 小航哥注释: 1.本文主要是把模拟迁移的流程讲了一遍,讲的很好.具体实现参考航哥这篇随笔“流量分析系统----实现-echarts模拟迁 ...