Download presentation
Presentation is loading. Please wait.
Published byArline Jones Modified over 8 years ago
1
1 Database Design Chapter -5- The Structure of Relational Data Model References: Prof. Mona Mursi Lecture notes
2
2 Database Design Steps in building a database for an application: Real-world domain Conceptual model DBMS data model Create Schema (DDL) Load data (DML)
3
3 Relational Model It uses the concept of mathematical relation In the relational model, all data is logically structured within relations (tables).
4
4 Relational Data Structure 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 Attributes Tuples Cardinality Relation Degree Relation’s name
5
5 Relational Data Structure 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.
6
6 Relational Data Structure 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.
7
7 Tuples A tuple is a set of ( ) pairs. These tuples are identical: T1 = T2= The ordering of the tuples is not part of the relation.
8
8 Domain Dom(colors): {black,red, white},
9
9 Domains 4170010 4182000 4182034 StudentNo Al-Saleh Al-Ghanem Al-Fahad LName Amal Nora Laila FNameInitial M. A. DOB 04-06-78 02-12-79 01-11-74 GPA 3.91 4.20 4.01 STUDENT StudentNo LName FName Initial Student Name Last Name First Name Initial DefinitionAttribute DOB GPA Domain Name Date of Birth Great Point Average Digits: size 7 Character: size 15 Character: size 3 Date: range 01-01-20, format dd-mm-yy Real: size 3, decimal 2, range 0-5
10
10 Relational Keys Super Key (SK) a set of columns within a table whose values can be used uniquely to identify a row ( tuple). for any 2 distinct tuples t1,t2 in R we have: t 1 [SK] <>t 2 [SK]
11
11 Super Key Example -1- Employee (EmpoyeeID, name, job, DepartmentID) Use EmployeeID in combination with any or all other attributes to uniquely identify a row in the table. Example of super keys: {EmployeeID, name} {EmployeeID, name, job} {EmployeeID, name,job, DepartmentID} => Minimal superkey {EmployeeID} = it is also a SK
12
12 Relational Keys A key K of R is a super key. E.g. Student (Name, SSN, Hphone, Address, WPhone, Age, GPA) {SSN} is a key and also a super key. {SSN, Name, Ag} is super key but not a key.
13
13 Relational Keys Candidate key (CK) It’s a minimal set of attributes necessary to identify a row. Its also called minimal super key. A relation schema may have more than one key, each of the keys is called a candidate key. Primary Key (PK) is the CK that is selected to identify tuples uniquely within the relation Foreign Key (FK) 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.
14
14 Relational Keys 4170010 4182000 4182034 4188134 4189860 StudentNo Al-Saleh Al-Ghanem Al-Fahad Saod Rashed LName Amal Nora Laila Amal Rana FNameInitial M. A. F. I. DOB 04-06-78 02-12-79 01-11-74 22-04-73 30-01-78 GPA 3.91 4.20 4.01 3.01 2.31 STUDENT D001 D002 D003 DeptNo Computer Science Business Administration Science Department Name Build # 20 Dept D001 D002 D003 D001 Location Build # 45 Build # 6 DEPARTMENT Primary Key Foreign Key
15
15 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 }
16
16 Relation Schema 4170010 4182000 4182034 4188134 4189860 StudentNo Al-Saleh Al-Ghanem Al-Fahad Saod Rashed LName Amal Nora Laila Amal Rana FNameInitial M. A. F. I. DOB 04-06-78 02-12-79 01-11-74 22-04-73 30-01-78 GPA 3.91 4.20 4.01 3.01 2.31 STUDENT Dept D001 D002 D003 D001 STUDENT (StudentNo, Lname, Fname, Initial, DOB, GPA, Dept)
17
17 Relation Schema D001 D002 D003 DeptNo Computer Science Business Administration Science Department Name Build # 20 Location Build # 45 Build # 6 DEPARTMENT DEPARTMENT (DeptNo, Department Name, Location)
18
18 Relational Integrity Data integrity refers to the validity, consistency, and accuracy of the data in the database. Integrity rules are constraints that apply to all instances of the DB. Two integrity rules for the relational model: - Entity integrity - Referential integrity
19
19 Entity Integrity Entity Integrity: (in a single relation) Ensures that there are no duplicate records within the table. In a base relation, no attribute of a PK can be null.
20
20 Referential Integrity Referential Integrity: ( two relations) If a FK exists in a relation, either the FK value must match a CK value of some tuple in its home relation or the FK value must be wholly null. The referencing relation R1 has the FK that reference a PK in the referenced relation R2. => t 1 [FK] =t 2 [PK], t 1 in R1 and t 2 in R2
21
21 Referential Integrity Constraints SSN | Fname | LName | Bdate | DNO Dnumber | Dname | MGRSSN | MgrStartDate EMPLOYEE DEPARTMENT
22
22 Properties of Relations 1. The relation has a distinct name from all other relation names in the relational DB (oracle enforces this rule) 2. Each cell of the relation (attribute) contains exactly single value 3. Each attribute has a distinct name (oracle enforces this rule) 4. The values of an attribute are all of the same domain 5. Each tuple is distinct. There are no duplicate tuples 6. The order of attributes has no significance 7. the order of tuples has no significance; theoretically.
23
23 Integrity constraints Domain constraints Entity integrity constraints Referential integrity constraints Key Constraint
24
24 Operations of the Relational Model Updates: changes the database’s state. Insert Delete Update/modify Retrievals: does not change the current state of the database.
25
25 The Insert Operation This operation can violate all constraints of the relation. How? Domain constraint: attribute value does not appear in the corresponding domain. Key constraint: if the key value already exist in another tuple. Entity integrity: if the PK of the new tuple is NULL Referential integrity: if any value of the FK refers to a tuple that does not exist in the other relation.
26
26 The Delete Operation This operation can violate only the referential integrity constraint. How? E.g: you can not delete a course that many students study in already.
27
27 The Update Operation The new value must be of the correct data type and domain.
28
28 Relational Algebra
29
29 Relational Algebra Operations to manipulate relations. Used to specify retrieval requests (queries).
30
30 Theoretical expressions where both operands are relations The output from one operation can become the input to another operation (nested relational algebra) Eight fundamental operations in relational algebra: Unary operations work on one relation Selection Projection Binary operations work on pairs of relations Cartesian productJoin UnionIntersection Set differenceDivision Relational Algebra
31
31 Algebra Operators abcabc xyxy aabbccaabbcc xyxyxyxyxyxy Cartesian Product a1 a2 a3 b1 b2 b3 b1 b2 b3 c1 c2 c3 a1 a2 a3 b1 b2 b3 c1 c2 c3 Join xyxy a aaabcaaabc xyzxyxyzxy Divide SelectionProject UnionIntersection Difference
32
32 Selection Operation Return the tuples of R that satisfy a selection condition Denoted by (R) Selection condition = can be one condition or more using AND / OR. Example: Select all staff with a salary greater than 10,000 salary > 10000 (STAFF)
33
33 SL21 SG37 SG14 SG5 StaffNo John Ann David Susan FName White Beech Ford Brand LNameSex M F M F DOB 1-Oct-45 10-Nov-60 24-May-58 3-Jun-40 Salary 30000 12000 18000 24000 Selection Operation BrnNo D005 D003 salary > 10000 (STAFF)
34
34 Projection Operation Return the attributes of R Denoted by (R) Example: Produce a list of salaries for all staff, showing only the staff number, first name, last name, and salary. sno,fname,lname,salary (STAFF)
35
35 SL21 SG37 SG14 SNo John Ann David FName 30000 Salary 12000 18000 Projection Operation LName White Ford Beech SA9 SG5 SL41 Mary Susan Julie Howe Lee Brand 9000 24000 9000 sno,fname,lname,salary (STAFF)
36
36 Query Question :Retrieve the names and salaries of employees who work in department 4. FNAME,LNAME,SALARY ( DNO=4 (EMPLOYEE)) R1= DNO=4 (EMPLOYEE) R2= FNAME,LNAME,SALARY ( R1) You can rename the attributes of the resulted relation TEMP DNO=5 (EMPLOYEE) R(FIRSTNAME,LASTNAME,SALARY) FNAME, LNAME, SALARY (TEMP) OR
37
Query 37
38
38 Set Operations Binary operations Union R S Intersection R S Difference R-S Cartesian Product R x S Note: (union compatible): Dom ( Ai ε R )= Dom (Bi ε S) R,S must have the same number of attributes and same domain.
39
39 Union Operation Return a relation that includes all tuples that are either in R or in S, or in both R and S. Eliminate duplicate tuples. R & S must be union-compatible. Denoted by R S R S R S abcabc adfadf abcdfabcdf
40
40 Union Operation Example: List all cities where there is either a branch office or a property for rent. city (BRANCH) city (PROPERTY)
41
41 PA14 PL94 PG4 PG36 PG21 PropertyNo Holheld Argyll St Lawrence Monar Novar Street Aberdeen London Glasgow Bristol City PostCode AB7 5SU NW2 G11 9QX G32 4QX G12 Type House Flat House OwnerNo PROPERTY B001 B002 B003 BrnNo Deer Rd Argyll St Main St Street SW1 4EH CO46 CO87 CO40 CO93 CO87 PostCode AB2 3SU G11 9QX BRANCH Union Operation City London Glasgow Aberdeen City London Glasgow Aberdeen Bristol city (BRANCH) city (PROPERTY) BrnNo B001 B003
42
Example: retrieve the SSN of all employees who either work in department 5 or directly supervise an employee who works in department 5. DEP5_EMPS = DNO=5 (EMPLOYEE) RESULT1 = SSN (DEP5_EMPS) RESULT2(SSN) = SUPERSSN (DEP5_EMPS) RESULT = RESULT1 RESULT2 42
43
43 Intersection Operation Return a relation that includes all tuples that are in both R and S. R & S must be union-compatible. Denoted by R S R S R S abcabc adfadf a
44
44 Intersection Operation Example: List all cities where there is a branch office and at least one property for rent. city (BRANCH) city (PROPERTY)
45
45 PA14 PL94 PG4 PG36 PG21 PropertyNo Holheld Argyll St Lawrence Monar Novar Street Aberdeen London Glasgow Bristol City PostCode AB7 5SU NW2 G11 9QX G32 4QX G12 Type House Flat House OwnerNo PROPERTY B001 B002 B003 BrnNo Deer Rd Argyll St Main St Street SW1 4EH CO46 CO87 CO40 CO93 CO87 PostCode AB2 3SU G11 9QX BRANCH Intersection Operation City London Glasgow Aberdeen City city (BRANCH) city (PROPERTY) London Glasgow Aberdeen BrnNo B001 B003
46
46 Difference Operation Return a relation that includes all tuples that are in R but NOT in S. R & S must be union-compatible. Denoted by R - S R S R - S abcabc adfadf bcbc
47
47 Difference Operation Example: List all cities where there is a property for rent but no branch office. city (PROPERTY) - city (BRANCH)
48
48 PA14 PL94 PG4 PG36 PG21 PropertyNo Holheld Argyll St Lawrence Monar Novar Street Aberdeen London Glasgow Bristol City PostCode AB7 5SU NW2 G11 9QX G32 4QX G12 Type House Flat House OwnerNo PROPERTY B001 B002 B003 BrnNo Deer Rd Argyll St Main St Street SW1 4EH CO46 CO87 CO40 CO93 CO87 PostCode AB2 3SU G11 9QX BRANCH Difference Operation City London Glasgow Aberdeen City Bristol city (PROPERTY) - city (BRANCH) BrnNo B001 B003
49
49 b. STUDENT INSTRUCTOR c. STUDENT INSTRUCTOR d. STUDENT - INSTRUCTOR e. INSTRUCTOR - STUDENT
50
50 Cartesian Product Operation Return a relation that is the concatenation of tuples from two relations R & S Denoted by R X S {a,b} X {1,2,3} = {(a,1), (a,2), (a,3), (b,1), (b,2), (b,3)} Cartesian Product is meaningless on its own, it can combine related tuples from two relations if followed by the appropriate SELECT operation.
51
51 Cartesian Product Operation Example: retrieve a list of names of each female employee’s dependents. FEMALE_EMPS = SEX=‘F’ (EMPLOYEE) EMPNAMES = FNAME, LNAME, SSN (FEMALE _EMPS) EMP_DEPENDENTS = EMPNAMES x DEPENDENT ACTUAL_DEPENDENTS = SSN=ESSN (EMP_DEPENDENTS) RESULT = FNAME, LNAME, DEPENTDENT_NAME (ACTUAL_DEPENDENTS)
52
52
53
53 Join Operation Return a relation that is the concatenation of tuples from two relations R & S that satisfy a certain condition Form of join operation: - Theta join - Equijoin - Natural join - Outer join
54
54 * Similar to Cartesian product followed by Select. The condition is called join condition (>,<, = ….) The join condition contains one or more equality conditions. (Could be redundant) Contains one or more equality conditions With no redundancy If there are no matching tuples in S that matches R then the NULL is shown
55
55
56
56 Theta join Operation Return a relation that includes all tuples that satisfy the logical condition F from the Cartesian product of R & S. Logical operators ( ) Denoted by R F S = F (R X S)
57
57 Theta join Operation CLIENT(ClientNo, FName, Lname, TelNo) VIEW(ClientNo, PropertyNo, ViewDate, Comment) Example: List the names and comments of all renters who have viewed a property. Client.ClientNo, Fname, Lname, View.ClientNo, PrprtyNo, Comment (CLIENT Client.ClientNo=View.ClientNo VIEW)
58
58 Equijoin Operation A Theta join where the logical condition is equality (=). Example: List the names and comments of all renters who have viewed a property. Client.ClientNo, Fname, Lname, View.ClientNo, PrprtyNo, Comment (CLIENT Client.ClientNo=View.ClientNo VIEW)
59
59 CR76 CR56 CR74 CR62 Client. ClientNo John Aline Mike Mary FName Kay Stewart Ritchie Tregear LName View. ClientNo PrprtyNo Comment CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR76JohnKay CR76JohnKay CR76JohnKay CR76JohnKay CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR56 AlineStewart CR56 AlineStewart CR56 AlineStewart CR74 MikeRitchie CR74 MikeRitchie CR74 Mike Ritchie CR74Mike Ritchie CR74Mike Ritchie CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR62 Mary Tregear CR62 Mary Tregear CR62 Mary Tregear CR62 Mary Tregear ClientNo, Fname, Lname (CLIENT) X clientNo, PropertyNo, comment (VIEW))
60
60 CR76 CR56 Client. ClientNo John Aline FName Kay Stewart LName View. ClientNo PrprtyNo Comment CR76 CR56 PG4 PA14 Too remote Too small PG4 PA14CR62 No dining roomMary Tregear CR62 Client.ClientNo, Fname, Lname, View.ClientNo, PrprtyNo, Comment (CLIENT Client.ClientNo=View.ClientNo VIEW) CR56 AlineStewart Equijoin Operation
61
61 Natural join Operation Equijoin of two relation R & S over all common attributes and values. One occurrence of each common attribute is eliminated from the result Denoted by R * (A ε R ),(B ε S ) S Example: Retrieve each EMPLOYEE’s name and the name of department he/she belongs to: T EMPLOYEE * (DNO),(DUNMBER) DEPARTMENT RESULT π FNAME, LNAME, DNAME (T)
62
62 Natural join Operation To apply a NATURAL JOIN on the department number attribute of DEPARTMENT and PROJECT: DEPT = π (DNAME,DNUM,MGRSSN,MGRSTARTDATE) (DEPARTMENT) PROJ_DEPT = PROJECT * DEPT
63
63 Outer join Operation A natural join in which tuples from R that do not have matching values in the common attributes of S are included in the result relation. Missing values in S are set to null. Denoted by R S Example: Produce a status report on property viewings. ( PropertyNo, Street, City, CLientNo,ViewDate, Comment (PROPERTY) VIEW)
64
64 PA14 PL94 PG4 PG36 PG21 PropertyNo Holheld Argyll St Lawrence Monar Novar Street Aberdeen London Glasgow Bristol City PostCode AB7 5SU NW2 G11 9QX G32 4QX G12 Type House Flat House OwnerNo PROPERTY CO46 CO87 CO40 CO93 CO87 VIEW Outer Join Operation CR56 CR76 CR56 ClientNo PA14 PG4 PrprtyNo Too small Comment Too remote No dining room ViewDate 24-May-01 26-May-01 20-Apr-01 CR62PA14 14-May-01
65
65 PA14 PL94 PG4 PG36 PG21 PropertyNo Holheld Argyll St Lawrence Monar Novar Street Aberdeen London Glasgow Bristol City ClientNo CR56 null CR76 ViewDate 24-May-01 null 20-Apr-01 Comment Too small null Too remote Outer Join Operation PA14 HolheldAberdeenCR6214-May-01 No dining room null PG4 Lawrence Glasgow CR5626-May-01 null PropertyNo, Street, City, CLientNo,ViewDate, Comment (PROPERTY) VIEW
66
66 More examples
67
67 UNION Example
68
68 INTERSECTION Example
69
69 DIFFERENCE Example
70
70 CARTESIAN PRODUCT example
71
71 JOIN Example
72
72 OUTER JOIN example 1
73
73 OUTER JOIN example 2
74
74 Additional Relational Functions
75
75 Aggregate Functions 1. SUM : returns the sum of the values in a specified attribute. 2. COUNT: returns the number of tuples in a specified attribute. 3. AVERAGE: returns the average of the values in a specified attribute. 4. MIN: returns the smallest value in a specified attribute. 5. MAX: returns the largest value in a specified attribute.
76
76 Examples NRNameSalaryDepartment 1John100A 5Sara300C 7Tom100A 12AnnienullC EMPLOYEE
77
77 1- SUM Find the total of the employees salary R SUM(SALARY) (EMPLOYEE) The result is sum= 500 select sum(salary) from EMPLOYEE Note: Duplicates are not eliminated. Null values are ignored.
78
78 2- COUNT Count all the empolyees who get a salary R count(salary) (EMPLOYEE) The result is count=3 select count(salary) from EMPLOYEE
79
79 3- AVERAGE Retrieve the average of the employees’ salaries. R AVG( SALARY) (EMPLOYEE) Select avg(salary) from EMPLOYEE
80
80 4- MIN Retrieve the minimum salary among all employees. R MIN (SALARY) (EMPLOYEE) The result is min= 100
81
81 5- MAX Retrieve the maximum salary among all employees. R MAX( SALARY) (EMPLOYEE) The result is max= 300
82
82 Summary of Relational Algebra Operations Operation Notation Selection Projection Union Difference Intersection Cartesian Product Theta join Equijoin Natural join Outer join Aggregate Function (R) (R) R S R - S R S R X S R F S R * S R S
83
83 Example (Company Example)
84
84 Examples of Queries in Relational Algebra Query 1: Retrieve the name and address of all employees who work for the ‘Research’ department. R_DEPT DNAME=‘Research’ (DEPARTMENT) R_EMPS (R_DEPT DNUMBER=DNO EMPLOYEE) RESULT LNAME, FNAME, ADDRESS (R_EMPS)
85
85 Examples of Queries in Relational Algebra Query 2: For every project located in ‘Stafford’, list the project number, the controlling department, and the department manager’slast name, address, and birth date. S_PROJS PLOCATION=‘Stafford’ (PROJECT) C_DEPT (S_PROJS DNUM=DNUMBER DEPARTMENT) P_DEPT_MGR (C_DEPT MGRSSN=SSN EMPLOYEE) RESULT PNUMBER, DNUM, LNAME, ADDRESS, BDATE (P_DEPT_MGR)
86
86 Examples of Queries in Relational Algebra Query 6: Retrieve the names of employees who have no dependents. ALL_EMPS SSN (EMPLOYEE) EMPS_WITH_DEPS(SSN) ESSN (DEPENDENT) EMPS_WITHOUT_DEPS (ALL_EMP - EMP_WITH_DEPS) RESULT LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)
87
87 Examples of Queries in Relational Algebra Query 7: list the names of managers who have at least one dependent.. MGRS(SSN) MGRSSN (DEPARTMENT) EMPS_WITH_DEPS(SSN) ESSN (DEPENDENT) MGRS_WITH_DEPS (MGRS EMPS_WITH_DEPS) RESULT LNAME, FNAME (MGRS_WITH_DEPS * EMPLOYEE)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.