Relational Data Model Sept. 2014Yangjun Chen ACS-39021 Outline: Relational Data Model Relational Data Model -relation schema, relations -database schema,

Slides:



Advertisements
Similar presentations
Database System - Assignment #3 Sept. 2012Yangjun Chen ACS Assignment #3 due Wed., Nov. 14, (30) Exercise 7.17 on Page 235 Show the result.
Advertisements

Defined by Edgar Codd in 1970 Defined by Edgar Codd in 1970 Considered ingenious but impractical Considered ingenious but impractical Conceptually simple.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
The Relational Algebra
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Database technology Lecture 2: Relational databases and SQL
Data Definition Languages Atif Farid Mohammad UNCC.
Exploring Microsoft Access 2003 Chapter 4 Proficiency: Relational Databases, External Data, Charts, Pivot, and the Switchboard.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
- relation schema, relations - database schema, database state
Review Database Application Development Access Database Development ER-diagram Forms Reports Queries.
Relational Model and Relational Algebra Rose-Hulman Institute of Technology Curt Clifton.
Chapter 3: The relational data Model, Relational Constraints Csci455 Hassan Reza, PhD. Copyright ©
Database: Review Sept. 2009Yangjun Chen ACS Database Introduction system architecture, Basic concepts, ER-model, Data modeling, B+-tree Hashing Relational.
관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5.
Chapter 5 Relational Model Concepts Dr. Bernard Chen Ph.D. University of Central Arkansas.
CS 380 Introduction to Database Systems (Chapter 5: The Relational Data Model and Relational Database Constraints)
1 Lecture 04 The relational data Model, Relational Constraints 1.
The Relational Data Model 1.Relational Model Concepts 2.Characteristics of Relations 3.Relational Integrity Constraints 3.1Key Constraints 3.2Entity Integrity.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints.
Review: Application of Database Systems
Database Management COP4540, SCS, FIU Relational Model Chapter 7.
Topic 5 The Relational Data Model and Relational Database Constraints Faculty of Information Science and Technology Mahanakorn University of Technology.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model.
10/9/20151 The Relational Data Model TCU Database Systems Last update: September 2004 Reference: Elmasri 4 th edition, chapter 5.
Relational Algebra - Chapter (7th ed )
CS 380 Introduction to Database Systems Chapter 7: The Relational Algebra and Relational Calculus.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Data Model and Relational Database Constraints by Pinar Senkul resources:
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
Retrieve the names of all employees in department 5 who work more than 10 hours per week on the ‘ProductX’ project. p10ssn ← (Π essn (σ hours > 10 (works-on.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Slide 6- 1 CARTESIAN (or cross) Product Operation Defines a relation Q that is the concatenation of every tuple of relation R with every tuple of relation.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Relational Model E.F. Codd at IBM 1970 Chapter 3 (ed. 7 – Chap. 5)
Review Database Application Development Access Database Development Theory Practice.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
7-1 Chapter 7 The Relational Data Model, Relational Constraints, and the Relational Algebra.
1Fundamentals of Database Systems 기본키에 밑줄을 그은 COMPANY 관계 데이타베이스 스키마 FNAMEMINITLNAMESSNBDATEADDRESSSEXSALARYSUPERSSNDNO EMPLOYEE DNAMEDNUMBERMGRSSNMGRSTARTDATE.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
An introduction to relational database RDB Sept
Chapter 71 The Relational Data Model, Relational Constraints & The Relational Algebra.
The Relational Data Model & Relational Algebra
Chapter (6) The Relational Algebra and Relational Calculus Objectives
The SQL Database Grammar
Database Design The Relational Model Text Ch5
376a. Database Design Dept. of Computer Science Vassar College
The Relational Data Model
Outline: Relational Data Model
Chapter (7) The relational Data Model, Relational Constraints, and the Relational Algebra Objectives Describe the basic principals of the relational model.
Joining Tables ضم الجداول وإستخراج مناظر views منها الهدف : 1- استخراج المعلومات من جدولين أو اكثر بالإستفادة من الرابط بينهما وبإستخدام SQL 2- شروط قواعد.
Company Requirements.
Review: Application of Database Systems
1. Explain the following concepts: (a) superkey (b) key
1.(5) Describe the working process with a database system.
1. Explain the following concepts of the ER data model:
Chapter (7) ER-to-Relational Mapping, and other Relational Languages
Presentation transcript:

Relational Data Model Sept. 2014Yangjun Chen ACS Outline: Relational Data Model Relational Data Model -relation schema, relations -database schema, database state -integrity constraints and updating Relational algebra -select, project, join, cartesian product division -set operations: union, intersection, difference

Relational Data Model Sept. 2014Yangjun Chen ACS dependent Dept_locations employee department project n n n n n n m Works on

Relational Data Model Sept. 2014Yangjun Chen ACS First introduced in 1970 by Ted Codd (IBM) A relation schema R, denoted by R(A 1, …, A n ), is made up of a relation name R and a list of attributes A 1, …, A n. A relation r(R) is a mathematical relation of degree n on the domains dom(A 1 ), dom(A 2 ), … dom(A n ), which is a subset of the Cartesian product of the domains that define R: r(R)  (dom(A 1 )  (dom(A 2 )  …  (dom(A n )) formal termsinformal relationtable tuplerow attributecolumn header domaindata type describing column values

Relational Data Model Sept. 2014Yangjun Chen ACS Cartesian product Emp(SSN, name, sex)  JDJD mfmf   JDJD  J), (1, D), (2, J), (2, D), (3, J), (3, D)}

Relational Data Model Sept. 2014Yangjun Chen ACS   JDJD  J, m), (1, D, m), (2, J, m), (2, D, m), (3, J, m), (3, D, m), (  J, f), (1, D, f), (2, J, f), (2, D, f), (3, J, f), (3, D, f)} Cartesian product  mfmf Emp(SSN, name, sex) 1 J m 2 D f

Relational Data Model Sept. 2014Yangjun Chen ACS Domain A domain is a set of atomic values from which values can be drawn Examples -social insurance numbers: set of valid 9-digit social insurance numbers -names: set of names of persons -grade point average: possible values of computed grade point averages; each must be a real number between 0 and 4.5.

Relational Data Model Sept. 2014Yangjun Chen ACS Domain In many systems one specifies a data type (e.g. integer, date, string(20), …) and writes supporting application code to enforce any specific constraints (e.g. a SIN must be a 9-digit number). Attribute An attribute A i is a name given to the role a domain plays in a relation schema R. Relation (or Relation State) A relation, or relation state, r of the relation schema R(A 1, A 2, … A n ) is a set of n-tuples r={t 1, t 2, … t m }, where each n-tuple is an ordered list of n values t i = (i = 1, …, m).

Relational Data Model Sept. 2014Yangjun Chen ACS Relation Schema example EMPLOYEE(Name, SSN, HomePhone, Address, OfficePhone, …) EMPLOYEE Relation example: EMPLOYEENameSSNHomePhoneAddress Benjamin Bayer Bluebonnet Lane... Katherine Ashly Kirby Road... Dick Davidson null 3452 Elgin Road... See Figure 6.1

Relational Data Model Sept. 2014Yangjun Chen ACS Some characteristics of relations no ordering of tuples each value in a tuple is atomic no composite values separate relation tuples for multivalued attributes some attributes may be null no value value missing/unknown a relation is an assertion e.g. an employee entity has a Name, SSN, HomePhone, etc each tuple is a fact or a particular instance some relations store facts about relationships

Relational Data Model Sept. 2014Yangjun Chen ACS instructor teachescourse jones smith Intro to X Z Intro to Y Advanced X Advanced Y jonesz Intro to XjonesAdvanced Xsmithz Intro to YsmithAdvanced Y

Relational Data Model Sept. 2014Yangjun Chen ACS Relational Database a relational database schema S is a set of relation schemas S = {R 1, R 2,...} and a set of integrity constraints IC. A relational database state DB of S is a set of relation states DB={r(R 1 ), r(R 2 ),...} such that... Figure a schema Figure a possible relational database state Figure RI constraints

Relational Data Model Sept. 2014Yangjun Chen ACS fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno Dname, dnumber, mgrssn, mgrstartdate Dnumber, dlocation Pname, pnumber, plocation, dnum Essn pno, hours Essn, dependentname, sex, bdate, relationship EMPLOYEE WORKS ON PROJECT DEPENDENT DEPARTMENT DEPT _LOCATIONS A database schema:

Relational Data Model Sept. 2014Yangjun Chen ACS fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno Dname, dnumber, mgrssn, mgrstartdate Dnumber, dlocation EMPLOYEE DEPARTMENT DEPT _LOCATIONS A database state: JohnBSmith FondrenM FranklinTWong VossM Research Houston Stafford r(EMPLOYEE) r(DEPARTMENT) r(DEPT_LOCATION)

Relational Data Model Sept. 2014Yangjun Chen ACS Integrity Constraints any database will have some number of constraints that must be applied to ensure correct data (valid states) 1. domain constraints a domain is a restriction on the set of valid values domain constraints specify that the value of each attribute A must be an atomic value from the domain dom(A). 2. key constraints a superkey is any combination of attributes that uniquely identify a tuple: t 1 [superkey]  t 2 [superkey]. -Example: (in Employee) a key is superkey that has a minimal set of attributes -Example: (in Employee)

Relational Data Model Sept. 2014Yangjun Chen ACS Integrity Constraints If a relation schema has more than one key, each of them is called a candidate key. one candidate key is chosen as the primary key (PK) foreign key (FK) is defined as follows: i) Consider two relation schemas R 1 and R 2 ; ii) The attributes in FK in R 1 have the same domain(s) as the primary key attributes PK in R 2 ; the attributes FK are said to reference or refer to the relation R 2 ; iii)A value of FK in a tuple t 1 of the current state r(R 1 ) either occurs as a value of PK for some tuple t 2 in the current state r(R 2 ) or is null. In the former case, we have t 1 [FK] = t 2 [PK], and we say that the tuple t 1 references or refers to the tuple t 2. Example: Employee(SSN, …, Dno)Dept(Dno, …, MGRSSN) FK

