Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester 3 2014.

Similar presentations


Presentation on theme: "Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester 3 2014."— Presentation transcript:

1 Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester 3 2014

2 2  Relational Model Relational Model  Tables Tables  Relations VS Tables Relations VS Tables  Example of a Relation Example of a Relation  Attribute Types Attribute Types  Relation Schema and Instance Relation Schema and Instance  Schema VS. Instance Schema VS. Instance *Words with underline can be clicked on

3 3  Relations are Unordered Relations are Unordered  Database Database  Keys Keys  Integrity Constraints Integrity Constraints  Primary Key Constraint Primary Key Constraint  Foreign Key Constraint Foreign Key Constraint  Referential Integrity Enforcement Referential Integrity Enforcement *Words with underline can be clicked on

4 4  Complex Integrity Constraints Complex Integrity Constraints  Relational Set Operators Relational Set Operators  SELECT SELECT  PROJECT PROJECT  UNION UNION  INTERSECT INTERSECT  DIFFERENCE DIFFERENCE *Words with underline can be clicked on

5 5  PRODUCT PRODUCT  JOIN JOIN  Natural Join Natural Join  DIVIDE DIVIDE  Relational Operator Symbols Relational Operator Symbols *Words with underline can be clicked on

6 Relational model is the most widely used data model today. Relational model view data logically rather than physically. Relational database model is easier to understand than hierarchical and network models. It’s main construct is a relation: table of records. 6

7 Every relation has a schema:  Relation name  Names of fields  Types of fields Attribute is a named column of a relation. Record/Tuple is a row of a relation. Degree of a relation is the number of attributes it contains. Cardinality is the number of tuples it contains. 7

8 Table is structural and data independence. It resembles a file conceptually. Table is a two-dimensional structure composed of rows and columns. It contains group of related entities. (entity set) 8

9 Tables are “flat” or two-dimensional; relations are of n-dimensions. A table may be said to represent a relation if and only if:  Each column have an underlying type and all atomic value will be of that type.  Row and column orderings will be irrelevant.  Duplicate rows are forbidden. 9

10 10 attributes (or columns) tuples (or rows) instance schema

11 The set of allowed values for each attribute is called the domain of the attribute. Attribute values are (normally) required to be atomic; that is, indivisible. The special value null is a member of every domain. The null value causes complications in the definition of many operations. 11

12 Formally, given sets A 1, A 2,…., A n a relation R is a subset of A 1 x A 2 x …. x A n Thus, a relation is a set of n-tuples (d 1, d 2,… d n ) where each d i  A i The current values (relation instance) of a relation are specified by a table. An element t of r is a tuple, represented by a row in a table. 12

13 Says A 1, A 2,…… A n are attributes R = (A 1, A 2, …., A n ) is a relation schema Example: instructor = (ID, name, dept_name, salary) 13

14 Instance: the data in the database at a particular moment in time. (database state or snapshot) Database changes frequently.  Each insert, delete or update change from one state of the database into another state. Schema is fairly static.  Changes occur only as the application requirements change. 14

15 Order of tuples is irrelevant (tuples may be stored in an arbitrary order) E.g. instructor relation with unordered tuples. 15

16 A database consists of multiple relations Information about an enterprise is broken up into parts: ◦ instructor ◦ student ◦ advisor 16

17 Is the design below good? uni(instructor_ID, name, dept_name, student_id,..) Bad design results in:  repetition of information (e.g. two students have the same instructor)  the need for null values (e.g. represent an student with no advisor) Normalization theory deals with how to design “good” relational schemas. 17

18 Let K  R K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R) Composite key is a key composed of more than one attribute. e.g. {ID} and {ID,name} are both superkeys of instructor. 18

19 Superkey K is a candidate key if K is minimal Example: {ID} is a candidate key for Instructor Entity integrity – each row (entity instance) in the table has its own unique identity Nulls ◦ No data entry ◦ Not permitted in primary key ◦ Should be avoided in other attributes 19

20 ◦ Can be represent:  An unknown attribute value  A known, but missing, attribute value  A “not applicable” condition ◦ Can create logical problems when:  functions such as COUNT, AVERAGE, and SUM are used.  relational tables are linked. 20

21 Controlled redundancy ◦ Makes the relational database work ◦ Tables within the database share common attributes  Enables tables to be linked together ◦ Multiple occurrences of values not redundant when required to make the relationship work ◦ Redundancy exists only when there is unnecessary duplication of attribute values. 21

