《mysql必知必会》学习_第15章

第15章:联结表

P98

外键:外键为某个表的一列A,同时这一列包含另一个表的主键值B(B属于A,等于或者小于的关系)

P99

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

# 选择的vend_name,prod_name只是在products表格中出现,所以无须指明是哪个表,这个的语句的条件是vend_id=products.vend_id ,排列顺序是先遵循vend_name的字母排列,若vend_name排列无效或重复,再遵循prod_name的排列。# ##当要检索的几个列出现在几个表中,又没有其他的限制条件,也没有说明读取的列是在同一个表,那得到的结果应该就会呈现笛卡儿积(##是我自己的观点)##

P101 笛卡儿积

select vend_name,prod_name,prod_price from vendors,products order by  vend_name,prod_name ;  #没有联结关系的表返回的结果是笛卡儿积,行数=第一个表的行数*第二个表的行数#  ##没有限定条件,而且要检索到的列出现在两个表格中##  ###下面没截图到###

P103 内部联结

select vend_name,prod_name,prod_price from vendors inner join products on vendors.vend_id=products.vend_id ; #这条语句和(select vend_name,prod_name,prod_price from vendors,products where vendors.vend_id =products.vend_id order by vend_name,prod_name; )表示的意思一样#

 P104 联结多个表,能联结的表的数目是没有影响的,列出表,定义它们之间的关系

select prod_name,vend_name,prod_price,quantity from orderitems,products ,vendors where products.vend_id=vendors.vend_id and orderitems.prod_id=products.prod_id and order_num=20005;  #多个表进行联结,原理和前面两个表是一样的#

P105

select cust_name,cust_contact from customers where cust_id in (select cust_id from orders where order_num 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,orders,orderitems where customers.cust_id=orders.cust_id and orderitems.order_num =orders.order_num and prod_id='TNT2';

#前面的两语句联结的数据库,表达的意思一样#

 理解好关联的表之间的关系就可以了

原文地址:https://www.cnblogs.com/qiyuanjiejie/p/9432745.html