Relational Data Model Sept. 2014Yangjun Chen ACS Integrity Constraints 3. entity integrity no part of a PK can be null 4. referential integrity domain of FK must be same as domain of PK FK must be null or have a value that appears as a PK value 5. semantic integrity other rules that the application domain requires: state constraint: gross salary > net income transition constraint: Widowed can only follow Married; salary of an employee cannot decrease

Relational Data Model Sept. 2014Yangjun Chen ACS fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno Dname, dnumber, mgrssn, mgrstartdate Dnumber, dlocation Pname, pnumber, plocation, dnum Essn, pno, hours Essn, dependentname, sex, bdate, relationship EMPLOYEE DEPARTMENT DEPT _LOCATIONS WORKS_ON PROJECT DEPENDENT Figure 7-7: reference integrity

Relational Data Model Sept. 2014Yangjun Chen ACS dependent Dept_locations employee department project ERD for Chapter 6 database example n n n n n n m Works on

Relational Data Model Sept. 2014Yangjun Chen ACS Updating and constraints insert Insert the following tuple into EMPLOYEE: When inserting, the integrity constraints should be checked: domain, key, entity, referential, semantic integrity update Update the SALARY of the EMPLOYEE tuple with ssn = ‘ ’ to When updating, the integrity constraints should be checked: domain, key, entity, referential, semantic integrity

