CIS 671Query Languages1 CIS 671 Introduction to Database Systems II Introduction S. Parthasarathy.

Slides:



Advertisements
Similar presentations
Logical DB Design: ER to Relational Entity sets to tables. Employees ssn name lot CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY.
Advertisements

The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Relational Model (CB Chapter 4) CPSC 356 Database Ellen Walker Hiram College.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
The Relational Model System Development Life Cycle Normalisation
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
Database Architecture The Relational Database Model.
EER vs. UML Terminology EER Diagram Entity Type Entity Attribute
CS 380 Introduction to Database Systems (Chapter 5: The Relational Data Model and Relational Database Constraints)
Chapter 3 Relational Model Chapter 4 in Textbook.
Databases Illuminated
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
Relational Model Session 6 Course Name: Database System Year : 2012.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Chapter 4 The Relational Model.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
CG084&085 / / 1 The Relational Data Model Properties of Relations Keys and Constraints.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
10/9/20151 The Relational Data Model TCU Database Systems Last update: September 2004 Reference: Elmasri 4 th edition, chapter 5.
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
Chapter 6 The Relational Data Model and the Relational Algebra.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
Logical Database Design (1 of 3) John Ortiz Lecture 6Logical Database Design (1)2 Introduction  The logical design is a process of refining DB schema.
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.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
ER/EER to Relational Data Model 1 Database Design.
The Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
Mapping ER to Relational Model Each strong entity set becomes a table. Each weak entity set also becomes a table by adding primary key of owner entity.
CSCI 6315 Applied Database Systems Review for Midterm Exam I Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Chapter 71 The Relational Data Model, Relational Constraints & The Relational Algebra.
COP Introduction to Database Structures
The Relational Data Model & Relational Algebra
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
COP Introduction to Database Structures
ER Modeling Exercise Consider a set of courses, both at grad and undergrad level. Each course has at least one section. Each section is taught by only.
Translation of ER-diagram into Relational Schema
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4+17 The Relational Model Pearson Education © 2014.
The Relational Model Transparencies
Database Design: Relational Model
INSTRUCTOR: MRS T.G. ZHOU
Presentation transcript:

CIS 671Query Languages1 CIS 671 Introduction to Database Systems II Introduction S. Parthasarathy

CIS 671Query Languages2 CIS 671 Course Outline Review of basic database material (CIS 670) Object-Oriented and Object Relational Database Systems Data Warehousing and Data Mining Active Databases Introduction to Storage Structures Emerging Technologies

CIS 671Query Languages3 Relations Relation schema R or R(A 1, A 2, …, A n ) fixed set of attributes A 1, A 2, …, A n each attribute A j corresponds to exactly one of the underlying domains D j (j = 1, 2, …, n), not necessarily distinct. Example: EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) note: EMPNO and MGR take values from the same domain, the set of valid employee numbers N, the number of attributes in the relation scheme is called the degree of the relation. Degree 1unarydegree 3ternary degree 2binarydegree nn-ary

CIS 671Query Languages4 Keys A relation is a set of tuples. All elements of a set are distinct. Hence all tuples must be distinct. There may also be a subset of the attributes with the property that values must be distinct. Such a set is called a superkey. SK a set of attributes t 1 and t 2 tuples t 1 [SK]  t 2 [SK] Guaranteed by the “real world”.

CIS 671Query Languages5 Candidate Key -a minimal superkey A set of attributes, CK is a superkey, but no proper subset is a superkey. Example EMPNO NAME ??? Primary Key - one arbitrarily chosen candidate key. Example EMPNO

CIS 671Query Languages6 Theoretical Properties of Relations Tuples are unordered. There are no tuple duplicates. Attributes are unordered All attribute values are atomic. Implementations vary in terms of how closely they follow the above properties.

CIS 671Query Languages7 Relational Database Schema S Set of Relation Schemas S = {R 1, R 2, …, R p } Set of integrity constraints, IC –Integrity constraints will be discussed next.

CIS 671Query Languages8 Relational Database (Instance) DB of Schema S Set of relation instances, DB DB = {r 1, r 2, …, r p } Each r k is an instance of relation RK The r k ’s satisfy the integrity constraints, IC

CIS 671Query Languages9 Database Example - Schema EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) EMPNO from domain EmployeeNumbers... Integrity constraints to be added DEPT( DNO, DNAME, LOC) DNO from domain DepartmentNumber Integrity constraints to be added

