■The features of an entity in real world are expressed as attributes in relational model
E.g.a student can be described with the attributes such as name,sid,gender,age,birthday,nationality,etc.
关系型数据库中我们用表来描述实体,那么这个表的模式,即从哪些方面来描述这个实体即属性。比如学生就用名字、学号、性别、年龄等来描述。
■The value scope of an attribute is called its domain.(属性的取值范围,或者说值域)
►Atomic data---1NF(关系属性必须是原子的,不能再分,即不允许表中套表)
►Null(允许属性的某个值为NULL,即空值,但要注意的是,空值不是空串,不是零,而是不知道。像在填霍格沃茨学员申请表的时候,你忘记了自己的联系方式,就是不知道,而非零。)
■An entity of real world can be expressed as one or more than one relations.
实体和实体之间的联系在关系型数据库中可以表示成一张表或多张表。
■A relation is a N-ary relationship defined on all of its attribute domain.
一个关系就是在属性值域上定义的N元联系。
Suppose a relation R with attributes A1,A2,...,An,the corresponding domains are D1,D2,...,Dn,then R can be expressed as:
■This is called the schema of R ,and n is the number of attributes,called the degree of R(目或度).Ai(1<=i<=n) is attribute name.
■An instance (value) of R can be expressed as r or r(R),it is a set of n-tuple:
every tuple t can be expressed as:
that is:
that is:
■Relation is also called table.Attribute is also called column,and tuple is also called row.
■A set of attributes is a candidate key for a relation if:
1.No two distinct tuples can have same values in this set of attributes.and
2.This is not true for any subset of this set of attributes.(候选码是满足此1条件的最小属性集合)
每张表里面总有一个或一组属性的值可以唯一的决定这条元组中其他属性的值,我们把这样的属性或属性组叫做候选码。
►Part 2 false? A superkey.(如果不满足第二个条件的话,就成为超码)
►If there's >1 key for a relation,one of the key is chosen (by DBA) to be the primary key,and the others are called alternate key(候补码).
►If the primary key consists of all attributes of a relation,it is called all key(全码).
■That means,the key can decide a tuple uniquely.
■E.g.sid is a key for Students.(What about name?)The set {sid,gpa} is a superkey.
■Foreign key:Set of attributes in one relation that is used to 'refer' to a tuple in another relation.(Must correspond to primary key of the second relation.) Like a 'logical pointer'.
(外码是一张表中的一个或一组属性,作用是引用其他关系中的元组,且外码在被引用的那张表里面应该是主码。如选课表中学号是外码,引用学生表的学号属性,学号在学生表中是主码;选课表中课程号是外码,引用课程表中的课程号属性,课程号在课程表中是主码。)
■Eg:sid is a foreign key referring to Students:
►Enrolled(sid:string,cid:string,grade:string)
►If all foreign key constraints are enforced,referential integrity is achieved,i.e.,no dangling references.
(此处解释一下参照完整性,还是上面的例子,enrolled表中的学号由于是引用学生信息表中的学号,那么enrolled表中的学号必须在学生表中是存在的,这就是参照完整性。比如,你在定义的时候,告诉了DBMS学号是选课表的外码,DBMS就会自动帮你检查参照完整性,如果你想在选课表中插入一条新元组,而其学号在学生表中不存在,DBMS就会拒绝执行此操作。)
►Have you forgotten soft link?