Relational Data Model Sept. 2014Yangjun Chen ACS Updating and constraints delete Delete the WORK_ON tuple with Essn = ‘ ’ and pno = 10. When deleting, the referential constraint will be checked. -The following deletion is not acceptable: Delete the EMPLOYEE tuple with ssn = ‘ ’ - reject, cascade, modify

Relational Data Model Sept. 2014Yangjun Chen ACS cascade – a strategy to enforce referential integrity  ssn  Employee Essn Pno    delete Works-on delete

Relational Data Model Sept. 2014Yangjun Chen ACS cascade – a strategy to enforce referential integrity Employee delete ssn supervisor    null  Employee delete ssn supervisor    null  delete not reasonable

Relational Data Model Sept. 2014Yangjun Chen ACS Modify – a strategy to enforce referential integrity  ssn  Employee Essn Pno    delete Essn Pno null   This violates the entity constraint. Works-on

Relational Data Model Sept. 2014Yangjun Chen ACS Modify – a strategy to enforce referential integrity  ssn  Employee delete This does not violate the entity constraint. Department  Dno   chairman Department null Dno   chairman

Relational Data Model Sept. 2014Yangjun Chen ACS Relational Algebra a set of relations a set of operations set operations relation specific select project join division union intersection difference cartesian product

Relational Data Model Sept. 2014Yangjun Chen ACS Relational algebra select horizontal subset project vertical subset join (equijoin, natural join, inner, outer) combine multiple relations cartesian product union, intersection, difference division

