22
UFCEKG202 Lecture 8 Data, Schemas & Applications Database Theory & Practice (2) : The Relational Data Model

UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

UFCEKG‐20‐2 

Lecture 8Data, Schemas & Applications

Database Theory & Practice (2) : The Relational Data Model

Page 2: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Origins of the Relational Modelo The relational model was developed by EF Codd in the early 1970s. o Commercial systems based on the relational model appeared in the 

late 1970slate 1970s. o At present there are several hundred relational DBMSs and most 

computer vendors support 'relational' software. o Examples of well‐known products include Oracle, DB2, Sybase, 

MySQL, MS.SQL Server and MS Access.

Informally, a relational system is a system in which:1. The data is perceived by the user as tables (and nothing but tables).tables).2. The operators available to the user for (e.g.) retrieval are operators that derive “new” tables from "old" ones. For example, there is one operator restrict which extract a subset of the rows ofthere is one operator, restrict, which extract a subset of the rows of a given table, and another, project, which extracts a subset of columns ‐ and a row subset and a column subset of a table can both be regarded in turn as tables in their own right.g g

Mar 2013 2N. H. N. D. de Silva

Page 3: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Components and terminology (1)Components and terminology (1)The model uses terminology taken from mathematics, particularly set theory and predicate logicparticularly set theory and predicate logic. Basic terminology used in relational theory includes:

o relation ‐ this corresponds to a table or flat file with columns and rows

o tuple ‐ a row of a relationo tuple a row of a relationo attribute ‐ a named column of a relationo domain ‐ the set of allowable values for one or more 

battributeso degree of a relation ‐ the number of attributes it contains o cardinality of relation ‐ the number of tuples it containso cardinality of relation ‐ the number of tuples it contains.

Mar 2013 3N. H. N. D. de Silva

Page 4: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Components and terminology (2)Components and terminology (2)

Mar 2013 4N. H. N. D. de Silva

Page 5: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Properties of relations

o There is only one data structure in the relational data model ‐ the relation.

o Every relation and every attribute within a relation musto Every relation and every attribute within a relation must have a distinct name.

o Attribute (column) values of a relation are atomic (i.e. i l l d)single valued). 

o All values in an attribute (column) are taken from same domain.

o The ordering of columns in a relation is not significant.o Duplicate tuples (rows) are not allowed (e.g. each row in a 

relation must be distinct)relation must be distinct).o The ordering of tuples (rows) and attributes (columns) is 

not significant.

Mar 2013 5N. H. N. D. de Silva

Page 6: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra & relational calculusRelational algebra & relational calculuso Relational algebra (ra) and relational calculus (rc) are both 

formal (mathematically based) languages defined by EFformal (mathematically based) languages defined by EF Codd. 

o ra & rc are logically equivalent languages. ra is g y q g g“procedural” and rc is “declarative” in nature.

o ra and rc are the formal grounding of the relational d b d l d ill h b i idatabase model and illustrate the basic operations required by any data manipulation language such as SQL.

o Relational algebra is an offshoot of first‐order logic is a seto Relational algebra is an offshoot of first order logic, is a set of relations closed under operators. Operators operate on one or more relations to yield a relation. 

o The “closure” property relates to the fact that from any given relational operation another relation is output  ‐ this is often referred to as the “relations in – relations out”is often referred to as the  relations in  relations outproperty.

Mar 2013 6N. H. N. D. de Silva

Page 7: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (1)Relational algebra operators (1)

o Each relational operator takes one or more relations as pits input and produces a new relation as output (closure). Codd originally defined eight operators, in two classes:

Set operators: UNION INTERSECTIONSet operators: UNION INTERSECTIONDIFFERENCE DIVIDE

The special relational operators:

RESTRICT PROJECToperators:

JOIN Cartesian PRODUCTPRODUCT

Mar 2013 7N. H. N. D. de Silva

Page 8: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (2)Relational algebra operators (2)

Mar 2013 8N. H. N. D. de Silva

Page 9: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (3)dept – emp – salgrade example (1)

Department : dept (depno, dname, location)p p ( p , , )Employee : emp (empno, ename, mgr, sal, deptno)Salary Grade : salgrade (grade, losal, hisal)y g (g , , )

Mar 2013 9N. H. N. D. de Silva

Page 10: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (4)g p ( )dept – emp – salgrade example (2)