CIS 671Query Languages10 Database Example - Database (instance) EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) 1234SmithD2J K JonesD2J K 4567BlueD1J K 9999GreenD3J5100K DEPT( DNO, DNAME, LOC) D1xxxyyy D2aaayyy D3bbbzzz

CIS 671Query Languages11 Relational Integrity Constraints Not included in Codd’s original definition of relations. Not supported in initial commercial relational database systems. Now supported in all major relational database systems.

CIS 671Query Languages12 Integrity Rules: Applicable to All Databases Key constraint Entity integrity Referential integrity

CIS 671Query Languages13 The Three Integrity Rules Key constraint Candidate keys are specified for each relation schema. Candidate key values must be unique for every tuple in any relation instance of that schema.

CIS 671Query Languages14 The Three Integrity Rules, Continued Entity integrity: –No attribute participating in the primary key is allowed to accept null values. Justification: we must be able to identify each tuple uniquely.

CIS 671Query Languages15 Foreign Keys EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) DEPT( DNO, DNAME, LOC) DNO in EMP -should be allowed only if that DNO appears as primary key in relation DEPT. MGR in EMP -should be allowed only if that MGR appears as primary key in relation EMP. The attributes DNO and MGR called foreign keys in relation EMP.

CIS 671Query Languages16 The Three Integrity Rules, Continued Referential integrity: –If relation schema R 1 includes a foreign key, FK, matching the primary key of relation schema R 2, then a value of FK in a tuple t 1 of R 1 must either Be equal to the value of PK in some tuple t 2 of R 2, i.e. T 1 [FK] = t 2 [PK], or Be wholly null (i.e. Each attribute value participating in that FK value must be null). Note: R 1 and R 2 need not be distinct. Justification: if some tuple t 1 references some tuple t 2, then tuple t 2 must exist.

CIS 671Query Languages17 Implications of the Three Integrity Rules System could reject the operation. System could accept the operation, but perform some additional operations so as to reach a new legal state. What should happen if an operation on the database is about to cause the violation of one of the integrity rules? i.e. about to put the database into an “illegal” state.

CIS 671Query Languages18 Query Languages Review Srinivasan Parthasarathy

CIS 671Query Languages19 SQL - Parts of the Language Data Definition Language (DDL) –create table –create index Data Manipulation Language (DML) –select (retrieve) –update –insert –delete

CIS 671Query Languages20 Select - Basic Form (select from where) Cartesian product followed by select and project. selectproject-list fromCartesian-product-list whereselect-condition(s) Abstract example: Given tables R(A,B) and S(B,C) selectR.A, R.B, S.C fromR, S whereR.A > 10 BUT - Duplicates NOT eliminated. Bag vs. Set.

CIS 671Query Languages21 EMP( EMPNO, …,DNO,JOB,...) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician CASE STUDY EXAMPLE

CIS 671Query Languages22 Select as a JOIN Cartesian product followed by select (“join” & “select” conditions) and project. selectproject-list fromCartesian-product-list wherejoin-condition andselect-condition Abstract example: Given tables R(A,B) and S(B,C) selectR.A, R.B, S.C fromR, S whereR.B = S.B/* join condition */ andR.A > 10/* “select” condition */ How is this related to these relational operators? Select (  ), Project (  ), Join: Natural (*)

CIS 671Query Languages23 Using EMP and DEPT From Relational Algebra to SQL List the names, employee numbers, department numbers and locations for all clerks. selectNAME, EMPNO, E.DNO, LOC fromEMP E, DEPT D whereE.DNO = D.DNO/* join condition */ andJOB = ‘Clerk’/* “select” condition */. EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) DEPT( DNO, DNAME, LOC)

CIS 671Query Languages24 Duplicates in project - must use explicit distinct List the different department numbers in the EMP table (eliminate duplicates). select distinctDNO fromEMP Specify sort order List employee number, name, and salary of employees in department 50. selectEMPNO, NAME, SAL fromEMP whereDNO = 50 order byEMPNO

CIS 671Query Languages25 Union List the numbers of those departments which have an employee named ‘Smith’ or are located in ‘Columbus’. selectDNO fromEMP whereENAME = ‘Smith’ union selectDNO fromDEPT whereLOC = ‘Columbus’ Duplicates ARE eliminated by default. union all - leaves duplicates

CIS 671Query Languages26 Functions and Groups List the departments (DNO) and the average salary of each. selectDNO, avg(SAL) fromEMP E, DEPT D whereE.DNO = D.DNO group byDNO List the departments (DNO, DNAME) in which the average employee salary < $25,000. selectDNO, DNAME fromEMP E, DEPT D whereE.DNO = D.DNO group byDNO, DNAME havingavg(SAL) < 25000