Relational Data Model Sept. 2014Yangjun Chen ACS Relational algebra - Select horizontal subset symbol:  boolean condition for row filter e.g. employees earning more than 30,000  salary>30000 (Employee) fname minit … salary... Franklin T … Jennifer S … James E … Every column of Employee appears in the result

Relational Data Model Sept. 2014Yangjun Chen ACS Relational algebra - Project vertical subset symbol:  e.g. names of employees  fname, minit, lname (Employee) fname minit lname John B Sarah Franklin T Wong AliciaJZalaya Jennifer S Wallace RameshKNarayan JoyceAEnglish Ahmad VJabbar James EBorg

Relational Data Model Sept. 2014Yangjun Chen ACS Relational algebra - Join join or combine tuples from two relations into single tuples symbol: boolean condition specifies the join condition e.g. to report on employees and their dependents Employee ssn=essn Dependent fname minit … essn dependent_name … All attributes of both employee and dependent will appear

Relational Data Model Sept. 2014Yangjun Chen ACS Essn dependent_name Alice Theodore Joy Abner Michael Alice Elizabeth Relational algebra - Join Employee ssn=essn Dependent fname minit … ssn Franklin T… Jennifer S… JohnB…

Relational Data Model Sept. 2014Yangjun Chen ACS fname minit ssn essn Franklin T Alice Franklin T … Theodore Franklin T … Joy Jennifer S … Abner John B … Michael John B … Alice John B … Elizabeth Employee ssn=essn Dependent... dependent_name

Relational Data Model Sept. 2014Yangjun Chen ACS Relational algebra - Join what is the result of Employee Dependent ? Note there is no join condition fname minit … essn dependent_name... If “Employee” contains 7 rows and “Dependent” contains 8 rows, there would be 8 times 7 = 56 rows in the result This is the Cartesian Product

Relational Data Model Sept. 2014Yangjun Chen ACS Relational algebra e.g. to report on employees and their dependents R1 Employee Dependent R2  ssn=essn (R1) Result fname minit lname dependent_name  fname, minit, lname, dependent_name (R2) Franklin T Wong Alice Franklin T Wong Theodore Franklin T Wong Joy Jennifer S Wallace Abner JohnB Smith Michael JohnB Smith Alice JohnB Smith Elizabeth

Relational Data Model Sept. 2014Yangjun Chen ACS Relational algebra - Join equijoin - one condition and the = operator natural join - an equijoin with removable of superfluous attribute(s). inner join - only tuples (in one relation) that join with at least one tuple (in the other relation) are included. This is what we have exhibited so far. outer join - full outer join, left outer join, right outer join

Relational Data Model Sept. 2014Yangjun Chen ACS Relational algebra - Natural join natural join - an equijoin with removable of superfluous attribute(s). E.g. to list employees and their dependents: employee * dependent has all attributes of employee, and all attributes of dependent minus essn, in the result if there is ambiguity regarding which attributes are involved, use a list notation like: employee * {ssn, essn} dependent

Relational Data Model Sept. 2014Yangjun Chen ACS Outer Joins join - only matching tuples are in the result left outer join - all tuples of R are in the result regardless... right outer join - all tuples of S are in the result regardless... full outer join - all tuples of R and S are in the result regardless... R R R R S S S S

