數據庫系統概念學習筆記

SQL 語句

/* 建立表 */
create table course
  course_id varchar(20),
  title varchar(20) not null,
  price numeric(5,2) default 100.00,
  primary key(course_id));

/* 刪除表 */
drop table course;

/* 增長列 */
alter table course add credits numeric(2,0);

/* 刪除列 */
alter table course drop credits;

/* 建立視圖 */
create view v as <query expression> 

/* 插入數據 */
insert into course
  values('CS-437', 'Database Systems', 'Comp.Sci.', 4);

/* 刪除數據 */
delete from course
  where course_id = 'CS-445';

/* 更新數據 */
update instructor
  set salary = salary * 1.05
  where salary < 70000;

/* 查詢數據 */
select distinct dept_name
  from instructor
  where salary > 80000 and dept_name <> 'Biology';

/* 用 as  給屬性別名(as 能夠省略)*/
select name as instructor_name from instructor;

/* 用 as  給關係別名(as 能夠省略)*/
select * from instructor as T,  instructor as S
    where T.sarary > S.sarary and S.dept_name = 'Biology';
    
 /* 建立索引 */
create index sid on student(ID);

/* 授予 */
grant <權限列表>
on <關係名或視圖名>
to <用戶/角色列表>

/* 收回 */
revoke <權限列表>
on <關係名或視圖名>
from <用戶/角色列表>

/* 建立角色 */
create role instructor;

字符串

  • %:匹配任意字符串
  • _:匹配一個字符
  • ||:串聯
  • trim():去掉後面的空格
  • upper()lower():轉大小寫
  • 查找類似:
select * from department 
where buidling like '%cat\_room%' escape '\';

排序

select * from instructor
order by salary desc, name asc; /* asc 能夠省略 */

where 中的謂詞

select * from instructor
where salary between 9000 and 10000; 
/* 等價於 salary >= 9000 and salary <= 10000;
   也有 not between */

集合

union
intersect
exceptsql

(select course_id from section where year = 2009)
union
(select course_id from section where semester = 'Spring')
去並集,會自動去重(union all 不去重)

空值

涉及空值的比較,返回 unknownexpress

判斷空值用 is null,不能用 = null閉包

彙集函數 Aggregate Functions

  • minmaxsumavgcount
  • count(distinct ID)
  • group by 分組彙集
  • having 分組的限定條件
  • in 集合成員資格, not in
  • > some 至少比某個大,< all, >=, <=, <>
  • exists 空關係測試
  • not exists(B except A) 「關係 A 包含關係 B」
  • unique 重複元組存在性測試
  • with 定義臨時關係

Join

  • Cross Join:即笛卡爾積
  • Inner Join:第一個關係的每個元組必須在第二個關係裏有對應項
  • Natural Join:\(R\Join S\)
  • Theta/Equi Join :\((R\Join_\theta S)\) 元組須要知足的斷言
  • Semi Join: 只返回第一個關係的記錄
  • Outer Join:保留了對應爲空的元組
select * from a join b on a.id=b.id;
select * from a natural left outer join b where course_id is null;
  • left outer join 左外鏈接
  • right outer join 右外鏈接
  • full outer join 全外鏈接
  • join... using (A1,A2) 只須要在某些屬性上相同的鏈接
  • join... on .. 相似 where 約束的鏈接

碼/鍵 Key

create table section
(
  course_id varchar(8),
  sec_id varchar(8),
  
/* check 約束,not null 約束*/
  semester varchar(6), check (semester in('Fall','Spring')),
  year numeric(4,0), check (year>1759 and year < 2100),
  room_number varchar(7) not null,
  primary key (course_id, sec_id, semester, year),
  foreign key (course_id) references course,
)
  • 超碼(superkey):一組能惟一標識一個元組的屬性集合
  • 候選碼(candidate key):最小超碼
  • 主碼(primary key):用來在一個關係中區分不一樣元組的候選碼
  • 外碼(foreign key):一個關係的屬性中包括另外一個關係的主碼,這個屬性就是參照另外一個關係的外碼。

