Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8 Database Theory & Practice (2) : The Relational Data Model UFCEKG-20-2 Data, Schemas & Applications.

Similar presentations


Presentation on theme: "Lecture 8 Database Theory & Practice (2) : The Relational Data Model UFCEKG-20-2 Data, Schemas & Applications."— Presentation transcript:

1 Lecture 8 Database Theory & Practice (2) : The Relational Data Model UFCEKG-20-2 Data, Schemas & Applications

2 Origins of the Relational Model o The relational model was developed by EF Codd in the early 1970s. o Commercial systems based on the relational model appeared in the late 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). 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 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.

3 Components and terminology (1) The model uses terminology taken from mathematics, particularly 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 relation o attribute - a named column of a relation o domain - the set of allowable values for one or more attributes o degree of a relation - the number of attributes it contains o cardinality of relation - the number of tuples it contains.

4 Components and terminology (2)

5 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 must have a distinct name. o Attribute (column) values of a relation are atomic (i.e. 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). o The ordering of tuples (rows) and attributes (columns) is not significant.

6 Relational algebra & relational calculus o Relational algebra (ra) and relational calculus (rc) are both formal (mathematically based) languages defined by EF Codd. o ra & rc are logically equivalent languages. ra is “procedural” and rc is “declarative” in nature. o ra and rc are the formal grounding of the relational database 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 set of relations closed under operators. Operators operate on one or more relations to yield a relation.first-order logicrelationsclosedoperators 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” property.

7 Relational algebra operators (1) o Each relational operator takes one or more relations as its input and produces a new relation as output (closure). Codd originally defined eight operators, in two classes: Set operators:UNIONINTERSECTION DIFFERENCEDIVIDE The special relational operators: RESTRICTPROJECT JOINCartesian PRODUCT

8 Relational algebra operators (2)

9 Relational algebra operators (3) dept – emp – salgrade example (1) Department : dept (depno, dname, location) Employee : emp (empno, ename, mgr, sal, deptno) Salary Grade : salgrade (grade, losal, hisal)

10 Relational algebra operators (4) dept – emp – salgrade example (2) o dept table deptnodnamelocation 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston

11 Relational algebra operators (5) dept – emp – salgrade example (3) empnoenamemgrsaldeptno 7369SMITH7902 £800.0020 7499ALLEN7698£1,600.0030 7521WARD7698£1,250.0030 7566JONES7839£2,975.0020 7654MARTIN7698£1,250.0030 7698BLAKE7839£2,850.0030 7782CLARK7839£2,450.0010 7788SCOTT7566£3,000.0020 7839KING£5,000.0010 7844TURNER7698£1,500.0030 7876ADAMS7788£1,100.0020 7900JAMES7698 £950.0030 7902FORD7566£3,000.0020 7934MILLER7782£1,300.0010 o emp table

12 Relational algebra operators (6) dept – emp – salgrade example (4) gradelosalhisal 1£700.00£1,200.00 2£1,201.00£1,400.00 3£1,401.00£2,000.00 4£2,001.00£3,000.00 5£3,001.00£99,999.00 o salgrade table

13 Relational algebra operators (7) dept – emp – salgrade example (5) o Restrict => s ubset of the Rows in a Table RESTRICT EMP WHERE sal > 2000 empnoenamemgrsaldeptno 7566JONES7839£2,975.0020 7698BLAKE7839£2,850.0030 7782CLARK7839£2,450.0010 7788SCOTT7566£3,000.0020 7839KING£5,000.0010 7902FORD7566£3,000.0020

14 Relational algebra operators (8) dept – emp – salgrade example (6) o Project => subset the Columns in a Table PROJECT EMP [EMPNO, SAL,DEPTNO] empnosal deptno 7369£800.0020 7499£1,600.0030 7521£1,250.0030 7566£2,975.0020 7654£1,250.0030 7698£2,850.0030 7782£2,450.0010 7788£3,000.0020 7839£5,000.0010 7844£1,500.0030 7876£1,100.0020 7900£950.0030 7902£3,000.0020 7934£1,300.0010

15 Relational algebra operators (9) dept – emp – salgrade example (7) o Restrict-Project RESTRICT EMP WHERE SAL >2000 PROJECT EMP[EMPNO, SAL, DEPTNO] empnosaldeptno 7566£2,975.0020 7698£2,850.0030 7782£2,450.0010 7788£3,000.0020 7839£5,000.0010 7902£3,000.0020 call this EMPX Could you reverse these operations - always? ( project then restrict?)

16 Relational algebra operators (10) dept – emp – salgrade example (8) o Product => combine each row of one table with each row of the other PRODUCT DEPT with EMPX empnosalEMPX. deptno dept. Depno dnameloc 7566£2,975.002010AccountingNew York 7698£2,850.003010AccountingNew York 7782£2,450.0010 AccountingNew York 7788£3,000.002010AccountingNew York 7839£5,000.0010 AccountingNew York 7902£3,000.002010AccountingNew York 7566£2,975.0020 ResearchDallas 7698£2,850.003020ResearchDallas 7782£2,450.001020ResearchDallas 7788£3,000.0020 ResearchDallas 7839£5,000.001020ResearchDallas 7902£3,000.0020 ResearchDallas

17 Relational algebra operators (11) dept – emp – salgrade example (9) 7566£2,975.002030SalesChicago 7698£2,850.0030 SalesChicago 7782£2,450.001030SalesChicago 7788£3,000.002030SalesChicago 7839£5,000.001030SalesChicago 7902£3,000.002030SalesChicago 7566£2,975.002040OperationsBoston 7698£2,850.003040OperationsBoston 7782£2,450.001040OperationsBoston 7788£3,000.002040OperationsBoston 7839£5,000.001040OperationsBoston 7902£3,000.002040OperationsBoston continued from last page :

18 Relational algebra operators (12) dept – emp – salgrade example (10) o Product (Cartesian product) DEPT has 4 records EMPX has 6 records so DEPT x EMPX has 24 records but not very useful

19 Relational algebra operators (13) dept – emp – salgrade example (11) o Equi-Join => product restricted to rows which have matching common domain empno salEMPX. deptno dept. deptno dnameloc 7566£2,975.0020 ResearchDallas 7698£2,850.0030 SalesChicago 7782£2,450.0010 AccountingNew York 7788£3,000.0020 ResearchDallas 7839£5,000.0010 AccountingNew York 7902£3,000.0020 ResearchDallas

20 Relational algebra operators (14) dept – emp – salgrade example (12) o Natural Join => equi-join projected with the duplicate column removed empno saldeptnodnameloc 7566£2,975.0020ResearchDallas 7698£2,850.0030SalesChicago 7782£2,450.0010AccountingNew York 7788£3,000.0020ResearchDallas 7839£5,000.0010AccountingNew York 7902£3,000.0020ResearchDallas

21 Basic SQL o 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 = DEPT.DEPTNO; o SELECT EMPNO, SAL, DEPTNO, DNAME FROM EMP,DEPT WHERE SAL > 2000 AND EMP.DEPTNO = DEPT.DEPTNO;

22 Bibliography / Readings Bibliography -An Introduction to Database Systems (8 th ed.), C J Date, Addison Wesley 2004An Introduction to Database Systems -Database Management Systems, P Ward & G Defoulas, Thomson 2006Database Management Systems Readings -Introduction to SQL, McGraw-Hill/Osbourne (handout)


Download ppt "Lecture 8 Database Theory & Practice (2) : The Relational Data Model UFCEKG-20-2 Data, Schemas & Applications."

Similar presentations


Ads by Google