CIS 671Query Languages27 Nested Select: No analog in Relational Algebra List names of employees in departments 25, 47 and 53. selectNAME fromEMP whereDNO in (25, 47, 53) List names of employees who work in departments in Ann Arbor. selectNAME fromEMP whereDNO in (selectDNO fromDEPT whereLOC = ‘Ann Arbor’ )

CIS 671Query Languages28 Null Values All of the following conditions are always false. null > 25null 25 null >= 25null null However we can use the following: selectNAME fromEMP whereSAL < orSAL is null

CIS 671Query Languages29 selectDNO fromDEPT where exists (select * fromEMP ED3 whereED3.DNO = ‘D3’ and exists (select * from EMP EY whereEY.JOB = ED3.JOB andEY.DNO = DEPT.DNO)) exists,  Q.Find the numbers of those departments that have employees who can do some job that is done by an employee in department D3. Answer: D1, D2, D3 The order of the two “selects” does not matter.

Ok, lets see why this works Since there is an external reference to DEPT within the second Nested select we will execute the nested select for each DEPT Tuple. Assume the DEPT table contains only three tuples corresponding to D1, D2 and D3 in that order. The first tuple we will evaluate is the DEPT.DNO = D1 The first nested select will simply highlight the tuples in department D3. The second nested select will point to tuples related to the tuple currently pointed to within the department table.

ED3.EMPNO, ED3.DNO,ED3.JOB,..) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician EY.EMPNO, EY.DNO,EY.JOB,..) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician selectDNO fromDEPT where exists (select * fromEMP ED3 whereED3.DNO = ‘D3’ and exists (select * from EMP EY whereEY.JOB = ED3.JOB andEY.DNO = DEPT.DNO))

CIS 671Query Languages32 for all,  EMP( EMPNO, …,DNO,JOB,...) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician Q.Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3. Answer: D1, but not D2

CIS 671Query Languages33 selectDNO fromDEPT where for all (select * fromEMP ED3 whereED3.DNO = ‘D3’ and exists (select * from EMP EY whereEY.JOB = ED3.JOB andEY.DNO = DEPT.DNO)) Q.Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3. Answer: D1,D3, but not D2 However no for all exists in SQL.

CIS 671Query Languages34 selectDNO fromDEPT where not exists (select * fromEMP ED3 whereED3.DNO = ‘D3’ and not exists (select * from EMP EY whereEY.JOB = ED3.JOB andEY.DNO = DEPT.DNO)) Q.Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3. Answer: D1,D3, but not D2 However no for all exists in SQL. Use two not exists. (see page 207 for a good list of mathematical logic operations/tricks)

Ok, lets see why this works Since there is an external reference to DEPT within the second Nested select we will execute the nested select for each DEPT Tuple. Assume the DEPT table contains only three tuples corresponding to D1, D2 and D3 in that order. The first tuple we will evaluate is the DEPT.DNO = D1 The first nested select will simply highlight the tuples in department D3. The second nested select will point to tuples related to the tuple currently pointed to within the department table.

ED3.EMPNO, ED3.DNO,ED3.JOB,..) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician EY.EMPNO, EY.DNO,EY.JOB,..) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician selectDNO fromDEPT where not exists (select * fromEMP ED3 whereED3.DNO = ‘D3’ and not exists (select * from EMP EY whereEY.JOB = ED3.JOB andEY.DNO = DEPT.DNO))

CIS 671Query Languages37 selectDNO fromDEPT where not exists (select * fromEMP ED3 whereED3.DNO = ‘D3’ and not exists (select * from EMP EY whereEY.JOB = ED3.JOB andEY.DNO = DEPT.DNO) ) and DNO <> ‘D3’ Q12.Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3. Answer: D1, but not D2 Eliminate department D3.

CIS 671Query Languages38 SQL:1999 (SQL 3) Recursive Closure Q 16.List all the superiors of EMPNO , 950, 980 Q 17.List all those supervised by EMPNO , 800, 100, 200, 300, 400 How to express these queries?

CIS 671Query Languages39 with recursive SUPERIORS(EMPNO, MGR) as (select EMPNO, MGR from EMP where EMPNO = 500 union all select SUPERIORS.EMPNO, EMP.MGR from SUPERIORS, EMP where EMP.EMPNO = SUPERIOR.MGR) select MGR from SUPERIORS Q 16.Given EMP (EMPNO, MGR,...), list all the superiors of EMPNO 500. Generate SUPERIORS (EMPNO, MGR) Just the superiors600 of Initial table The recursion

