前言
學習資料庫系統,記錄筆記。
參考資料:
《資料庫系統(設計、實作與管理 第6版, Thomas M.Connolly, Carolyn E.Begg)》
第一章:資料庫簡介
相關概念
- 資料(Data):Data are raw facts that describe people, objects and events.(資料是描述人、物和事件的原始事實。)
- 資料存儲:計算機存儲資料的方式可以分為兩種:記憶體(暫時存儲、掉電資料丢失),外存(永久存儲,掉電資料不丢失)
-
資料庫(DB):資料存儲的倉庫
資料的傳遞如下圖:

資訊系統包含:
- 事務處理系統(TPS)
- 線上事務處理系統(OLTP)
- 供應鍊管理系統(SCM)
- 客戶關系管理系統(CRM)
- 企業資源規劃系統(ERP)
運作資料庫可以操作資料:
收集、存儲、檢視、查找、更新、整理、分類、移動、删除
分析資料庫可以提取分析資料:
- 資料挖掘(Data Mining):找資料關系和模式
- 預測分析(Predictive Analytics):分析曆史和現在,預測未來
- 線上分析處理(OLAP):複雜因素檢測
-
資料庫系統(DBS):由硬體系統、資料庫、資料庫管理系統、資料庫應用程式、資料庫使用者等組成的系統。
資料庫系統的結構層次如下圖:
基于Web的資料庫應用系統工作流程如下:
- 資料庫管理系統(DBMS):A collection of programs that controls,create,maintain,and manipulate databases(一種控制、管理、建立、維護和使用資料庫的大型軟體)
- 合理管理和控制資料庫中資料
- 保證資料庫中資料的準确、安全、一緻性,高效地利用資料
- 建立在作業系統之上
- 使用者或應用程式發出操作資料庫中資料的指令都要通過資料庫管理系統來執行
- 資料的查詢是通過資料庫管理系統,而并非從資料庫直接查詢
- 存儲、檢索、更新資料:使用者通過資料庫管理系統通路資料庫
- 事務支援:把使用者或應用程式的一系列動作看成一個事務
- concurrency control under multiple users (多使用者并發控制):多使用者并行更新資料庫且更新正确
- Integrity(完整性):確定資料庫中資料及其的更改遵循規則
- Views(視圖):實作程式和資料的獨立,隐藏資料的安全機制
- Authorization(授權):授權使用者才可以通路
- Access controls(通路控制):Based on the granting and revoking of privileges.(基于權限的授予和回收)
- Backup and recovery(備份和恢複):資料庫受到破壞後被恢複
- Encryption(加密):使用特殊算法對資料進行編碼,使沒有解密密鑰的任何程式都無法讀取資料。
實作流程
資料庫實作流程主要分為系統設計階段以及系統實作兩個部分:
- 系統設計:又分為3個階段
- 概念資料庫設計:所用方法為E-R模組化模型
- 邏輯資料庫設計:所用方法為規範化模式
- 實體資料庫設計
- 系統實作:依靠資料庫語言來實作
- 建立資料庫的方法:資料庫定義語言(DDL)
- 操作資料庫的方法:資料庫操作語言(DML)
資料庫設計
資料庫設計要分為三個層次(三層體系結構 The Three-level ANSI-SPARC Achitecture):
- 外部層(使用者級)(不唯一):資料庫中的部分資訊,并非所有資訊。
- Users’view of the database.(使用者視圖)
- Describes that part of database that is relevant to a particular user.(和使用者相關的資料庫部分)
- 子模式:從模式導出的一個子集
- 概念層(概念級)(唯一):包含庫中所有資訊。
- Community view of the database.(資料庫整體視圖)
- Describes what data is stored in database and relationships among the data.(資料庫的邏輯結構):資料和資料之間的關系
- 内部層(實體級)(唯一):
- Physical representation of the database on the computer.(實體實作)
- Describes how the data is stored in the database.(如何存)
- 内部表示或底層描述
- 資料庫最低一級的邏輯描述
- 資料在存儲媒體上的存儲方式和實體結構,對應着實際存儲在外存儲媒體上的資料庫
資料模型
資料結構
- 結構化:統一的格式存儲資料 (存儲形式:關系型資料庫)
- 半結構化:可了解的邏輯流程和格式,格式變化很大(存儲形式:非關系型資料庫)
- 非結構化:每份檔案獨特的格式,結構不規則或不完整,資料模型不固定(存儲形式:非關系型資料庫)
資料模型的分類
不同的資料庫結構,對應不同的資料庫模型。
資料庫按照資料模型可分為:
- Hierarchical Database(層次資料庫)
- Networked Database(網狀資料庫)
- Relational Database(關系資料庫):現代大多數商業系統采用此模型
- Object Database(對象資料庫)
- Object-Relational(對象關系資料庫)
- Multi Dimensional Database(多元資料庫)
資料模型的特點
資料模型包含3個部分:
- a structural part(結構部分)
- a manipulative part(操作部分)
- possibly a set of integrity constraints(完整性規則)
資料模型(Model)與資料模式(Schema)的差別:
- Schema(模式):用某一具體的DBMS資料定義語言編寫
- Model(模型):比模式更高層次,更抽象地描述資料庫
資料庫語言
資料庫語言包含兩個部分:
- Data Definition Language(DDL)資料庫定義語言:
- Entities(實體)
- Attributes(屬性)
- Relationships(聯系)
- Integrity constraints(完整性限制)
- Security constraints(安全性限制)
- Data Manipulation Language(DML)資料庫操作語言:提供基本的資料處理操作。有兩種類型。
-
Procedural DML(過程DML):allows user to tell system exactly how to
manipulate data.(需指定如何獲得資料)
-
Non-Procedural DML(非過程DML):allows user to state what data is needed
rather than how it is to be retrieved.(說明需要什麼資料)
-
關系模型
關系模式/關系架構(Relation schema):A named relation defined by a set of attribute
and domain name pairs.(被屬性和域定義的關系)
- 定義:(A1:D1, A2:D2, … ,An:Dn)
- An為屬性名;Dn為屬性的取值範圍
-
執行個體:
屬性(A) 域名 含義 域定義(D) branchNo BranchNumbers 所有可能的分公司編号 4個字元,範圍B001~B999 street StreetNames 街道名稱 25個字元 city CityNames 城市名稱 15個字元 postcode Postcodes 郵編 8個字元 sex Sex 性别 1個字元,取值為M或F DOB DatesOfBirth 員工的生日 日期類型,範圍從1-1-20起,格式為dd-mmm-yy salary Salaries 員工的工資 7位數字,範圍6000.00~40000.00
Relational database schema(關系資料庫模式):其實就是關系模式的集合
下面以Dreamhome(一個房産銷售和租賃公司的資料庫系統)案例來了解關系資料庫模式:
表名(屬性):
Branch分公司(branchNo, street, city, postcode)
Staff職員(staffNo, fName, IName, position sex, DOB, salary, branchNo)
PropertyForRent待租房産(propertyNo, street, city, postcode, type, rooms, rent, ownerNo, staffNo, branchNo)
Client客戶(clientNo, fName, IName, telNo, prefType, maxRent, eMail)
PrivateOwner私人房産房主(ownerNo, fName, IName, address, telNo, eMail, password)
Viewing看房記錄(clientNo, propertyNo, viewDate, comment)
Registration注冊記錄(clientNo, branchNo, staffNo, dateJoined)
Lease合同(leaseNo, propertyNo, clientNo, rent, paymentMethod, deposit, paid, rentStart, rentStart, rentFinish, duration)
關系模型(Relational Model):是一種特殊的資料模型。
包含三要素:
- Relational Data structure(資料結構)
- Relational Data manipulation(資料操縱)
- Constraints of data integrity(資料的完整性限制)
關系資料模型的相關概念:
- Attribute(屬性):columns,field(列,字段):An attribute is a named column of a relation.(屬性是關系中命名的列)
- Domain(域):A domain is the set of allowable values for one or more attributes.(屬性的取值範圍,域是一個或多個屬性的取值集合)
- Tuple(元組):row,record(行,記錄):A tuples is a row of relation.(關系中的每一行稱為元組)
- Degree(維,目,度):屬性的數目(列數):The degree of a relation is the number of attributes it contains.(關系的維數是指關系所包含屬性的個數)
- Cardinality(基數):元組的數目(行數):The cardinality of a relation is the number of tuples it contains.(關系的基數是指它所包含元組的個數)
- Super Key(超關鍵字):One or more columns whose values are sufficient to identify a row.(一個屬性或屬性集合,它能唯一地辨別出關系中的每個元組)
- Candidate Key(候選關鍵字):A super key but no proper subset is a super key.(本身是超關鍵字但其任何子集都不再是超關鍵字):可能有多個
- Primary Key(主關鍵字):Select one of the candidate keys to use primarily as primary key.(被選用于唯一辨別關系中各元組的候選關鍵字,多個候選關鍵字中選最合适的一個)
- Foreign Key(外部關鍵字):An attribute or a set of attributes within a relation that matches the candidate key of some relation。(當一個關系中的某個屬性或屬性集合與另一個關系(也可是自己)的候選關鍵字比對時,就稱這個屬性或屬性集合為外部關鍵字)
下面以Dreamhome關系模式為例進行關鍵字說明:
關系完整性:
- Nulls(空):Null represents a value for an attribute that is currently unknown or is not applicable for this tuple.(代表對一個元組目前取值還不知道或是不可用的屬性值。)
-
Entity Integrity(實體完整性):
(Candidate)keys must be unique.(關鍵字值唯一)
In a relation,no attribute of a primary key can be null.(基本表主關鍵字的諸屬性均不能為空)
- Referential Integrity (參照完整性):If a foreign key exists in a relation,either the foreign value must match a candidate key value of some tuple in its home relation or the foreign key value must be wholly null.(外部關鍵字取參照候選關鍵字值或取空值)
- Enterprise constraints(企業限制):Additional rules specified by the users or database administrators of a database.(使用者和管理者定義的附加規則)