資料庫應用黃皮本第3章關聯式資料庫語言SQL

2022-12-23 12:06:01 字數 5054 閱讀 7420

第3章關聯式資料庫語言sql

教材中習題3的解答

3.2 解:① select s#, sname

from s

where age<17 and ***=』f』;

②select cname(聯接查詢方式)

from s,sc,c

where and and ***=』m』;

或:select c#, cname(巢狀查詢方式)

from c

where c# in (select c#

from sc

where s# in (select s#

from s

where ***=』m』));

或:select c#, cname(存在量詞方式)

from c

where exists (select *

from sc

where

and exists (select *

from s

where

and ***=』m』));

exists 表示存在量詞, exists操作符後子查詢的結果集中如果不為空,則產生邏輯真值「true」,否則產生假值「false」

這類查詢和不相關子查詢有乙個明顯區別,即子查詢的查詢條件依賴於外層父查詢的某個屬性值,內層查詢由於與外層查詢有關,因此必須反覆求值

一般處理過程為:首先取外層查詢中表的第乙個元組,根據它與內層查詢相關的屬性值處理內層查詢,若where子句返回值為真,則取此元組放入結果表,然後再取外層表的下乙個元組,重複這一過程,直到外層表全部查完為止。

③ select

from t,sc, c,s

where and and sc. s#= and ***=』m』;

④ select distinct x..s#

from sc as x, sc as y

where and

5 select c#

from sc as x, sc as y

where and and

⑥ select c#

from c

where not exists

select *

from s, sc

where and

and sname=』wang』);

⑦ select c#, cname

from c

where not exists

(select *

from s

where not exists

select *

from sc

where s#= and c#=

外層迴圈依次掃瞄c關係中的每個元組,中層迴圈依次掃瞄s關係中的每個元組,內層迴圈依次掃瞄sc關係中的每個元組,對於c關係中的每個元組,若有乙個學生沒有選修,則最內層查詢為空,得出中層選擇條件為真,進而得出中層查詢不為空,從而外層的選擇條件為假,該課程不會選出來放到結果集中。所以,只有某個課程被學生中的所有學生選修,最內層查詢每次都不為空,中層選擇條件每次都為假,得出中層查詢結果為空,進而得出外層選擇條件為真,該課程元組被選擇出來放到結果集中。

⑧ select distinct s#

from sc as x

where not exists

select *

from c, t

where and tname=』liu』

and not exists

select *

from sc as y

where and

或者select distinct s#

from sc as x

where not exists

select *

from c

where t# in (select t# from t where tname=』liu』)

and not exists

select *

from sc as y

where and

加3.4

解:① (select * from r)

union

(select * from s);

② (select * from r)

intersect

(select * from s);

③ (select * from r)

except

(select * from s);

④ select *

from r, s;

⑤ select

from r, s

where

⑥ select

from r, s

where

⑦ select rr.*表示r中全部屬性)

from r, s

where

⑧ r÷πc(s)

select a, b

from r rx

where not exists

( select *

from s

where not exists

( select *

from r ry

where and and

3.7解: select count(distinct c#)

from sc;

select **g(age)

from s, sc

where and c#=』c4』 and ***=』f』;

select

from sc,c,t

where and and tname=『liu』

group by

select c#, count(s#)

from sc

group by c#

h**ing count(*)>10

order by 2 desc, 1;

select sname

from s

where s#>all(select s#

from s

where sname=』wang』)

and agefrom s

where sname=』wang』);

select s#, c#

from sc

where score is null;

select sname, age

from s

where sname like 』l%』;

select sname, age

from s

where ***=』m』

and age>(select **g(age)

from s

where ***=』f』);

select sname, age

from s

where ***=』m』

and age>all(select age

from s

where ***=』f』);

3.12

解:① insert into c

values('c8','vc++','t6');

③ delete from sc

where score is null;

④ delete from sc

where s# in(select s# from s where ***='f')

and c# in(select c# from c where t# in (select t# from t where tname='liu'));

⑤ update sc

set score=60

where score<60

and c# in(select c# from c where cname='maths');

⑥ update sc

set score=score*1.05

where s# in(select s# from s where ***='f')

and score<(select **g(score) from sc);

⑦ 用兩個update語句實現:

update sc

set score=score*1.04

where c#='c4' and score>70;

update sc

set score=score*1.05

where c#='c4' and score<=70;

(這兩個update語句的順序不能顛倒。)

⑧ update sc

set score=score*1.05

where score<(select **g(score)

from sc);

3.13

解:① create table emp

( echar(4) not null,

ename char(8) not null,

age smallint,

*** char(1),

ecity char(20),

primary key(e#));

create table comp

( cchar(4) not null,

cname char(20) not null,

city char(20),

primary key(c#));

create table works

( echar(4) not null,

c# char(4) not null,

salary smallint,

primary key(e#, c#),

foreign key(e#) references emp(e#),

foreign key(c#) references comp(c#));

第7章關聯式資料庫的規範化理論與資料庫的設計

提出的資料庫規範化理論 1.1 不好 的關係模式中存在的問題 可能存在的問題 資料冗餘 更新異常 插入異常 刪除異常 資料依賴 是可以作為關係模式的取值的任何乙個關係所必須滿足的一種約束條件,是通過乙個關係中各個元組的某些屬性值之間的相等與否體現出來的相互關係。資料依賴包括 函式依賴和多值依賴和其他...

第二章關聯式資料庫習題

一 選擇題 1 對於關係模型敘述錯誤的是 a 建立在嚴格的數學理論 集合論和謂詞演算公式基礎之一 b 微機dbms絕大部分採取關係資料模型 c 用二維表表示關係模型是其一大特點 d 不具有連線操作的dbms也可以是關聯式資料庫管理系統 2 關係模式的任何屬性 a 不可再分b 可再分 c 命名在該關係...

關聯式資料庫原理下的會計賬務資料庫結構分析

摘要 本文從關聯式資料庫原理和會計賬務核算電算化原理相結合的角度剖析會計賬務資料庫 電子賬 的構造原理,展示出會計電子賬結構的共性規則。資料庫是以某種資料模型所確定的資料結構方式來組織和儲存某個組織 或部門 相互關聯的資料集。資料庫管理系統是一種幫助使用者建立 使用 管理和維護資料庫的計算機系統軟體...