1.Assignment 1
分為兩個(gè)部分:先做ER圖,建立conceptual model,這部分不算分,Week3會(huì)課上討論缤言,Week3結(jié)束會(huì)放出參考答案,最遲Week4初视事;第二個(gè)部分是用Week2搭好的PostgreSQL實(shí)現(xiàn)之前做的model胆萧,計(jì)分。
2.ER Model
再次強(qiáng)調(diào)relationship types:其一是cardinality俐东,課程遵循課上說的ER Notation跌穗,即“1”個(gè)的時(shí)候用“箭頭”,“多個(gè)”的時(shí)候用“非箭頭”犬性;其二是participation瞻离,如果是total participation用“加粗/雙線”,如果是partial participation用“正常粗細(xì)”乒裆。
注意:cardinality和participation都是relationship constraints套利,二者是并列關(guān)系,表達(dá)時(shí)可以疊加鹤耍。比如肉迫,粗箭頭。
處理復(fù)雜關(guān)系時(shí)稿黄,可以分多個(gè)圖完成喊衫,一張圖處理主要的relationship,這里的entity不帶attributes杆怕,其他的圖表征attributes和subclass族购。
subclass的線段如果加粗,和之前的notation含義相同陵珍,也是指total participation寝杖。
3.Relational Data Model
relational model將現(xiàn)實(shí)世界描述為有內(nèi)在聯(lián)系的關(guān)系/表格集合(a collection of inter-connected relations (or tables)),它能簡化復(fù)雜問題互纯,映射到可操作的文件結(jié)構(gòu)中瑟幕。
有兩類術(shù)語:其一是mathematical的,如relation, tuple, attribute;其二是data-oriented的只盹,如table, record, field/column辣往。雖然表達(dá)不算,但是是一致的殖卑,比如mathematical relation = data-oriented table站削。
3.1 Core values
relation是relational data model的核心,包括:
(1)name懦鼠,在具體的database是unique的钻哩,可以和其他database的relation name重名屹堰,比如肛冶,address出現(xiàn)在不同database中
(2)attribue,可以理解為column heading扯键。
attribute又包括:
(1)name睦袖,在具體的relation中應(yīng)該是unique的
(2)associated domain,相關(guān)的限制荣刑。例如馅笙,車牌VIN,它的限制是不能使用I, O, Q厉亏,因?yàn)檫@些字母容易和數(shù)字01混淆
3.2 Notice
(1)relation的instance通常叫做tuples, rows, records
(2)relation中的attribute必須是atomic的董习,不能是composite或者是multi-valued,這樣無法用table表達(dá)爱只。如果name是由first name和given name構(gòu)成皿淋,不能把name當(dāng)成attribute,而是first name和given name恬试。如果colours of car是一個(gè)multi-valued attribute窝趣,包括red, blue, black,不能把colours of car當(dāng)成attribute训柴,而是red, blue, black哑舒。
(3)如果出現(xiàn)上述的red, blue, black,一個(gè)車不可能同時(shí)多種顏色幻馁,如果是red洗鸵,那么要在blue和black的column上填寫“NULL”(NULL的含義很豐富,可以是none仗嗦,don't know, irrelevant)
(4)NULL雖然很好用膘滨,但是primary key不能是NULL,這樣會(huì)無法識(shí)別儒将。
(5)Database schema : a collection of relation schemas. (schema is a set of tables)
(6)Database (instance) : a collection of relation instances.
(7)unique:DBMS層面吏祸,database name必須unique;database層面,schema name必須unique贡翘;schema層面蹈矮,table name必須unique;table層面鸣驱,attribute name必須unique泛鸟。
3.3 Example
ER model中的entity映射到relational model中的table,entity的attribute映射到table中的column heading踊东,relation要分情況處理北滥,如果是1 to many,則把1中的key映射到many對(duì)應(yīng)的table column heading闸翅,如果是many to many再芋,則創(chuàng)建新的table把兩個(gè)many的key都寫入新table。
例如坚冀,圖中customer和branch的關(guān)系是home济赎,是1 to many,則把branch的key branchName寫入customer的table记某;account和customer的關(guān)系是many to many司训,則創(chuàng)建新的table,圖中命名的是heldBy液南,這個(gè)命名不好壳猜,因?yàn)槿绻麍D表復(fù)雜,會(huì)難以辨認(rèn)滑凉,最好命名為兩個(gè)entity的名字連接统扳,Account_customer,再把各自的key寫入這個(gè)table譬涡,也就是customer No.和Account No.
map后的instance如下圖
3.4 Integrity Constraints
attribute中包括domain闪幽,domains limit the set of values that attributes can take.因?yàn)楝F(xiàn)實(shí)世界中relation都有可以取值的范圍,比如上文舉例的車牌號(hào)涡匀,或者學(xué)號(hào)只能由英文和數(shù)字構(gòu)成盯腌,一個(gè)人的年紀(jì)不能是負(fù)數(shù)。
同時(shí)integrity意味著primary key不能是NULL陨瘩。
Referential Integrity constraints describe references between relations (tables), and they are related to notion of a foreign key (FK).
上文提到了不同cardinality下如何映射腕够,在table原有的attribute之外,還有來自其他table的key舌劳,這些key叫做foreign key帚湘。與primary key不同,foreign key可以是NULL甚淡,但來自同一個(gè)table的foreign key必須統(tǒng)一大诸,或者都是NULL,或者都有確定的值。Foreign key扮演了“關(guān)聯(lián)”的角色资柔,使得數(shù)據(jù)庫查詢成為可能焙贷,它連接了不同的table,實(shí)現(xiàn)了ER model中的relationships贿堰。
3.5 Describing Relational Schemas
SQL實(shí)現(xiàn)了Data Definition Language(DDL)辙芍,它基于database level,不是基于data的Data manipulation羹与」使瑁基本架構(gòu)如下:
CREATE TABLE TableName (
attrName1 domain1 constraints1 ,
attrName2 domain2 constraints2 ,
...
PRIMARY KEY (attri,attrj,...)
FOREIGN KEY (attrx,attry,...)
REFERENCES
OtherTable (attrm,attrn,...)
);
SQL的基本語法:
(1)--,代表comment
(2)'don''t ask'纵搁,在string中表達(dá)'時(shí)吃衅,重復(fù)兩次,而不是使用'don\'t ask'
(3)Identifiers and reserved words are case-insensitive:
TableName = tablename = TaBLeNamE != "TableName"
(4)數(shù)據(jù)類型诡渴,integer, float, char(n), varchar(n), date, ... (char is fixed, and varchar is flexible)
(5)操作符:=, <>, <, <=, >, >=, AND, OR, NOT, ...
要求掌握基本語法捐晶,考試時(shí)會(huì)涉及簡單內(nèi)容
用上文的例子來實(shí)現(xiàn):
create table Branch (
branchName text,
address text,
assets integer,
primary key (branchName)
);
--從沒有foreign key的table開始菲语,所有寫下的內(nèi)容妄辩,尤其是foreign key,前文一定要定義好山上。
create table Account (
accountNo text,
balance integer,
branchName text,
primary key (accountNo),
foreign key (branchName) references Branch(branchName)
);
create table Customer (
customerNo integer,
name text,
address text,
homeBranch text,
primary key (customerNo),
foreign key (homeBranch) references Branch(branchName)
);
create table Account_Customer (
account text,
customer integer,
primary key (account, customer),
--many to many的關(guān)系table primary key是tuple
foreign key (account) references Account(accountNo),
foreign key (customer) references Customer(customerNo)
);
增加限制條件:
1.no accounts can be overdrawn
2.customer numbers are seven-digit integers
3.account numbers look like A-101, B-306, etc.
4.the assets of a branch is the sum of the balances in all of the accounts held at that branch
create domain CustNumType as
char(7) check (value ~ '[0-9]{7}');
create table Branch (
branchName text,
address text,
assets integer,
primary key (branchName)
);
create table Account (
accountNo text check (accountNo ~ '^[A-Z]-[0-9]{3}$'),
balance integer check (balance >= 0),
branchName text,
primary key (accountNo),
foreign key (branchName) references Branch(branchName)
);
create table Customer (
customerNo CustNumType,
name text,
address text unique not null,
homeBranch text,
primary key (customerNo),
foreign key (homeBranch) references Branch(branchName)
);
create table Account_Customer (
account text,
customer CustNumYype,
primary key (account, customer),
foreign key (account) references Account(accountNo),
foreign key (customer) references Customer(customerNo)
);
create table t (
x integer,
y integer,
constraint xBiggerThanY check (x > y)
)眼耀;
相關(guān)語法可以搜索regular expression學(xué)習(xí)
4.Mapping ER Designs to Relational Schemas
Correspondences between relational and ER data models:
- attribute(ER) ? attribute(Rel), entity(ER) ? tuple(Rel)
- entity set(ER) ? relation(Rel), relationship(ER) ? relation(Rel)
Differences between relational and ER models:
- Rel uses relations to model entities and relationships
- Rel has no composite or multi-valued attributes (only atomic)
- Rel has no object-oriented notions (e.g. subclasses, inheritance)
4.1Mapping strong entities
和上文內(nèi)容一致
4.2Mapping weak entities
要有兩個(gè)primary key,一個(gè)是strong relation的佩憾,一個(gè)是weak relation的哮伟。
For 1:1 relationship, we have a choice, and we prefer total to partial participation in order to minimize the number of NULL.