Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences The Relational.

Similar presentations


Presentation on theme: "Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences The Relational."— Presentation transcript:

1 Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences The Relational Model and Relational Database Constraints IS 320 : Introduction to Database Introduction to Databases

2 Database Design Steps in building a database for an application: Introduction to Databases2 Real-world domain Conceptual Model (EER) DBMS data model Create Schema (DDL) Load data (DML)

3 Relational Data Structure In the relational model, all data is logically structured within relations (tables). Relation is a table with columns & rows. Holds information about entities. Attribute is a named column of a relation. Domain is the set of allowable values for one or more attributes. Every attribute in a relation is defined on a domain. Tuple is a row of a relation. Degree of a relation is the number of attributes it contains. Cardinality of a relation is the number of tuples it contains. Relational database is a collection of normalized relations with distinct relation names. Introduction to Databases3

4 Relational Data Structure Introduction to Databases4 4170010 4182000 4182034 4188134 4189860 4192134 StudentNo Al-Saleh Al-Ghanem Al-Fahad Saod Rashed Al-Fahad LName Amal Nora Laila Amal Rana Rania FNameInitial M. A. F. I. M. DOB 04-06-78 02-12-79 01-11-74 22-04-73 30-01-78 19-03-79 GPA 3.91 4.20 4.01 3.01 2.31 3.50 STUDENT Tuples Attributes Cardinality Relation Degree Relation Name

5 Properties of Relations The relation has a name that is distinct from all other relation names in the relational DB 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. Introduction to Databases5

6 Relational Integrity Constraints Constraints are conditions that must hold on all valid relation instances. There are four main types of constraints: 1. Domain constraints 2. Key constraints 3. Entity integrity constraints 4. Referential integrity constraints Introduction to Databases6

7 Relational Keys Introduction to Databases7

8 Key Constraints Primary key of R: A set of attributes PK of R such that no two tuples in any valid relation instance will have the same value for PK. Example: The STUDENT relation schema: STUDENT( Ssn, Name, Dob, Age, Address) The attribute set {Ssn} is a key of STUDENT because no two student tuples can have the same value for Ssn. Introduction to Databases8

9 Entity Integrity Constraints Entity Integrity: The primary key attributes PK of each relation schema R in S cannot have null values in any tuple. This is because primary key values are used to identify the individual tuples. PK  null for any tuple Note: Other attributes of R may be similarly constrained to disallow null values, even though they are not members of the primary key. Introduction to Databases9

10 DB Relations Relation schema is a named relation defined by a set of attributes If A 1, A 2,.., A n are a set of attributes, then relation schema R is: R = (A 1, A 2,.., A n ) Relational Database Schema is a set of relation schemas, each with a distinct name If R 1, R 2,.., R n are a set of relation schemas, then relational schema R is: R= {R 1, R 2,.., R n } Introduction to Databases10

11 Referential Integrity Constraints A constraint involving two relations (the previous constraints involve a single relation). Used to specify a relationship among tuples in two relations: the referencing relation and the referenced relation. Tuples in the referencing relation R 1 have attributes FK (called foreign key attributes) that reference the primary key attributes PK of the referenced relation R 2. A tuple t 1 in R 1 is said to reference a tuple t 2 in R 2 if t 1 [FK] = t 2 [PK]. A referential integrity constraint can be displayed in a relational database schema as a directed arc from R 1.FK to R 2. Introduction to Databases11

12 Referential integrity constraints displayed on the COMPANY relational database schema. Introduction to Databases12

13 Update Operations on Relations INSERT a tuple. DELETE a tuple. MODIFY a tuple. - Integrity constraints should not be violated by the update operations. - Several update operations may have to be grouped together. - Updates may propagate to cause other updates automatically. This may be necessary to maintain integrity constraints. Introduction to Databases13

14 Update Operations on Relations In case of integrity violation, several actions can be taken: Cancel the operation that causes the violation (REJECT option) Perform the operation but inform the user of the violation Trigger additional updates so the violation is corrected (CASCADE option, SET NULL option) Execute a user-specified error-correction routine Introduction to Databases14