Relational Data Model Sept. 2014Yangjun Chen ACS Left Outer Joins r1r2 B1=B2 a1b1  a3b3 a2b2 C c1 c3 null a1b1  a3b3 a2b2 r1 b1c1  C b4c4 b3c3 r2

Relational Data Model Sept. 2014Yangjun Chen ACS r1r2 a1b1  a3b3 a2b2 r1 b1c1  C b4c4 b3c3 r2 a1b1  null b4 a3b3 C c1 c4 c3 Right Outer Joins B1=B2

Relational Data Model Sept. 2014Yangjun Chen ACS Full Outer Joins r1r2 a1b1  a2b2 C c1 a3b3 c3 null B1=B2 nullb4c4 a1b1  a3b3 a2b2 r1 b1c1  C b4c4 b3c3 r2

Relational Data Model Sept. 2014Yangjun Chen ACS Outer Joins EmployeeDepartment ssn=mgrssn EmployeeDependent ssn=essn Project Works_on pno=pnumber ProjectWorks_on pno=pnumber Result: a list of all employees and also the department they manage if they happen to manage a department.

Relational Data Model Sept. 2014Yangjun Chen ACS Set difference, union, intersection A - B A  B A  B       A - B A  B A  B

Relational Data Model Sept. 2014Yangjun Chen ACS T  R S Division : a1b1  a2b1 a1b2 R a3b2 a3b1  b2 :  a1 a3  S T

Relational Data Model Sept. 2014Yangjun Chen ACS Division Query: Retrieve the name of employees who work on all the projects that ‘John Smith’ works on. SMITH   FNAME = ‘John’ and LNAME = ‘Smith’ (EMPLOYEE) SMITH_PNOs   PNO (WORK_ON ESSN = SSN SMITH) SSN_PNO   ESSN,PNO (WORK_ON) SSNS(SSN)  SSN_PNO : SMITH_PNOs RESULT   FNAME, LNAME (SSNS * EMPLOYEE)

Relational Data Model Sept. 2014Yangjun Chen ACS fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno Dname, dnumber, mgrssn, mgrstartdate Dnumber, dlocation Pname, pnumber, plocation, dnum Essn, pno, hours Essn, dependentname, sex, bdate, relationship EMPLOYEE DEPARTMENT DEPT _LOCATIONS WORKS_ON PROJECT DEPENDENT

Relational Data Model Sept. 2014Yangjun Chen ACS ssnfname lname    JohnSmith JohnSmith MarryBlack essn PNo hours        EMPLOYEE WORK_ON

Relational Data Model Sept. 2014Yangjun Chen ACS SMITH   FNAME = ‘John’ and LNAME = ‘Smith’ (EMPLOYEE) ssnfname lname   JohnSmith JohnSmith SMITH

Relational Data Model Sept. 2014Yangjun Chen ACS SMITH_PNOs   PNO (WORK_ON ESSN = SSN SMITH) ssnfnamelnameessnPNohours  John Smith    John Smith    John Smith   WORK_ON ESSN = SSN SMITH Pno SMITH_PNOs   

Relational Data Model Sept. 2014Yangjun Chen ACS SSN_PNO   ESSN,PNO (WORK_ON) essn PNo        SSN_PNO

Relational Data Model Sept. 2014Yangjun Chen ACS SSNS(SSN)  SSN_PNO : SMITH_PNOs essn PNo        SSN_PNO Pno SMITH_PNOs    :  ssn  SSNS(SSN)

Relational Data Model Sept. 2014Yangjun Chen ACS RESULT   FNAME, LNAME (SSNS * EMPLOYEE) ssnfname lname  MarryBlack RESULT

Relational Data Model Sept. 2014Yangjun Chen ACS Division The DIVISION operator can be expressed as a sequence of , , and - operations as follows: Z = {A 1, …, A n, B 1, …, B m }, X = {B 1, …, B m }, Y = Z - X = {A 1, …, A n }, R(Z) S(X) : T 1   Y ( R) T 2   Y ((S  T 1 ) - R) T  T 1 - T 2 result