24
The Relational Data Model E.F Codd A Relational Data Model for Large Shared Data Banks (1970) The basic model used by Access, Oracle, DB2 All Relational DBMSs depart from the basic model

The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

  • View
    219

  • Download
    1

Embed Size (px)

Citation preview

Page 1: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

The Relational Data Model

E.F Codd A Relational Data Model for Large Shared Data Banks (1970)

The basic model used by Access, Oracle, DB2

All Relational DBMSs depart from the basic model

Page 2: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

ComponentsThe concepts available to represent the UoD

Relations (tables) of Tuples (rows) , of Columns (fields) containing values drawn from a

DomainBase Relations - factsDerived Relations -

Relations constructed by extracting, combining base relations

Page 3: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Relation

Deptno Dname Loc10 Accounting New York20 Research Dallas30 Sales Chicago40 Operations Boston

RelationRelation

tupletuple

Column (field)Column (field)

domaindomainstring

city name

general

specific

integer

0<int<99

Page 4: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Relations

There is only one data structure in the relational data model - the relation: • Every relation in a database must have a distinct name. • Every column in a relation must have a distinct name within the relation. • All entries in a column must be of the same kind.• The ordering of columns in a relation is not significant.• Each row in a relation must be distinct.• The ordering of rows is not significant.• Each cell or column/row intersection in a relation should contain only a so-called atomic value.

Page 5: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Primary Keys, Foreign Keys and Domains

Each relation must have a primary key. This is to enforce the property that duplicate rows are forbidden in a relation. A primary key is one or more columns of a table whose values are used to uniquely identify each of the rows in a table.

Foreign keys are the means of interconnecting the data stored in a series of disparate tables. A foreign key is a column or group of columns of some table which draws its values from the same domain as the primary key of some related table in the database.

Domains are pools of values from which actual values appearing in the columns of a table are drawn.

A special character is used in relational systems to indicate incomplete or unknown information - the character null. This character which is distinct from zero or space is particularly useful in the context of primary-foreign key links

Page 6: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Two approaches to defining a derived relation

Relational AlgebraDefine a new,

derived relation as a sequence of operations on Relations

Based on operational algebra

Relational CalculusDefines a new,

derived relation as a proposition which is true for every tuple in the new table

Based on predicate calculus

Page 7: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Algebra??

Algebra: x + 6 = 10 - what’s x?An Algebra:

a SET of values defined by INTENSIONe.g. whole numbers - 0,1,2,3,..

OPERATORS which combine values to compute new valuese.g. + * - / - (4+5)*6 = 120

Operators can only produce values in the original set - ‘closure’

Page 8: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Is it an Algebra??Whole numbers and + - * /

5 - 3 = 2 but 3 - 5 = -2 6/3 = 2 but 6/4 = 1.5

So not an algebra can redefine / as remainder e.g 6/4 = 1 - then / OK

Integers ( +ve and -ve) and + - * 8 - 9 = -1 and -8 - -9 = 1 3 * 4 = 12 and 3 * -4 = -12

Page 9: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

More Algebra

Operators act on different number of values 1 unary - 2 - + * /

Order may matter 2 + 3 = 3 + 2 but 6/3 not = 3/6

Brain teazer - make 13 from 2,3,5,7 ( (3 * 7) + 5 ) /2 = 13

Page 10: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Relational Algebra

Base operations RESTRICT, PROJECT, PRODUCT

Convenience operations EQUI-JOIN, (Natural) JOIN, Outer Joins

Set operations UNION, INTERSECTION, DIFFERENCE,

DIVISION

Page 11: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

The Relational Algebra

Restrict

Project

Join

Union

Intersect

Difference

Division

Page 12: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

EMP-DEPT example

Three relations:

Department : DEPT

Employee : EMP

Salary Grade : SALGRADE

Page 13: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

DEPT Table

deptno dname location

10 Accounting New York

20 Research Dallas

30 Sales Chicago

40 Operations Boston

Page 14: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

EMP - table ( reduced)Empno Ename Mgr Sal Deptno7369 SMITH 7902 £800.00 207499 ALLEN 7698 £1,600.00 307521 WARD 7698 £1,250.00 307566 JONES 7839 £2,975.00 207654 MARTIN 7698 £1,250.00 307698 BLAKE 7839 £2,850.00 307782 CLARK 7839 £2,450.00 107788 SCOTT 7566 £3,000.00 207839 KING £5,000.00 107844 TURNER 7698 £1,500.00 307876 ADAMS 7788 £1,100.00 207900 JAMES 7698 £950.00 307902 FORD 7566 £3,000.00 207934 MILLER 7782 £1,300.00 10

Page 15: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Salgrade Table

