Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 2 RELATIONAL DATA MODEL. DEFINITION OF DATABASE Shared collection of logically related data (and a description of this data), designed to meet.

Similar presentations


Presentation on theme: "CHAPTER 2 RELATIONAL DATA MODEL. DEFINITION OF DATABASE Shared collection of logically related data (and a description of this data), designed to meet."— Presentation transcript:

1 CHAPTER 2 RELATIONAL DATA MODEL

2 DEFINITION OF DATABASE Shared collection of logically related data (and a description of this data), designed to meet the information needs of an organization.

3 RDBMS Relational Database Management System (RDBMS) is a database base on the relational model developed by Dr. E.F. Codd of IBM Research in 1970. A DBMS that manages the relational database.

4 The model represent data in form of table.

5 COMPONENTS & CHARACTERISTICS OF RDBMS RELATION Is a table with column and rows A relation(table) consists of unique attributes (column) and tuples (rows) ATTRIBUTE (FIELD) An attribute is named column of relation Used to hold information about the objects to be represented in database RECORD (TUPLE) A record is a row of a relation

6 COMPONENTS & CHARACTERISTICS OF RDBMS DOMAIN A domain is the set of allowable values for one or more attributes DEGREE The degree of relation is the number of attributes it contains Relationship degree: The number of entities associated with the relationship.

7 CARDINALITY The cardinality of relation is the number of rows it contain. Relationship cardinality: Express the specific number of entity occurrences associated with occurrences of the related entity In data modelling terms, cardinality is how one table relates to another. 1-1 (1:1) (one row in table A relates to one row in tableB) 1-Many (1:m) (one row in table A relates to many rows in tableB) Many-Many (m:n) (Many rows in table A relate to many rows in table B)

8 COMPONENTS & CHARACTERISTICS OF RDBMS

9 RELATION INSTANCE Relation: made up of 2 parts: 1. SCHEMA : specifies name of relation, plus name and type of each column E.G. Student(sid: char(10), name: char(20), age: integer, gpa: real, login: char(20)). 2. INSTANCE : a table of values in rows and columns Relation instance is a set of rows (tuples, records) As a set, it is not supposed to have duplicates!

10 RELATION KEYS There are no duplicate tuples within a relation. Therefore, we need to be able to identify one or more attributes (called relational keys) that uniquely identifies each tuple in a relation. There are several types of relational keys; Primary Key Candidates Key Foreign Key

11 RELATION KEYS PRIMARY KEY is a column (or columns) in a table that uniquely identifies the rows in that table. For example, in the table above, CustomerNo is the primary key. The values placed in primary key columns must be unique for each row: no duplicates can be tolerated. In addition, nulls are not allowed in primary key columns.

12 RELATION KEYS CANDIDATES KEY is a column that meets all of the requirements of a primary key. In other words, it has the potential to be a primary key.

13 RELATION KEYS FOREIGN KEY are columns that point to primary key columns. So, for example, OrderNo is the primary key of the table ORDERS below and CustomerNo is a foreign key that points to the primary key in the CUSTOMERS table.

14

15 Integrity Rules Rules of constraints or restrictions that apply to all instances of the database. A feature provided by relational database management system (RDBMS), which prevents users or applications from entering inconsistent data. There are 2 types of integrity rules; Entity Integrity Referential Integrity

16 INTEGRITY RULES ENTITY INTEGRITY No attribute of a primary key can be null. If we allow null for any part of primary key, we are implying that not all the attributes are needed to distinguish between tuples. NULL Null mean that no value has yet been supplied. Null not the same with zero numeric value or text string filled with spaces; zeros and spaces are values A null represent the absence of a value.

17 INTEGRITY RULES REFERENTIAL INTEGRITY This rule applies to a foreign keys. If a foreign key exist in a relation, either the foreign key value must match a candidate key value of some tuples in its home relation or the foreign key value must be wholly null. A table which has a foreign key referring to its own candidate key is known as self-referencing table.

18 DATABASE DESIGN TECHNIQUE ER diagram is widely used in database design –Represent conceptual level of a database system –Describe things and their relationships ERD is a top-down approach to database design. ERD consists 3 basic concepts: - Entity - Attribute - Relationship

