連結操作(Join operations):取兩個關係,將結果返回另一個關係
利用笛卡爾乘積,將兩個元組匹配
通常在 from 後做子查詢表達式
連接條件(Join condition):定義兩個關係中哪個元組匹配,連接結果存在那些屬性
course inner join prereqon
course.course_id= prereq.course_id
course left outer join prereq on
course.course_id= prereq.course_id
限定資料庫用戶可瀏覽的虛擬關係
視圖定義(View Definition)
定義一張視圖(View)
create view v as < query expression >
query expression為SQL表達式
v為視圖名稱
範例
create view faculty as
select ID, name, dept_name
from instructor
select name
from faculty
where dept_name= ‘Biology’
create view departments_total_salary(dept_name, total_salary) as
select dept_name, sum (salary)
from instructor
group by dept_name;
利用視圖定義其他視圖
repeat
Find any view relation vi in e1
Replace the view relation vi by the expression defining vi
until no more view relations are present in e1
視圖的更新
insert into faculty values (’30765’, ’Green’, ’Music’)
完整性約束
確保數據庫資料的一致性,不會因為缺是數據導致損害
1.客戶必須有非空的電話號碼
2.銀行薪資必須至少4美元
單一關係的限制
name varchar(20) not null
create table section (course_idvarchar (8),sec_idvarchar (8),
semester varchar (6),
year numeric (4,0),
building varchar (15),
room_number varchar (7),
time slot id varchar (4),
primary key (course_id, sec_id, semester, year),
check(semester in (’Fall’, ’Winter’, ’Spring’, ’Summer’));
參照完整性
關聯的兩個表中,被關聯的屬性會互相影響,不可單獨刪除或增加
A 為一組屬性。R 和 S 兩個包含屬性 A 的關係,其中 A 是 S 的主要鍵。如果對於所出現的任何值,這些值也會出現在 S 中的所有值也會出現。
create table course (
course_idchar(5) primary key,title varchar(20),
dept_name varchar(20) references department)
check (time_slot_idin (select time_slot_idfrom time_slot))
date '2005-7-27'
time '09:00:30'
time '09:00:30.75'
timestamp '2005-7- 27 09:00:30.75'
nterval '1' day
create type Dollarsas numeric (12,2) final
grant<privilege list>
on <relation name or view name> to<user list>
<user list>:user-id
* 視圖的授權並不代表基礎關聯的任何授權
SQL的特權
select:允許讀取關聯的訪問,或使用視圖查詢的能力
EX:予許U1~3的查詢關聯的授權
grant select on instructor to U1, U2, U3
在SQL中撤銷授權
revoke select on branch from U1, U2, U
角色
grant select on takes to instructor;
•create role student•
grant instructor to Amit;
•create role dean;
•grant instructor to dean;
•grant dean to Satoshi;
授權視圖
create view geo_instructor as
(select *
from instructor
where dept_name= 'Geology');
grant select on department to Amit with grant option;
revoke select on department from Amit, Satoshi cascade;
revoke select on department from Amit, Satoshi restrict;