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 ...
随机推荐
- Exponential Backoff
f^x(f^y+f-m)+f-n =f^(x+y)+f^(f-m)+(f-n) <?php $exponent=0; w(80,3); function w($input,$base){ glo ...
- sqlbulk的问题
今天在bulk的时候遇到一个很“奇怪”的问题 无论怎样修改传入的值,也会报类型转换的错误.后来 在此文中找到了正解 bbs.csdn.net/topics/390430064 SqlBulkCopy不 ...
- <2013 08 20> -----澳大利亚博士研究生申请-----
1.澳大利亚昆士兰大学博士的申请一年中什么时间都可以,但奖学金的评选每年只有四轮.和美国不同的是,在提交申请材料之前,个人必须联系好愿意接收你的导师,这个可以自己套磁联系,也可以和那边学院的小秘联系, ...
- 密码硬编码(Password Management: Hardcoded Password)
在对项目进行安全扫描时,发现一些密码硬编码问题,本文主要三个方面:1)什么是密码硬编码:2)密码硬编码的危害:3)密码硬编码的解决方案. 一 什么是密码硬编码 将密码以明文的形式直接写到代码中,就是密 ...
- VM安装之分区、自定义安装包
一.分区 1.一般分为3个区:/root .swap./ 1)./root:引导分区.存放引导文件和Linux内核等. 启动文件:用于判断你需要启动哪个操作系统或者哪个内核: 内核:程序与硬件之间的桥 ...
- CentOS 7.4 下设置定时任务
cron介绍 我们经常使用的是crontab命令是cron table的简写,它是cron的配置文件,也可以叫它作业列表,我们可以在以下文件夹内找到相关配置文件. /var/spool/cron/ 目 ...
- mysql用户管理(新增用户及权限管理)
一.登录: # mysql –u root –p 回车输入密码 退出: #exit; 二.修改密码: 格式:mysqladmin –u 用户名 –p旧密码 password 新密码 # ...
- 基于 普通及Lambda方式实现策略模式
什么是策略模式 策略模式代表了解决一类算法的通用解决方案,你可以在运行时选择使用哪种方案.比如如何使用不同的条件(比如苹果的重量,或者颜色 )来筛选库存中的苹果.你可以将这一模式应用到更广泛的领域 , ...
- SpringMVC:学习笔记(4)——处理模型数据
SpringMVC—处理模型数据 说明 SpringMVC 提供了以下几种途径输出模型数据: – ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添 ...
- 深入浅出Node.js(下)
(五):Node.js的异步实现 专栏的第五篇文章<Node.js的异步实现>.之前介绍了Node.js的事件机制,也许读者对此尚会觉得意犹未尽,因为仅仅只是简单的事件机制,并不能道尽No ...