Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIS 671Query Languages1 CIS 671 Introduction to Database Systems II Introduction S. Parthasarathy."— Presentation transcript:

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

2 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

3 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

4 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”.

5 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

6 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.

7 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.

8 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

9 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

10 CIS 671Query Languages10 Database Example - Database (instance) EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) 1234SmithD2J1456730K10 2222JonesD2J2456725K 4567BlueD1J4999940K 9999GreenD3J5100K DEPT( DNO, DNAME, LOC) D1xxxyyy D2aaayyy D3bbbzzz

11 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.

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

13 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.

14 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.

15 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.

16 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.

17 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.

18 CIS 671Query Languages18 Query Languages Review Srinivasan Parthasarathy

19 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

20 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.

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

22 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 (*)

23 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)

24 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

25 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

26 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

27 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’ )

28 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 < 35000 orSAL is null

29 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.

30 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.

31 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))

32 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

33 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.

34 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)

35 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.

36 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))

37 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.

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

39 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 500.950 980 Initial table The recursion

40 CIS 671Query Languages40 Q 16. List all the superiors of EMPNO 500. SUPERIORS ( EMPNO, MGR ) Initial table Second addition First addition EMP(EMPNO,MGR,...) 100700 200700 300800 400800 500600 600950 700900 800900 850950 900980 950980980 500600 500950 500980

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

42 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

43 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.

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

45 CIS 671Query Languages45 Example: Relationships and Attributes Project Employee Assigned To Employee Department Is In %TIME N 1 M N 2. 1. 2. 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.

46 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.

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

48 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

49 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

50 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

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

52 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

53 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#)]

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

55 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.

56 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


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

Similar presentations


Ads by Google