候選碼用 unique 或者 primary key 約束。函數

參照完整性

create table course(
  ...
  foreign key(dept_name) references department
    on delete cascade
    on update cascade,
  );

cascade表明級聯,當刪除 department 元組時,course 的對應元組也會被級聯刪除。
相似的還有 set null、set default。測試

數據類型

  • varchar(n)
  • char(n)
  • int
  • numeric(p,d)
  • real
  • double precision
  • float(n)
    時間相關
  • date
  • time
  • timestamp
  • timestamp with timezone
  • interval
  • current_date() 當前日期
  • currrent_time()
  • current_timestamp() 帶時區
  • localtime() 本地時間
  • extract(filed from d) 從 date 或 time 類型的 d 中提取year,month,day,hour,minute,second的任意一種
  • 關係代數運算

  • \(\sigma\) Select 選擇
  • \(\Pi\) Projection 投影
  • \(\rho\) Rename 改名
  • \(\gets\) Assignment 賦值
  • \(\cup\) Union 並集
  • \(\cap\) Intersection 交集
  • \(-\) Difference 集合差
  • \(\times\) (Cartesian) Product 笛卡爾積
  • \(\Join\) (Natural) Join 天然鏈接
  • \(\mathcal{G}\) Aggregate 彙集
select A1, A2, sum(A3)
from r1, r2, ..., rm
where P
group by A1, A2

等價於
\(_{A_1,A_2}\mathcal{G}_{sum(A_3)}(\Pi_{A_1,A_2,\cdots,A_n}(\sigma_P(r_1\times r_2\times \cdots \times r_m)))\)ui

函數依賴

  • 關係實例知足函數依賴 :\(\alpha \rightarrow \beta\)\(t_1[\alpha]=t_2[\alpha]\),則\(t_1[\beta]=t_2[\beta]\)
  • 全部合法的實例都知足,則該函數依賴在模式 r 上成立。
  • \(\beta\subseteq\alpha\) ,則依賴\(\alpha \rightarrow \beta\)是平凡的。spa

  • Armstrong 公理:
    • 自反律:平凡依賴
    • 增補律:若\(\alpha \rightarrow \beta\)\(\gamma\)是屬性集,則 \(\gamma\alpha \rightarrow \gamma\beta\)
    • 傳遞律:若\(\alpha \rightarrow \beta,\beta \rightarrow \gamma\),則\(\alpha \rightarrow \gamma\)
    • \(ID,dept\_name \rightarrow name,salary,building\)
  • \(F^+\) 表示函數依賴集合F的閉包code

  • Boyce-Codd範式 BCNF:關係 R 是 BCNF 當它的函數依賴集 F 知足:\(F^+\) 中的全部非平凡依賴的都是 R 的超碼。排序

能夠將關係分解爲 BCNF 模式集合。索引

  • 第三範式 3NF:BCNF 的條件 || \(\beta-\alpha\)中的每一個屬性包含於 R 的一個候選碼中。
  • F 的正則覆蓋\(F_c\)知足:全部函數依賴不含無關屬性 且 \(\alpha\)都是惟一的。

  • \(R_1 \cap R_2\) 是 R 上的超碼,則是無損分解

  • 保持依賴:分解後總的函數依賴集與原函數依賴集保持一致
  • BCNF 分解:每次找出一個不知足 BCNF 的關係r,將 r 分解爲\(r-\beta\)\(\{\alpha,\beta\}\)兩個關係。
  • 3NF 分解:\(F_c\)的每一個函數依賴,構成關係\(\{\alpha,\beta\}\), 若全部關係都不包含 R 的候選碼,任意候選碼構成一個關係,刪除冗餘。
  • 求候選碼:
    1. 只在右邊的必定不屬於候選碼,
    2. 只在左邊的必定包含於候選碼,
    3. 不在函數依賴集中出現的必定包含於候選碼,
    4. 其它屬性與2,3的屬性的組合中(必須包含2,3的屬性),閉包等於全集 U 的爲候選碼。
相關文章
相關標籤/搜索