15 Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Relational Database Design by ER- and EER-to- Relational Mapping IS 320: Introduction to Database Hatoon AlSagri Introduction to Databases

16 Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences ER Relational Model Introduction to Databases

17 Entity Type Relational Model STUDENT (SID, Lname, Fname, DOB, GPA) DEPARTMENT (DeptNo, Department Name, Location) Introduction to Databases17 STUDENT LName Name DOB SID GPA FName

18 The ER conceptual schema diagram for the COMPANY database. Introduction to Databases18

19 Result of mapping the COMPANY ER schema into a relational schema Introduction to Databases 19

20 Relational Model Introduction to Databases20 Weak Entity Type EMPLOYEE EmpNo LNameFName DOB DepNo FName DEPENDENT has EMPLOYEE (EmpNo, Lname, Fname, DOB) DEPENDENT (DepNo, EmpNo, FName)

21 For each binary 1:1 relationship type R in the ER schema, identify the relations S and T that correspond to the entity types participating in R. There are three possible approaches: (1) Foreign Key approach: Choose one of the relations--S, and include a foreign key in S the primary key of T. It is better to choose an entity type with total participation in R in the role of S. (2) Merged relation option: An alternate mapping of a 1:1 relationship type is possible by merging the two entity types and the relationship into a single relation. This may be appropriate when both participations are total. (3) Cross-reference or relationship relation option: The third alternative is to set up a third relation R for the purpose of cross-referencing the primary keys of the two relations S and T representing the entity types. Introduction to Databases21 ER-to-Relational Mapping Algorithm (contd.)

22 Relational Model Introduction to Databases22 1:1 Relationship EMPLOYEE(EmpNo, Lname, Fname, DOB) BRANCH(BrnNo, Name, EmpNo, StartDate, EndDate) BRANCH manage EMPLOYEE 1 1 (0,1)(1,1) StartDEndD EmpNo LNameFName DOB Name BrnNo

23 Relational Model Introduction to Databases23 1:M Relationship EMPLOYEE(EmpNo, Lname, Fname, DOB, BrnNo) BRANCH(BrnNo, Name) BRANCH allocate EMPLOYEE M 1 EmpNo LNameFName DOB Name BrnNo

24 Relational Model Introduction to Databases24 M:N Relationship EMPLOYEE(EmpNo, Lname, Fname, DOB) PROJECT(ProjNo, Name) Work-on(EmpNo,ProjNo, hours) PROJECT work-on EMPLOYEE M N hours EmpNo LNameFName DOB Name ProjNo

25 Tel_no Multivalue attributesRelational Model Multivalue attributes Relational Model EMPLOYEE EmpNo DOB Tel_no Introduction to Databases25 EMPLOYEE(EmpNo, DOB) TELEPHONE(EmpNo, tel_no)

26 Introduction to Databases26 Ternary Relationships Relational Model

27 ER ModelRelational Model Entity type“Entity” relation 1:1 or 1:N relationship typeForeign key (or “relationship” relation) M:N relationship type“Relationship” relation and two foreign keys n-ary relationship type“Relationship” relation and n foreign keys Simple attributeAttribute Composite attributeSet of simple component attributes Multivalued attributeRelation and foreign key Summary of Mapping for ER model constructs Correspondence between ER and Relational Models Introduction to Databases27

28 Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences EER Relational Model Introduction to Databases

29 EMPLOYEE( EmpNo, Fname, Lname, DOB, Salary,TypingSpeed,TGrade, EngType, Secretary Flag, Technician Flag, Engineer Flag ) SECRETARY TECHNICIAN ENGINEER EMPLOYEE EmpNo Fname LName DOB Salary o Typing Speed EngType TGrade Mandatory / Overlapping Introduction to Databases29

