The Relational Model May Aldoayan
Simple uses the concept of a mathematical relation Oracle SQLServer and Acces the SQL query language, which is the standard for commercial relational DBMSs.
The relational model represents the database as a collection of relations. Informally, each relation resembles a table of values or, to some extent, a flat file of records May Aldoayan
Relational Data structure Attributes Branch Figure 3.1 postcode City Street branchNo SW1 AEH London 22 Deer Rd B005 AB2 3SU Aberdeen 16 Argyll St B007 G11 9QX Glasgow 163 Main St B003 BS99 1NZ Bristol 32 Manse Rd B004 NW10 6EU 56 Clover Dr B002 Relation Cardinality Degree Foreign Key Primary Key Staff branchNo Salary DOB Sex Position LName fName StaffNo B005 30000 1-Oct-45 M Manager White John SL21 B003 12000 10-Nov-60 F Assistant Beech Ann SG37 18000 24-Mar-58 Supervisor Ford David SG14 B007 9000 19-Feb-70 Howe Mary SA9 24000 3-Jun-40 Brand Susan SG5 13-Jun-63 Lee Julie SL41 Relation May Aldoayan
Relational Data structure Relation: A relation is a table with columns and rows. In the relational model, relations are used to hold information about the object to be represented in the database. Attribute: An attribute is a named column of a relation. Attribute can appear in any order and the relation will still be the same relation, and therefore convey the same meaning May Aldoayan
Relational Data structure Domain : A domain is the set of allowable values for one or more attributes. Domains may be distinct for each attribute, or two or more attributes may be defined on the same domain. Tuple: A tuple is a row of a relation. The elements of a relation are the rows or tuples in the table. May Aldoayan
Examples of Attribute Domains Pearson Education © 2009
Relational Data structure Degree: The degree of a relation is the number of attributes it contains. In figure 3.1, The Branch relation has four attributes or degree four. This means that each row of the table is a four-tuple, containing four values. A relation with only one attribute would have degree one and be called unary relation or one-tuple. A relation with two attribute is called binary, one with three attribute is called ternary May Aldoayan
Relational Data structure Cardinality: The cardinality of a relation is the number of tuples it contain. Relational database: A collection of normalized relation with distinct relation name . Database relations: Relation schema: A named relation defined by a set of attribute and domain name pairs. Relational database schema: A set of relation schema, each with a distinct name. If R1, R2, .., Rn are a set of relation schemas, then relational schema R is: R= {R1, R2, .., Rn} May Aldoayan
Alternative Terminology for Relational Model Pearson Education © 2009
Properties of relations The relation has a name that is distinct from all other relation names in the relational schema. Each cell of the relation contains exactly single value Each attribute has a distinct name The values of an attribute are all of the same domain Each tuple is distinct. There are no duplicate tuples The order of attributes has no significance the order of tuples has no significance; theoretically May Aldoayan
Relational Keys Primary Key: The candidate key that is selected to identify tuples uniquely within the relation. Foreign Key : is an attribute, or set of attributes, within one relation that matches the CK of some relation. Used to represent relationship between tuples of two relations. May Aldoayan
Integrity Constraints Null Represents value for an attribute that is currently unknown or not applicable for tuple. Deals with incomplete or exceptional data. Represents the absence of a value and is not the same as zero or spaces, which are values. Pearson Education © 2009
Relation Schema Student Dept GPA DOB LName FName StudentNo D001 4.03 15-1-1993 Alsaleh Maha 431671078 3.90 5-6-1992 AlAhmed Reem 430670028 D002 3.50 3-7-1993 AlQahtani Sara 431679802 4.50 10-10-1994 Alotaibi Suha 432784900 D003 4.89 12-3-1992 AlMutairi Lamea 430453772 STUDENT (StudentNo, Lname, Fname, DOB, GPA, Dept) Department Location Name Department DeptNo Build# 1 Computer Science D001 Build# 3 Business Administrator Doo2 Build # 6 Science D003 DEPARTMENT (DeptNo, Department Name, Location) May Aldoayan
1:1 Relationship Relational Model Identify an entity type (S) (preferably total participator) Include the PK of the other entity (T) as a FK in S Add attributes that describes the relationship. EMPLOYEE(EmpNo, Lname, Fname, DOB) BRANCH(BrnNo, Name, EmpNo) LName FName Name EmpNo DOB BrnNo 1 1 manage EMPLOYEE BRANCH (0,1) (1,1) May Aldoayan
1:M Relationship Relational Model Identify a participating entity type (S) on the m-side Include the PK of the other entity type (T) as a FK in S if S is at “M-side” of relationship type then include primary key of T in S Add attributes that describes the relationship STAFF (StaffNo, Lname, Fname, DOB) PropertForRent(ProertyNo, Address, StaffNo) LName FName Address PropertyNo StafNo Name DOB 1 M Oversees Staff ProperyForRent (0,*) (1,1) May Aldoayan
M:N Relationship Relational Model Create a new relation R to represent the relationship Include the PK of participating entity types (T & S) as FK in R. The combination of the two FK will form the PK of R Add attributes that describes the relationship EMPLOYEE(StaffNo, Lname, Fname, DOB) Project(ProjNo, Name) Work-on(EmpNo, ProjNo , hours) LName Name ProjNo Fname hours EmpNo DOB M N Work-on Employee Peoject May Aldoayan
n-ary Relationship Relational Model Create a new relation R to represent the relationship Include the PK of the participating entities as FK in R. The combination of all FK form the PK of R. Add attributes that describes the relationship BUSINESS(BizNo) LAWYER(LawNo) SUPPLIER(SupNo) contract(BizNo, SupNo, LawNo, StartDate, EndDate) StartD EndD SupNo BizNo contract BUSINESS SUPPLIER Lawyer LawNo May Aldoayan
Composite Attribute Relational Model Include its simple components in the relation EMPLOYEE(EmpNo, Fname, initial, Lname, DOB) initial FName LName name DOB emp_no EMPLOYEE May Aldoayan
Multi Valued Attribute Relational Model Suppose A is a relation that contains the multi valued attribute Create a relation R to represent the attribute Include the PK of A as FK in R The PK of R is the combination of the PK of A (FK) & the multivalued attribute EMPLOYEE(EmpNo, DOB) TELEPHONE(EmpNo, tel_no) DOB EmpNo Tel_no EMPLOYEE May Aldoayan
EER Relational Model Mandatory/NonDisjoint Suppose specialization with subclasses (S1, S2, .., Sm} & a superclass C Create a relation L to represent C with PK & attributes Include the unshared attributes for each subclass Si, 1 i m Add discriminator to distinguish the type of each tuple May Aldoayan
EER Relational Model Mandatory/NonDisjoint EMPLOYEE(EmpNo, Fname, Lname, DOB,Salary,TypingSpeed,TGrade, EngType, Secretary Flag, Technician Flag, Engineer Flag) Salary EmpNo LName EMPLOYEE Fname DOB o Typing Speed EngType SECRETARY TECHNICIAN ENGINEER TGrade May Aldoayan
EER Relational Model Mandatory/Disjoint Suppose specialization with subclasses (S1, S2, .., Sm} & a superclass C. Create a relation Li, 1 i m, to represent each combination of super/subclass May Aldoayan
EER Relational Model Mandatory/Disjoint SECRETARY(EmpNo, Fname, Lname, DOB, Salary,TypingSpeed) TECHNICIAN(EmpNo, Fname, Lname, DOB, Salary,Tgrade) ENGINEER(EmpNo, Fname, Lname, DOB, Salary, EngType) Salary EmpNo LName EMPLOYEE Fname DOB d Typing Speed EngType SECRETARY TECHNICIAN ENGINEER TGrade May Aldoayan
EER Relational Model Optional/NonDisjoint Suppose specialization with subclasses (S1, S2, .., Sm} & a superclass C Create a relation L to represent C with PK & attributes Create a relation Li to represent each subclass Si, 1 i m May Aldoayan
EER Relational Model Optional/NonDisjoint EMPLOYEE(EmpNo, Fname, Lname, DOB, Salary) SUB-EMP(EmpNo, TypingSpeed,TGrade, EngType, Secretary Flag, Technician Flag, Engineer Flag) Salary EmpNo LName EMPLOYEE Fname DOB o Typing Speed EngType SECRETARY TECHNICIAN ENGINEER TGrade May Aldoayan
EER Relational Model Optional/Disjoint Suppose specialization with subclasses (S1, S2, .., Sm} & a superclass C Create a relation L1 to represent C with PK & attributes Create a relation L2 to represent all subclasses Si, 1 i m Add discriminator to distinguish the type of each tuple May Aldoayan
EER Relational Model Optional/Disjoint EMPLOYEE(EmpNo, Fname, Lname, DOB, Salary) SECRETARY(EmpNo, TypingSpeed) TECHNICIAN(EmpNo, Tgrade) ENGINEER(EmoNo, EngType) Salary EmpNo LName EMPLOYEE Fname DOB d Typing Speed EngType SECRETARY TECHNICIAN ENGINEER TGrade May Aldoayan
E/R Relational Model Relational Model E/R Model Relational Entity Type FK or relation 1:1 or 1:M Relationship Type Relation & 2 FK M:N Relationship Type Relation & n FK n-ary Relationship Type Attribute Simple attribute Simple component attribute Composite attribute Relation and FK Multivalued attribute Domain Value set PK Key attribute May Aldoayan
Integrity Constraints General Constraints Additional rules specified by users or database administrators that define or constrain some aspect of the enterprise. Pearson Education © 2009
Integrity Constraints Entity Integrity In a base relation, no attribute of a primary key can be null. Referential Integrity If foreign key exists in a relation, either foreign key value must match a candidate key value of some tuple in its home relation or foreign key value must be wholly null. Pearson Education © 2009
Entity Integrity The first integrity rule applies to the primary Keys of base relations. Entity integrity : In a base relation, no attribute of a primary key can be null May Aldoayan
Relational Integrity The second integrity rule applies to foreign key. Referential Integrity: If a foreign key exists in a relation, either the foreign key 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 DBA of the DB. based on the ways an organization perceives and uses its data (e.g. number of staff working in a branch is at most 20) May Aldoayan
Views Base relation is a named relation corresponding to an entity in the conceptual schema, whose tuples are physically stored in the DB View is a derived relation. Virtual, may not exist, but dynamically derived from one or more base relations. The only information about a view that is stored in the database is its structure. The external model can consist of both conceptual level relations (base relations) and derived views. May Aldoayan
Views STUDENT Base Relation STUDENT_GPA View Dept GPA DOB LName FName StudentNo D001 4.03 15-1-1993 Alsaleh Maha 431671078 3.90 5-6-1992 AlAhmed Reem 430670028 D002 3.50 3-7-1993 AlQahtani Sara 431679802 4.50 10-10-1994 Alotaibi Suha 432784900 D003 4.89 12-3-1992 AlMutairi Lamea 430453772 Base Relation STUDENT_GPA GPA StudentNo 4.03 431671078 3.90 430670028 3.50 431679802 4.50 432784900 4.89 430453772 View May Aldoayan
Purpose of Views It provides a powerful and flexible security mechanism by hiding parts of the DB from certain users. It permits users to access data in a way that is customized to their needs, so that the same data can be seen by different users in different ways, at the same time. It can simplify complex operations. It allow you to work with data from different tables simultaneously. Supports logical data independence May Aldoayan
Updating Views All updates to a base relation should be immediately reflected in all views that reference that base relation. All updates to a view should be reflected in the underlying base relation, under the following rules: Updates are allowed through a view defined using a simple query involving a single base relation & containing either the PK or a CK of the base relation Update are NOT allowed through views involving multiple base relations Updates are not allowed through views involving aggregation or grouping operations May Aldoayan