19 Entity Relational database design starts by defining the required entities Entities are set of object you want to store data about – people (students, customers, employees, etc.) – places (resorts, cities, countries, restaurants etc.) – things (products, invoices, movies, paintings, books, buildings, contracts, etc.) – events (elections, presentations, earthquakes, etc.) Represented by a rectangular box Entity Name

20 Attribute Attribute hold values that describe the properties of entities  E.g. student entity could have attributes such as name, address, DOB etc. Each entity has one or more attributes associated with it. Represented by a ellipse Attribute Name

21 Types of AttributesDefinitionExample Simple attributeCannot be divided into simpler components Gender of the employee Composite attributeCan be split into components Address of the employee Single valuedCan take on only a single value for each entity instance Age of the employee Multi valuesCan take up many valuesSkill set of the employee Stored attributeAttribute that need to be stored permanently Date of joining of the employee Derived attributeAttribute that can be calculated based on other attributes Years of the service of the employee Type of Attributes

22 Composite Attribute EMPLOYEE EmpNo EmpName Designatio n DOB Building Street Address Poscod e State

23 MULTI VALUED ATTRIBUTE EMPLOYEE EmpNo EmpName Designatio n DOB Building Street Address Poscod e State Skill

24 STORED & DERIVED ATTRIBUTE EMPLOYEE EmpNo EmpName Designatio n Building Street Address Poscod e State DateEmploye d YearsEmploye d

25 ATTRIBUTES OF A RELATIONSHIP VENDORRAW MATERIALS Supplie s VendorID VendorNam e VendorAddress MaterialID MaterialNam e StandardCost SupplyUnitPri ce

26 RELATIONSHIP Relationship specify the relations among entities from two or more entity sets Relationship specify the relations among entities from two or more entity sets Describe a meaningful interaction between entity. Describe a meaningful interaction between entity. Represented by a diamond Relationship Name

27 Degree of Relationship Relationship degree: The number of entities associated with the relationship. Degree: the number of entity types involved One Unary Two Binary Three Ternary

28 Types of Relationship Degrees 1. Unary Relationship – exist when an association is maintained within a single entity - also known as RECURSIVE RELATIONSHIP

29 Types of Relationship Degrees 2. Binary Relationship – exist when 2 entities are associated

30 Types of Relationship Degrees 2. Ternary Relationship – exist when 3 entities are associated

31 RELATIONAL MODEL RELATIONSHIPS Relationship Cardinality: Express the specific number of entity occurrences associated with occurrences of the related entity There are 3 types of relationships; One to One (1:1) One to Many (1:M) Many to One (M:1) Many to Many (M:N) Eg; Employee is assigned to parking place (1:1) Department offers course (1:M) Employee work in department (M:1) Employee is assigned to project (M:N)

32 RELATIONAL MODEL RELATIONSHIPS ONE TO ONE (1:1) One occurrence of an entity relates to only one occurrence in another entity AB 11

33 RELATIONAL MODEL RELATIONSHIPS ONE TO MANY (1:M) – One occurrence in an entity relates to many occurrences in another entity. A B1B1 B2B2 B3B3 M1

34 RELATIONAL MODEL RELATIONSHIPS MANY TO ONE (M:1) many occurrences in an entity relate to one occurrences in another entity A B2B2 B3B3 B1B1 1M

35 RELATIONAL MODEL RELATIONSHIPS MANY TO MANY (M:N) many occurrences in an entity relate to many occurrences in another entity B3B3 B2B2 B1B1 A1A1 A2A2 MN

36 STEPS IN ER MODELING:  Identify the entities  Find the relationship  Identify the key attributes for every entities  Identify other relevant attributes  Draw complete ER-modelling with all attributes including primary key

37 RELATIONAL ALGEBRA Relational algebra is a (high-level) procedural language associated with the relational model. Basic operations in relational algebra: Unary Relational Operations: Selection Projection Cartesian product Union Set Difference Intersection Binary Relational Operations Division operations Join

38 RELATIONAL ALGEBRA

39 5 BASIC OPERATIONS Selection ( ) Selects a subset of rows from relation (horizontal). Projection ( ) Retains only wanted columns from relation (vertical). Cross-product (x) Allows us to combine two relations. Set-difference (–) Tuples in r1, but not in r2. Union (  ) Tuples in r1 and/or in r2.

40 Example Instances R1 S1 S2 Boats

41 SELECT operation (  ) Used to select a subset of the tuples from a relation that satisfies a selection condition Symbol -> σ (sigma) Syntax : σ ( R )