o dept table

deptno dname location10 Accounting New York20 Research Dallas30 Sales Chicago40 Operations Boston

Mar 2013 10N. H. N. D. de Silva

Page 11: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (5)( )dept – emp – salgrade example (3)

l d t

o emp tableempno ename mgr sal      deptno

7369 SMITH 7902 £800.00 20

7499 ALLEN 7698 £1,600.00 30

7521 WARD 7698 £1,250.00 30

7566 JONES 7839 £2,975.00 20

7654 MARTIN 7698 £1 250 00 307654 MARTIN 7698 £1,250.00 30

7698 BLAKE 7839 £2,850.00 30

7782 CLARK 7839 £2,450.00 10

7788 SCOTT 7566 £3,000.00 20

7839 KING £5,000.00 10

7844 TURNER 7698 £1 500 00 307844 TURNER 7698 £1,500.00 30

7876 ADAMS 7788 £1,100.00 20

7900 JAMES 7698 £950.00 30

7902 FORD 7566 £3,000.00 20

7934 MILLER 7782 £1,300.00 10Mar 2013 11N. H. N. D. de Silva

Page 12: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (6)g p ( )dept – emp – salgrade example (4)

l d t bl

grade losal hisal

o salgrade table

g

1 £700.00 £1,200.00

2 £1 201 00 £1 400 002 £1,201.00 £1,400.00

3 £1,401.00 £2,000.00

4 £2 001 00 £3 000 004 £2,001.00 £3,000.00

5 £3,001.00 £99,999.00

Mar 2013 12N. H. N. D. de Silva

Page 13: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (7)g p ( )dept – emp – salgrade example (5)

o Restrict  => subset of the Rows in a TableRESTRICT  EMP  WHERE sal > 2000 

empno ename mgr sal deptno7566 JONES 7839 £2 975 00 207566 JONES 7839 £2,975.00 207698 BLAKE 7839 £2,850.00 307782 CLARK 7839 £2,450.00 10,7788 SCOTT 7566 £3,000.00 207839 KING £5,000.00 107902 FORD 7566 £3,000.00 20

Mar 2013 13N. H. N. D. de Silva

Page 14: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (8)d l d l (6)dept – emp – salgrade example (6)

o Project => subset the Columns in a TablePROJECT EMP [EMPNO SAL DEPTNO]PROJECT EMP [EMPNO, SAL,DEPTNO]

empno sal deptno

7369 £800.00 207369 £800.00 20

7499 £1,600.00 30

7521 £1,250.00 30

7566 £2 975 00 207566 £2,975.00 20

7654 £1,250.00 30

7698 £2,850.00 30

7782 £2,450.00 10

7788 £3,000.00 20

7839 £5,000.00 10

7844 £1,500.00 30

7876 £1,100.00 20

7900 £950.00 30

7902 £3,000.00 20

7934 £1,300.00 10Mar 2013 14N. H. N. D. de Silva

Page 15: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (9)g p ( )dept – emp – salgrade example (7)

o Restrict‐ProjectRESTRICT  EMP  WHERE SAL >2000PROJECT EMP[EMPNO, SAL, DEPTNO]

empno sal deptno7566 £2 975 00 207566 £2,975.00 207698 £2,850.00 307782 £2,450.00 10,7788 £3,000.00 207839 £5,000.00 107902 £3,000.00 20

call this EMPXcall this EMPXCould you reverse these operations ‐ always?  ( project then restrict?)

Mar 2013 15N. H. N. D. de Silva

Page 16: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (10)g p ( )dept – emp – salgrade example (8)

o Product => combine each row of one table with each row of the othero Product > combine each row of one table with each row of the otherPRODUCT DEPT with  EMPX

empno sal EMPX. dept. dname locdeptno Depno

7566 £2,975.00 20 10 Accounting New York

7698 £2,850.00 30 10 Accounting New York

7782 £2,450.00 10 10 Accounting New York

7788 £3,000.00 20 10 Accounting New York

7839 £5,000.00 10 10 Accounting New York

7902 £3,000.00 20 10 Accounting New York

7566 £2,975.00 20 20 Research Dallas

7698 £2,850.00 30 20 Research Dallas,

7782 £2,450.00 10 20 Research Dallas

7788 £3,000.00 20 20 Research Dallas

