一、联结

  联结是利用SQL的select能执行的最重要的操作。

  1、关系表:假如有一个包含产品目录的数据库表,其中每个类别的物品占一行。对于每种物品要求存储的信息包括产品描述和价格,以及生产该产品的供应商信息。

    现在假如有由同一供应商生产的多种物品,那么在何处存储供应商信息(如供应商名、地址、联系方式等)。

    由于相同数据出现多次违背了范式设计原则,因此在这个例子中,可以建立两个表,一个存储供应商信息vendors,另一个存储产品信息products。产品信息表中除了存储供应商ID外不存储其他供应商信息。因此vendors表的主键又叫做products的外键。他将vendors与products关联。

  2、外键(foreign key):外键为某个表中的一列,它包含另一个表中的主键值,定义了两个表之间的关系。

  

二、创建联结

  联结是一种机制,用来在一条select语句中关联表,因此称为联结。使用特殊的语法,可以联结多个表返回一组输出。

  select vend_name,prod_name,prod_price from vendors,products where vendos.vend_id=products.vend_id order by vend_name,prod_name;

  为了匹配两个列以vender.vend_id和products.vend_id指定,需要使用完全限定名,防止引起歧义。

  笛卡尔积:由没有联结条件的表关系返回的结果为笛卡尔积。检索出的行数目将是第一个表中的行数乘以第二个表中的行数。

  select vend_name,prod_name,prod_price from vendors,products order by vend_name,prod_name;

三、内部联结

  等值联结:它基于两个表之间的相等测试。可以使用:

  inner join on:

  select vend_name,prod_name,prod_price from vendors inner join products on vendors.vend_id = products.vend_id;

  以上语句以inner join指定,在使用这种语法时,联结条件用特定的on子句而不是where子句给出。传递给on实际条件与传递给where相同。

  inner join on性能比where好。

四、联结多个表

  SQL对一条select语句中可以联结的数目没有限制。创建联结基本规则也相同。首先列出所有表,然后定义表之间的关系。

  select prod_name,vend_name,prod_price,quantity from orderitems,products,vendors where products.vend_id=vendor.vend_id and orderitems.prod_id = products.prod_id and order_num=20005;

  联结的表越多,性能下降的越厉害。

  我们在之前使用的子查询其实也可以使用联结语句。如下:

  select cust_name,cust_contact from customers,orders,orderitems where customers.cust_id=orders.cust_id and orderitems.order_num=orders.order_num and prod_id='TNT2';

而子查询中可以这样写:select cust_name,cust_contact form customers where cust_id in (select cust_id from orders where order_num in(select order_num from orderitems where prod_id='TNT2'))

五、使用表别名

  不仅仅可以对列起别名,也可以对表起别名。

  select cust_name,cust_contact from customers as c,orders as o,orderitems as oi where c.cust_id = o.cust=id and oi.order_num=o.cust_id and oi.order_num=o.order_num and prod_id ='TNT2';

六、使用不同类型的联结:

  我们在前面使用了内部联结和等值联结,其实还有更复杂的联结

  1、自联结:假如你发现某物品(ID为DTNTR)存在问题,因此想知道该物品的供应商生产的其他物品是否也存在这些问题,因此,你需要先查到生产ID为DTNTR的物品供应商,然后找出这个供应商生产的其他物品。自连接的速度非常好:

  select prod_id,prod_name from products where vend_id=(select vend_id form products where prod_id ='DTNTR');

  也可以使用别名:  

  select p1.prod_id,p1.prod_name from products as  p1, products as  p2 where p1.vend_id=p2.vend_id and p2.prod_id='DTNTR';

  

  2、外部连接

  许多连接将一个表中的行与另一个表中的行向关联,但有时候会需要包含没有关联行的那些行。

  外链接不包含在相关表中没有关联行的行,这种类型的联结称为外部联结,例如:

  1、对每个客户下了多少订单进行计数,包括那些至今尚未下订单的客户。

  2、列出所有产品以及订购数目,包括没有人订购的产品

  3、计算平均销售规模,包括那些至今尚未下订单的客户。

  select customers.cust_id,orders.order_num from customers left outer join orders on(customers.cust_id=orders.cust_id);

  以上语句我们使用了左外连接,目的是也关联左表并没有被查询关联数据。当然我们也可以使用右连接。

  select customers.cust_id,orders.order_num form customers right outer join orders on (orders.cust_id=customers.cust_id);

  七、使用带聚集函数的联结

  聚集函数也可以与联结一起使用。

  如果要检索所有客户及每个客户所下的订单数:

  select customers.cust_name,customers.cust_id,count(orders.order_num) as num_ord from customers inner join orders on customers.cust_id = orders.cust_id group by customers.cust_id;

  聚集函数也可以分方便的与其他联结一起使用。

  select customers.cust_name,customers.cust_id,count(orders.order_num) as num_ord from customers left outer orders on customers.cust_id = orders.cust_id group by customers.cust_id ;

  外部联结,把没下订单的客户也查出来了。

最新文章

  1. 工作中常用的awk命令
  2. 【转】如何使用VS 2013发布一个可以在Windows XP中独立运行的可执行文件
  3. 日期与时间戳相互转化(js)
  4. python学习心得第三章
  5. Devexpress WidgetView 使用总结
  6. MongoDB学习笔记-01 简介、安装
  7. PHPCMS后台统计点击量时出现点击数不改变可能丢失了JS代码。
  8. mongoDb(2)聚合
  9. 如何在User版本开启串口(Uart),抓取上层Log,开启输入控制台
  10. css(字体,文本,边距,边框,阴影,背景,渐变,多重背景,列表)
  11. 通过ctypes获得python windows process的内存使用情况
  12. JS自动微信消息轰炸
  13. cocos2dx (关于斗地主人物偏移位置)
  14. Spring MVC 异常处理 - ResponseStatusExceptionResolver
  15. Openstack镜像和密码
  16. C++ 单链表的实现
  17. springboot(服务端接口)获取URL请求参数的几种方法
  18. Vue插槽 slot
  19. Struts2 利用拦截器 interceptor 控制登陆和访问权限
  20. Resharper快捷键及用法

热门文章

  1. 常见的版本号及Springcloud的版本
  2. 关于一些运算(&(与运算)、|(或运算)、^(异或运算)........)的本质理解【转】
  3. SpringCloud 构建微服务架构-练习
  4. 对Java、C#转学swift的提醒:学习swift首先要突破心理障碍。
  5. android:异步任务asyncTask介绍及异步任务下载图片(带进度条)
  6. linux怎么开启telnet服务
  7. CSS3个人盲点总结【总结中..........】
  8. C# WindowsAPI
  9. Tomcat学习(一)——使用Eclipse绑定Tomcat并发布应用
  10. django steps EASY WAY