42 SELECT operation (  )

43 Eg1: Select the STUDENT tuples whose Semester is 3. Eg2: Select the MANAGER tuples whose Salary is greater than RM25,000. Eg3: Select the tuples for all SUPERVISOR who either work in IT Department and Salary less than RM20,000 per year, or work in HR Department and Salary less than RM30,000. σ Sem=3 (STUDENT) σ Salary>25000 (MANAGER) σ (Dept=“IT” AND Salary<20000) OR Dept=“HR” AND Salary<30000)) (SUPERVISOR)

44 PROJECT operation Select certain columns from the table and discard the other columns Symbol -> Л (pi) Syntax – Л (R)

45 PROJECT operation S2

46 PROJECT operation Eg1: List each student’s Regs.No, First Name and Last Name. Eg2: List IDNum, Name and Age of all supervisors who work in IT department. Л Regs.No, Fname, Lname (STUDENT) Л IDNum,Name,Age (σDept=“IT” (SUPERVISOR))

47 UNION operation UNION compares tuples in two relations and create a new relation that contains some of the tuples from each of the input relations. Symbol -> υ (cup) R υ S

48 UNION operation S1 S2

49 UNION operation Eg1: List all IDNum where there is either a Manager or Supervisor for IT Department. Eg2: List Regs.No for students those register at Department and Hostel. Л IDNum(σDept=“IT” (MANAGER)) υ Л IDNum(σDept=“IT” (SUPERVISOR)) Л Regs.No (DEPARTMENT) υ Л Regs.No (HOSTEL)

50 CROSS PRODUCT operation Defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S. Denoted by X R X S

51 CROSS PRODUCT operation R1 S1 R1 X S1

52 CARTESIAN PRODUCT operation Eg1: List the Regs.No, Name and NRIC of all students who registred CourseID, CourseName for course. Л Regs.No,Name,NRIC (Student) X ( Л Regs.No,CourseID, CourseName (Course))

53 INTERSECTION operation Defines a relation consisting of the set of all tuples that are in both R and S. Symbol -> ∩ (cap) R ∩ S

54 INTERSECTION operation S1 S2

55 INTERSECTION operation Eg1: List Regs.No for students those having register at both Department and Hostel. Л Regs.No (DEPARTMENT) ∩ Л Regs.No (HOSTEL)

56 SET DIFFERENCE operation Defines a relation consisting of the tuples that are in relation R, but not in S. Symbol -> - (minus) R - S

57 SET DIFFERENCE operation S1 S2 c S2 – S1

58 SET DIFFERENCE operation Eg1: List Regs.No for students those register at Department but no at Hostel. Л Regs.No(σcourse=“DNS3A” (DEPARTMENT)) – Л Regs.No(σcourse=“DNS3A” (HOSTEL))

59 JOIN operation Compute R X S Select rows where attributes that appear in both relations have equal values Project all unique atttributes and one copy of each of the common ones.

60 NATURAL JOIN operation R1 S1 c R1 S1

61 Natural Join The tuples in the resulting relation are obtained by combining tuples in the operands with equal values on the common attributes. The common attributes often form a key of one of the operands (remember: references are realized by means of foreign keys, and we join in order to follow references)

62 OUTER JOIN “Pads With Nulls” A variant of the join, to keep all pieces of information from the operands. Three Variants : Left – only tuples of the left operand are padded. Right – only tuples of the right operand are padded. Full – tuples of both operands are padded

63 R1 R2 EmployeeDepartment SmithSales BlackProduction WhiteProduction DepartmentHead ProductionMori PurchasingBrown

64 OUTER LEFT JOIN R1 LEFT R2 EMPLOYEEDEPARTMENTHEAD SmithSalesNull BlackProductionMori WhiteProductionMori

65 OUTER RIGHT JOIN R1 RIGHT R2 EMPLOYEEDEPARTMENTHEAD BlackProductionMori WhiteProductionMori NullPurchasingBrown

66 OUTER FULL JOIN R1 RIGHT R2 EMPLOYEEDEPARTMENTHEAD SmithSalesNull BlackProductionMori WhiteProductionMori NullPurchasingBrown


Download ppt "CHAPTER 2 RELATIONAL DATA MODEL. DEFINITION OF DATABASE Shared collection of logically related data (and a description of this data), designed to meet."

Similar presentations


Ads by Google