7839 £5 000 00 10 20 Research Dallas7839 £5,000.00 10 20 Research Dallas

7902 £3,000.00 20 20 Research DallasMar 2013 16N. H. N. D. de Silva

Page 17: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (11)g p ( )dept – emp – salgrade example (9)

continued from last page : 

7566 £2,975.00 20 30 Sales Chicago

7698 £2,850.00 30 30 Sales Chicago

p g

7698 £2,850.00 30 30 Sales Chicago

7782 £2,450.00 10 30 Sales Chicago

7788 £3,000.00 20 30 Sales Chicago

7839 £5 000 00 10 30 Sales Chicago7839 £5,000.00 10 30 Sales Chicago

7902 £3,000.00 20 30 Sales Chicago

7566 £2,975.00 20 40 Operations Boston

7698 £2,850.00 30 40 Operations Boston

7782 £2,450.00 10 40 Operations Boston

7788 £3,000.00 20 40 Operations Boston

7839 £5,000.00 10 40 Operations Boston

7902 £3,000.00 20 40 Operations Boston

Mar 2013 17N. H. N. D. de Silva

Page 18: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (12)g p ( )dept – emp – salgrade example (10)

o Product (Cartesian product)

DEPT has 4 recordsDEPT has 4 recordsEMPX has 6 records

so  DEPT x EMPX has 24 records

b t t f lbut not very useful

Mar 2013 18N. H. N. D. de Silva

Page 19: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (13)g p ( )dept – emp – salgrade example (11)

o Equi‐Join =>product restricted to rows which have matching common domain

empno sal EMPX. deptno            

dept. deptno

dname loc

7566 £2 975 00 20 20 Research Dallas7566 £2,975.00 20 20 Research Dallas

7698 £2,850.00 30 30 Sales Chicago

7782 £2,450.00 10 10 Accounting New York, g

7788 £3,000.00 20 20 Research Dallas

7839 £5,000.00 10 10 Accounting New York

7902 £3,000.00 20 20 Research Dallas

Mar 2013 19N. H. N. D. de Silva

Page 20: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

Relational algebra operators (14)g p ( )dept – emp – salgrade example (12)

o Natural Join =>equi‐join projected with the duplicate column removed

empno sal  deptno             dname loc

7566 £2,975.00 20 Research Dallas

7698 £2,850.00 30 Sales Chicago

7782 £2,450.00 10 Accounting New York

7788 £3 000 00 20 Research Dallas7788 £3,000.00 20 Research Dallas

7839 £5,000.00 10 Accounting New York

7902 £3,000.00 20 Research Dallas

Mar 2013 20N. H. N. D. de Silva

Page 21: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

B i SQLBasic SQL

SELECT * FROM EMP WHERE SAL 2000o SELECT * FROM EMP WHERE SAL > 2000;

o SELECT ENAME,SAL,DEPTNO FROM EMP;

o SELECT ENAME,SAL,DEPTNO FROM EMP WHERE SAL > 2000;

o SELECT * FROM EMP, DEPT WHERE SAL > 2000;

o SELECT * FROM EMP,DEPT WHERE SAL > 2000 AND EMP.DEPTNO =o SELECT   FROM EMP,DEPT WHERE SAL > 2000 AND EMP.DEPTNO   

DEPT.DEPTNO;

o SELECT EMPNO, SAL, DEPTNO, DNAME FROM EMP,DEPT WHERE SAL  > 

2000 AND EMP.DEPTNO = DEPT.DEPTNO;2000 AND EMP.DEPTNO   DEPT.DEPTNO;

Mar 2013 21N. H. N. D. de Silva

Page 22: UFCEKG 20 2 Data, Schemas Applicationsix.cs.uoregon.edu/~nisansa/Classes/02_Northshore... · Relational algebra operators (()4) dept–emp–salgradeexample (2) o dept table deptno

bl h / dBibliography / Readings

BibliographyBibliography‐ An Introduction to Database Systems (8th ed.), C J Date, Addison Wesley 

2004Database Management Systems P Ward & G Defoulas Thomson 2006‐ Database Management Systems, P Ward & G Defoulas, Thomson 2006

Readingsd ll/ b (h d )‐ Introduction to SQL, McGraw‐Hill/Osbourne (handout)

Mar 2013 22N. H. N. D. de Silva