CIS 671Query Languages40 Q 16. List all the superiors of EMPNO 500. SUPERIORS ( EMPNO, MGR ) Initial table Second addition First addition EMP(EMPNO,MGR,...)

CIS 671Query Languages41 Entity-Relationship (ER) Model (Peter P.-S. Chen) Review CIS 671 S. Parthasarathy

CIS 671Query Languages42 Helpful for conceptualizing the Real World Entity: a thing that exists –e.g. person, automobile, department, employee Entity Set: a group of similar entities –e.g. all persons, all automobiles, all employees Relationship: association between entities –e.g. a person is assigned to a department Relationship Set : set of similar relationships Attribute: property of an entity or relationship –e.g. person - name, address Domain: set of values allowed for an attribute

CIS 671Query Languages43 Example EmployeesE#, ENAME, ADDRESS DepartmentsD#, DNAME ProjectsP#, PNAME Constraints (cardinality) 1. Employees may be assigned to only 1 department at a time. 2. Employees may be assigned to several projects at once, each with an associated %time. Constraints (participation) 3. Employees must be assigned to a department. 4. Employees need not be assigned to any projects.

CIS 671Query Languages44 Complete Picture Project Assigned To Employee Department Is In %TIME N 1 M N D#DNAME P# E#ENAMEADDRESSPNAME

CIS 671Query Languages45 Example: Relationships and Attributes Project Employee Assigned To Employee Department Is In %TIME N 1 M N Employees may be assigned to several projects at once, each with an associated %time. 1.Employees may be assigned to only 1 department at a time.

CIS 671Query Languages46 Example: Relationships and Attributes Project Employee Assigned To Employee Department Is In %TIME N 1 M N 4.Partial 3. Total 3.Employees must be assigned to a department. Total: Each entity must be included at least once in the relationship. 4.Employees need not be assigned to any projects. Partial: Each entity instance need not be included at least once in the relationship.

CIS 671Query Languages47 Entity-Relationship Enhancements: Attributes 1. Simple (atomic) vs. Composite Attributes –Simple –Composite Employee LName FName Name E# LName FName

CIS 671Query Languages48 2. Single-valued vs. Multi-valued Attributes –Multi-valued –Multi-valued as Entity Entity-Relationship Enhancements: Attributes Student Major_Program Major Student Has Major N M

CIS 671Query Languages49 Entity-Relationship Enhancements: Attributes 3. Derived Attributes - Include in Department the average salary of the employees in the department. EmployeeDepartment SalaryAvgSal Member Of

CIS 671Query Languages50 Name Entity-Relationship Enhancements: Entities Weak Entity Type, Identifying Relationship Type, Partial Key E.g. Represent all the dependents of each employee given his or her name. No ID number. Problem: Names are not unique across employees. Employee Dependents Of Dependent NameSSN 1N

CIS 671Query Languages51 Complete Picture Project Assigned To Employee Department Is In %TIME N 1 M N D#DNAME P# E#ENAMEADDRESSPNAME

CIS 671Query Languages52 As Relations Entities –Department(D#, DNAME) –Employee(E#, ENAME, ADDRESS) –Project(P#, PNAME) Relationships –Is_In(E#, D#)1:N –Assigned_To(E#, P#, %TIME)N:M

CIS 671Query Languages53 As Relations: Replacing Employee and Is_In with Employee’ Entities –Department(D#, DNAME) –Employee’(E#, ENAME, ADDRESS, D#) –Project(P#, PNAME) Relationships –Assigned_To(E#, P#, %TIME)N:M –[Is_In(E#, D#)]

CIS 671Query Languages54 Enhanced-ER (EER) Model Subclasses & Superclasses –Specialization & Generalization –Type Inheritance Categories

CIS 671Query Languages55 Superclasses & Subclasses Generalization & Specialization Inheritance Example: University Database Person Employee Student Grad Student Undergrad Student SSNSalaryNameClassEmpID Degree Program Major Dept do U U U U d disjoint o overlap total partial U superclasssubclass Superclass instance must always exist.

CIS 671Query Languages56 Example: Meeting Locations MeetingLocation Place Park Organization Date Day* MeetsAt Capacity Room Number* Name* Street Address Time* Organization Name* NorthSouth Coordinates Park Name* EaseWest Coordinates Building IsIn Room U U U union Categories