Grade Losal Hisal1 £700.00 £1,200.002 £1,201.00 £1,400.003 £1,401.00 £2,000.004 £2,001.00 £3,000.005 £3,001.00 £99,999.00

Page 16: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Restrict Subset of the Rows in a Table

RESTRICT EMP WHERE sal > 2000

empnoename mgr sal deptno7566 JONES 7839 £2,975.00 207698 BLAKE 7839 £2,850.00 307782 CLARK 7839 £2,450.00 107788 SCOTT 7566 £3,000.00 207839 KING £5,000.00 107902 FORD 7566 £3,000.00 20

Page 17: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Project: subset the Columns in a Table PROJECT EMP

[EMPNO, SAL,DEPTNO]

Empno sal Deptno7369 £800.00 207499 £1,600.00 307521 £1,250.00 307566 £2,975.00 207654 £1,250.00 307698 £2,850.00 307782 £2,450.00 107788 £3,000.00 207839 £5,000.00 107844 £1,500.00 307876 £1,100.00 207900 £950.00 307902 £3,000.00 207934 £1,300.00 10

Page 18: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Restrict-Project

RESTRICT EMP WHERE SAL >2000

PROJECT EMP[EMPNO, SAL, DEPTNO]

Could you reverse these operations - always? ( project then restrict?)

call this EMPX

empno sal deptno7566 £2,975.00 207698 £2,850.00 307782 £2,450.00 107788 £3,000.00 207839 £5,000.00 107902 £3,000.00 20

Page 19: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Product: Combine each row of one table with

each row of the other: PRODUCT DEPT with EMPX

empnosal EMPX. DEPT. Dname Loc Deptno Depno

7566 £2,975.00 20 10 Accounting New York7698 £2,850.00 30 10 Accounting New York7782 £2,450.00 10 10 Accounting New York7788 £3,000.00 20 10 Accounting New York7839 £5,000.00 10 10 Accounting New York7902 £3,000.00 20 10 Accounting New York7566 £2,975.00 20 20 Research Dallas7698 £2,850.00 30 20 Research Dallas7782 £2,450.00 10 20 Research Dallas7788 £3,000.00 20 20 Research Dallas7839 £5,000.00 10 20 Research Dallas7902 £3,000.00 20 20 Research Dallas

Page 20: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

7566 £2,975.00 20 30 Sales Chicago7698 £2,850.00 30 30 Sales Chicago7782 £2,450.00 10 30 Sales Chicago7788 £3,000.00 20 30 Sales Chicago7839 £5,000.00 10 30 Sales Chicago7902 £3,000.00 20 30 Sales Chicago7566 £2,975.00 20 40 Operations Boston7698 £2,850.00 30 40 Operations Boston7782 £2,450.00 10 40 Operations Boston7788 £3,000.00 20 40 Operations Boston7839 £5,000.00 10 40 Operations Boston7902 £3,000.00 20 40 Operations Boston

Page 21: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Product :

DEPT has 4 recordsEMPX has 6 records

so DEPT x EMPX has 24 records

but not very useful

Page 22: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Equi-Join :Product restricted to rows which have matching common domain

Empno sal EMPX. Dept. Dname Loc Deptno Deptno7566 £2,975.00 20 20 Research Dallas7698 £2,850.00 30 30 Sales Chicago7782 £2,450.00 10 10 Accounting New York7788 £3,000.00 20 20 Research Dallas7839 £5,000.00 10 10 Accounting New York7902 £3,000.00 20 20 Research Dallas

Page 23: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Natural Join:Equi-join projected with the duplicate column removed

empno sal deptno dname loc7566 £2,975.00 20 Research Dallas7698 £2,850.00 30 Sales Chicago7782 £2,450.00 10 Accounting New York7788 £3,000.00 20 Research Dallas7839 £5,000.00 10 Accounting New York7902 £3,000.00 20 Research Dallas

Page 24: The Relational Data Model zE.F Codd A Relational Data Model for Large Shared Data Banks (1970) zThe basic model used by Access, Oracle, DB2 zAll Relational

Basic SQL

SELECT * FROM EMP WHERE SAL > 2000;

SELECT ENAME,SAL,DEPTNO FROM EMP;

SELECT ENAME,SAL,DEPTNO FROM EMP WHERE SAL >

2000;

SELECT * FROM EMP, DEPT WHERE SAL > 2000;

SELECT * FROM EMP,DEPT WHERE SAL > 2000 AND

EMP.DEPTNO = DEPT.DEPTNO;

SELECT EMPNO, SAL, DEPTNO, DNAME FROM EMP,DEPT

WHERE SAL > 2000 AND EMP.DEPTNO = DEPT.DEPTNO;