【正文】
否則 ,給出反例 . DBMS和其中的 SQL語(yǔ)句 ,他們與講課時(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ù)庫(kù)中數(shù)據(jù)的一致性 。 ? 刪除條件中可以有子查詢 刪除總訂貨金額小于 600的代理商 : delete from agents where aid in (select aid from orders group by aid having sum(dollars)600)。 如果省略 WHERE子句 ,表示刪除表中全部元組 ,但表的定義仍在字典中 。 update agents set =* ? Entry SQL92中 SET子句中不能用子查詢 , 但 SQL99及 ORACLE、 DB2 UDB等都支持 。 ? 只有一個(gè)表可以作為 UPDATE的對(duì)象 。其中 SET子句給出 expr的值用于取代相應(yīng)的屬性列值 。 insert into my_c (select * from customers where city=‘ 北京 ’ )。 ? 子查詢不需要用括號(hào)括起來(lái) 。 ? 在表定義時(shí)說(shuō)明了 NOT NULL的屬性列不能取空值 ,否則會(huì)出錯(cuò) 。 INTO子句中沒(méi)有出現(xiàn)的屬性列 , 新記錄在這些列上將取空值 。 select avg() from t。 對(duì) : SQL高級(jí)形式 ,ORACLE中可行 : select avg() from (select aid,max(dollars) as x from orders group by aid) t。 ? GROUP BY對(duì)象的列上的空值會(huì)被分在同一組里 . 郭文明 SQL中行的分組 ? 如果要去掉分組后的某些行 ,不能用 where,只能用 HAVING子句 . 例 :求被至少兩個(gè)顧客訂購(gòu)的產(chǎn)品 pid. select pid from orders group by pid having count(distinct cid) =2。 注 : ORACLE僅提供左連和右連 ,而且語(yǔ)法與標(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ù)語(yǔ)法 :SET_FUNCTION ([ALL|DISTINCT] col)|COUNT(*) 注 :ALL時(shí)包括重復(fù)行 , DISTINCT不包括重復(fù)行 。 可以是視圖名 ORACLE不能有 AS 允許為表的列重新命名 允許是子查詢 ,此時(shí)別名必須 內(nèi)連 ,左連 ,右連 ,外連 連接條件 郭文明 高級(jí) SQL語(yǔ)法 例:檢索至少訂購(gòu)了一件價(jià)格低于 。 以上三種形式在產(chǎn)品中并沒(méi)有完全實(shí)現(xiàn) 。 (Q UNION ALL Q UNION ALL Q) EXCEPT ALL (Q UNION ALL Q) 結(jié)果是 (Q),沒(méi)有包含重復(fù)行 。 ALL考慮重復(fù)行及數(shù)目 郭文明 高級(jí) SQL語(yǔ)法 例 : (Q UNION ALL Q UNION ALL Q) INTERSECT ALL (Q UNION ALL Q) 結(jié)果是 (Q UNION ALL Q),包含兩個(gè)重復(fù)行 。 但不支持 INTERSECT ALL或MINUS ALL。 兩個(gè)子查詢從左到右的列類型是兼容的 ,即可以并 、 交 、 差 。運(yùn)算 被 c006訂購(gòu) 沒(méi)有被 郭文明 高級(jí) SQL語(yǔ)法 ? 以下介紹的高級(jí) SQL運(yùn)算符不是 Entry SQL92的部分 ,但幾乎都屬于 SQL99,目前產(chǎn)品中不一定支持 , 但可能出現(xiàn)在未來(lái)的數(shù)據(jù)庫(kù)產(chǎn)品中 。運(yùn)算 郭文明 UNION運(yùn)算和 FOR ALL條件 例:找出訂購(gòu)了所有被顧客 c006訂購(gòu)的商品的顧客的 cid. 反例:被 c006訂購(gòu)但沒(méi)有被 : 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 =)) 。 例:求通過(guò)住在北京的所有代理商訂了貨的顧客 cid. :住在北京但沒(méi)有為所求顧客 : select * from agents where =‘ 北京 ’ and not exists (select * from orders x where = and =) : not exists (反例 ) : select from customers where not exists (select * from agents where =‘ 北京 ’ and not exists (select * from orders x where = and =))。 如果面臨的查詢 “ 要求被檢索的對(duì)象集合必須符合 ‘ 所有 ’ 這類關(guān)鍵詞的條件 ” (FOR ALL)時(shí) , 關(guān)系代數(shù)要用到除運(yùn)算 。 行不重復(fù) 行重復(fù) ∪運(yùn)算 郭文明 UNION運(yùn)算和 FOR ALL條件 ? SQL中沒(méi)有等價(jià)的 247。 select city from customers union select city from agents。 例 :找出沒(méi)有通過(guò)代理商 a03訂貨的顧客 cid.