30 SECRETARY(EmpNo, Fname, Lname, DOB, Salary,TypingSpeed) TECHNICIAN(EmpNo, Fname, Lname, DOB, Salary,Tgrade) ENGINEER(EmpNo, Fname, Lname, DOB, Salary, EngType) SECRETARY TECHNICIAN ENGINEER EMPLOYEE EmpNo Fname LName DOB Salary d Typing Speed EngType TGrade Mandatory / Disjoint Introduction to Databases30

31 EMPLOYEE(EmpNo, Fname, Lname, DOB, Salary) SUB-EMP(EmpNo, TypingSpeed,TGrade, EngType, Secretary Flag, Technician Flag, Engineer Flag) SECRETARY TECHNICIAN ENGINEER EMPLOYEE EmpNo Fname LName DOB Salary o Typing Speed EngType TGrade Optional / Overlapping Introduction to Databases31

32 EMPLOYEE(EmpNo, Fname, Lname, DOB, Salary) SECRETARY(EmpNo, TypingSpeed) TECHNICIAN(EmpNo, Tgrade) ENGINEER(EmoNo, EngType) SECRETARY TECHNICIAN ENGINEER EMPLOYEE EmpNo Fname LName DOB Salary d Typing Speed EngType TGrade Optional / Disjoint Introduction to Databases32

33 Step8: Options for Mapping Specialization or Generalization Convert each specialization with m subclasses {S 1, S 2,….,S m } and generalized superclass C, where the attributes of C are {k,a 1,…a n } and k is the (primary) key, into relational schemas using one of the four following options: Option 8A: Multiple relations-Superclass and subclasses Create a relation L for C with attributes Attrs(L) = {k,a 1,…a n } and PK(L) = k. Create a relation L i for each subclass S i, 1 < i < m, with the attributes Attrs(L i ) = {k} U {attributes of S i } and PK(L i )=k. This option works for any specialization (total or partial, disjoint of over-lapping). Option 8B: Multiple relations-Subclass relations only Create a relation L i for each subclass S i, 1 < i < m, with the attributes Attr(L i ) = {attributes of S i } U {k,a 1 …,a n } and PK(L i ) = k. This option only works for a specialization whose subclasses are total (every entity in the superclass must belong to (at least) one of the subclasses). Introduction to Databases33 Mapping EER Model Constructs to Relations

34 Options for mapping specialization or generalization. Mapping the EER schema using option 8A Introduction to Databases34

35 Options for mapping specialization or generalization. Mapping the EER schema using option 8B Introduction to Databases35

36 Mapping EER Model Constructs to Relations Option 8C: Single relation with one type attribute. Create a single relation L with attributes Attrs(L) = {k,a 1,…a n } U {attributes of S 1 } U … U {attributes of S m } U {t} and PK(L) = k. The attribute t is called a type (or discriminating) attribute that indicates the subclass to which each tuple belongs. This option works only for a specialization whose subclasses are disjoint. Option 8D: Single relation with multiple type attributes. Create a single relation schema L with attributes Attrs(L) = {k,a 1,…a n } U {attributes of S 1 } U…U {attributes of S m } U {t 1, t 2, …,t m } and PK(L) = k. Each t i, 1 < I < m, is a Boolean type attribute indicating whether a tuple belongs to the subclass S i. This option works only for a specialization whose subclasses are overlapping. Introduction to Databases36

37 Options for mapping specialization or generalization. Mapping the EER schema using option 8C Introduction to Databases37 d

38 Options for mapping specialization or generalization. Mapping the EER schema using option 8D; with Boolean type fields Mflag and Pflag. Introduction to Databases38 o

39 Mapping EER Model to Relations (contd.) Mapping of Shared Subclasses (Multiple Inheritance) A shared subclass, such as STUDENT_ASSISTANT, is a subclass of several classes, indicating multiple inheritance. These classes must all have the same key attribute. We can apply any of the options discussed in Step 8 to a shared subclass, subject to the restriction discussed in Step 8 of the mapping algorithm. both 8C and 8D are used for the shared class STUDENT_ASSISTANT. Introduction to Databases39


Download ppt "Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences The Relational."

Similar presentations


Ads by Google