22 Foreign Key (FK) – an attribute whose values match primary key values in the related table Referential integrity – FK contains a value that refers to an exising valid tuple (row) in another relation Secondary key – Key used strictly for data retrieval purposes. 22

23 IC is the condition that must be true for any instance of the database (e.g. domain constraints)  A legal instance of a relation is one that satisfies all specified ICs.  ICs are specified when schema is defined.  ICs are enforced when tables are modified. 23

24 A set of fields is a key for a relation if no two distinct tuples can have the same values in all key fields (unique) If there is more than one key for a relation:  Each is called a candidate key  One candidate key is designated as the primary key  Other candidate key(s) are designated as unique key(s) 24

25  Candidate Keys: SID, and Login  Primary Key: SID  Unique Key: Login 25

26 Foreign key is a set of fields in one relation that is used to “refer” to a tuple in another relation. it must correspond to primary key of the referred relation. Like a “logical pointer” and there shouldn’t be dangling references. 26

27 If foreign key constraint are enforced, referential integrity is achieved. E.g.  Only students can enroll in a class.  Only students enlisted in “Students” relation should be allowed to enroll for courses. 27

28 28 Can 546009 be added to Enrolled.SID? Can Students.SID = 546007 be deleted? This is Referential Integrity Constraints.

29 What are the alternatives when a tuple with referential integrity constraints has to be deleted?  CASCADE - Delete all tuples that refer to it.  RESTRICT - Disallow deletion of a tuple that is referred to.  Set a “default” value for the tuple.  NULLIFY - Set a special value “null”, denoting “unknown” or “inapplicable”. 29

30 Three complex integrity constraints  Checks ◦ Used to limit the value range that can be placed in a column. E.g. CHECK(AGE>18)  Assertions ◦ A constraint at Schema level. Involves tables.  Triggers ◦ Is a procedural code that is automatically executed in response to certain events. 30

31 Relational Algebra – Define theoretical way of manipulating table contents relational operators. Below are the list of relational algebra operators:  SELECT  PROJECT  UNION  INTERSECT 31  DIFFERENCE  PRODUCT  JOIN  DIVIDE

32 SELECT, also known as RESTRICT, yields values for all rows found in a table that satisfy a given condition. SELECT can be used to list all of the row values, or it can yield only those row values that match a specified criterion. In other words, SELECT yields a horizontal subset of a table. 32

33 33

34 PROJECT yields all values for selected attributes. (Vertical subset of a table) 34

35 UNION combines all rows from two tables, excluding duplicate rows. The tables must have the same attribute characteristics (the columns and domains must be compatible) to be used in the UNION. When two or more tables share the same number of columns, and when their corresponding columns share the same domain, they are said to be union-compatible. 35

36 36

37 INTERSECT yields only the rows that appear in both tables which must be union-compatible. 37

38 DIFFERENCE yields all rows in one table that are not found in the other table; that is, it subtracts one table from the other. The table must be union-compatible. 38

39 PRODUCT yields all possible pairs of rows from two tables – also known as the Cartesian product. If one table has six rows and the other table has three rows, the PRODUCT yields a list composed of 6 x 3 = 18 rows. 39

40 40

41 Relational Algebra provides few types of JOIN.  Natural join – Links tables by selecting rows with common values in common attributes (join columns)  Equijoin – Links tables on the basis of an equality condition that compares specified columns.  Theta join – Any other comparison operator is used. 41

42  Inner join – Only returns matched records from the table that are being joined.  Outer join – Matched pairs are retained, and any unmatched values in other table are left null.  Left outer join  Right outer join 42

43 A natural join is the result of a three-stage process: a. First, a PRODUCT of the tables is created. b. Second, a SELECT is performed on the output of Step a to yield only rows for which the column values are equal. The common columns are referred to as the join columns. c. Last, a PROJECT is performed on the result of Step b to yield a single copy of each attribute, thereby eliminating duplicate columns. 43

44 Tables below will be used as an example to describe natural join. 44

45 1. PRODUCT 45

46 2. SELECT 3. PROJECT 46

47 DIVIDE operation uses one single-column (a) table as the divisor and one 2-column table (a & b) as the dividend. The table must have a common column. The output of the DIVIDE operation is a single column with the values of column (a) from the dividend table rows where the value of the common column in both tables (a) matches. 47

48 48

49 49

50 50

51 51

52 Lecture Slides by Tan Szu Tak, SICT, Politeknik Brunei


Download ppt "Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester 3 2014."

Similar presentations


Ads by Google