【正文】
省情況不遵守行唯一性規(guī)則 。ORACLE和 INFORMIX使用 別名 或 表別名 , DB2 UDB使用 相關(guān)名 。 這是 SQL與關(guān)系代數(shù)的一個(gè)很重要的不同點(diǎn) 。 郭文明 子查詢 ? 1) IN謂詞 (NOT IN) 例 :求通過住在北京或上海的代理商訂貨的顧客的姓名和折扣 . select ame,dist from customers where cid in (select cid from orders where aid in (select aid from agents where city in (‘ 北京 ’ ,’ 上海 ’ ))); 例 :求由住在北京的顧客和住在北京的代理商組成的所有訂貨 ordno. select ordno from orders where (cid,aid) in (select cid,aid from customers c,agents a where =‘ 北京 ’ and =‘ 北京 ’ )。 select aid from agents where percent =all (select percent from agents)。 N O T EXISTS(Subquery)為真當(dāng)且僅當(dāng)返回集合為空 . 例 :求出既訂購了產(chǎn)品 p01有訂購了產(chǎn)品 p07的顧客 cid. 關(guān)系代數(shù) :πcid(σpid=’ p01’ (O))∩ πcid(σpid=’ p07’ (O)) select distinct cid from orders x where pid=‘ p01’ and exists (select * from orders where cid= and pid=‘ p07’ )。 not exists能被用來實(shí)現(xiàn)關(guān)系代數(shù)的 MINUS運(yùn)算 。 select city from customers union select city from agents。 如果面臨的查詢 “ 要求被檢索的對象集合必須符合 ‘ 所有 ’ 這類關(guān)鍵詞的條件 ” (FOR ALL)時(shí) , 關(guān)系代數(shù)要用到除運(yùn)算 。運(yùn)算 郭文明 UNION運(yùn)算和 FOR ALL條件 例:找出訂購了所有被顧客 c006訂購的商品的顧客的 cid. 反例:被 c006訂購但沒有被 : select from products p where in (select pid from orders x where =‘ c006’ ) and not exists (select * from orders y where = and =) 反例不存在: not exists (select from products p where in (select pid from orders x where =‘ c006’ ) and not exists (select * from orders y where = and =)) 最終: select cid from customers where not exists (select from products p where in (select pid from orders x where =‘ c006’ ) and not exists (select * from orders y where = and =)) 。 兩個(gè)子查詢從左到右的列類型是兼容的 ,即可以并 、 交 、 差 。 ALL考慮重復(fù)行及數(shù)目 郭文明 高級 SQL語法 例 : (Q UNION ALL Q UNION ALL Q) INTERSECT ALL (Q UNION ALL Q) 結(jié)果是 (Q UNION ALL Q),包含兩個(gè)重復(fù)行 。 以上三種形式在產(chǎn)品中并沒有完全實(shí)現(xiàn) 。 注 : ORACLE僅提供左連和右連 ,而且語法與標(biāo)準(zhǔn) SQL也不同 : SELECT ? FROM T1,T2 WHERE [[(+)]=|=[(+)]] AND condition T2保留所有行 與 T1連不上的用 NULL 郭文明 SQL中的集合函數(shù) ? SQL中稱集合函數(shù) (set function),ORACLE稱組函數(shù) (group function),DB2 UDB稱列函數(shù) (column function),INFORMIX稱聚集函數(shù) (aggregate function). ? 集合函數(shù)語法 :SET_FUNCTION ([ALL|DISTINCT] col)|COUNT(*) 注 :ALL時(shí)包括重復(fù)行 , DISTINCT不包括重復(fù)行 。 對 : SQL高級形式 ,ORACLE中可行 : select avg() from (select aid,max(dollars) as x from orders group by aid) t。 INTO子句中沒有出現(xiàn)的屬性列 , 新記錄在這些列上將取空值 。 ? 子查詢不需要用括號括起來 。其中 SET子句給出 expr的值用于取代相應(yīng)的屬性列值 。 update agents set =* ? Entry SQL92中 SET子句中不能用子查詢 , 但 SQL99及 ORACLE、 DB2 UDB等都支持 。 ? 刪除條件中可以有子查詢 刪除總訂貨金額小于 600的代理商 : delete from agents where aid in (select aid from orders group by aid having sum(dollars)600)。否則 ,給出反例 . DBMS和其中的 SQL語句 ,他們與講課時(shí)的不一樣或者不能用 . 郭文明 謝謝觀看 /歡迎下載 BY FAITH I MEAN A VISION OF GOOD ONE CHERISHES AND THE ENTHUSIASM THAT PUSHES ONE TO SEEK ITS FULFILLMENT REGARDLESS OF OBSTACLES. BY FAITH I BY FAITH 。 因此在執(zhí)行增刪改操作時(shí) ,要注意數(shù)據(jù)庫中數(shù)據(jù)的一致性 。 如果省略 WHERE子句 ,表示刪除表中全部元組 ,但表的定義仍在字典中 。 ? 只有一個(gè)表可以作為 UPDATE的對象 。 insert into my_c (select * from customers where city=‘ 北京 ’ )。 ? 在表定義時(shí)說明了 NOT NULL的屬性列不能取空值 ,否則會出錯(cuò) 。 select avg() from t。 ? GROUP BY對象的列上的空值會被分在同一組里 . 郭文明 SQL中行的分組 ? 如果要去掉分組后的某些行 ,不能用 where,只能用 HAVING子句 . 例 :求被至少兩個(gè)顧客訂購的產(chǎn)品 pid. select pid from orders group by pid having