fen 2014-02-061 concepts and terminology operations (relational algebra) integrity constraints...

22
FEN 2014-02-06 1 Concepts and terminology Operations (relational algebra) Integrity constraints The relational model

Upload: louise-cross

Post on 29-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

FEN 2014-02-06 1

Concepts and terminology

Operations (relational algebra)

Integrity constraints

The relational model

Page 2: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

2

Relational DatabasesAll data is organised in tables with atomic valuesAssociations are represented by primary key/foreign key connectionsEvery operation operates on tables and returns tables

FEN 2014-02-06

Small exercise (5 min..):• Describe step by step how you

will retrieve this information:• When did Mr Smith attend the

“Intro to Computer Science” course, who was the instructor and what grade did he receive?

Page 3: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

3

The Relational Model

A sound theoretical data model (Codd, 1970).Based on the mathematical theory of relations, sets and first order predicate logic.De facto standard since the late eighties.Many-many implementations – most SQL-based.

FEN 2014-02-06

The Notorious Supplier-Part Database (Date)

For instance:OracleMySQLMS SQL ServerPostgreSQL

Page 4: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

4

The Relational Model: Concepts

FEN 2014-02-06

Central concepts:Tables (relations).Columns (attributes).Type (domain).Rows (tuples). Tuples are unordered.Tuples are unique. A relation is a set (mathematical) of tuples.Primary and foreign keys

The Notorious Supplier-Part Database (Date)

Page 5: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

5

The Relational Model

Data is organised in a number of tables (relations).Each table has a number (>=1) columns (attributes).Attributes are atomic and defined over some domain.A table holds a number (maybe none) rows (tuples). Tuples are unordered.Tuples are unique (existence of a key is guaranteed). A relation is a set (mathematical) of tuples.

FEN 2014-02-06

Page 6: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

6

Attributes and Domains

A domain defines the valid value of an attribute.Domains are based on the built-in standard data types (int, char etc.) offered by the DBMS.Theoretically it should be possible to define problem specific domains as CPR numbers, account numbers, IP addresses etc. and complex aggregate (structured) domain as maps, diagrams, pictures, sound bites, video clips etc. More attributes may be defined over the same domain.An attribute may have the value “empty” (not known /not defined for this instance). Empty is notated NULL.

FEN 2014-02-06

Page 7: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

7

Properties of a Relation

Follows from the fact that relations are (mathematically) sets:

Tuples must be unique within a relation (hence a primary key always exists)Tuples are unordered (vertically)Attributes are unordered (horizontally)Attribute values are atomic

Note the difference to the usual notion of a table

FEN 2014-02-06

Page 8: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

8

Keys

A key is a combination of attributes that is:Unique andMinimal

An attribute combination that is unique, but not minimal is called a superkeyThe set of all attributes will always be a superkey, hence a superkey (and a key) always exists.A relation (table) may have several candidate keys.One these is appointed primary key.

FEN 2014-02-06

Any primary keys here?

Page 9: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

9

Associations Between Relations

Associations are represented by foreign keys.A foreign key is an attribute (combination) that corresponds to an attribute (combination) of the primary key of some other relation.A foreign key references a tuple in another relation and indicates that here is more information about the entity.Foreign key attributes and corresponding primary key attributes must be defined over compatible domains (normallly the same domain).

FEN 2014-02-06

Any foreign keys here?

Page 10: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

10

Integrity Constraints

FEN 2014-02-06

Domain constraintsAttributes may only hold valid values

Entity IntegrityPrimary key attributes may not hold NULL-values

Referential Integrity (foreign key constraint)A foreign key must either be NULL or reference an existing primary key in the other relation

Semantic IntegrityConstraints depending on the problem domain

Any constraints here?

Page 11: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

11

Example: MiniBank

Two tables:CustomersAccounts

Associated:An account belongs to one customer

FEN 2014-02-06

Association

Any constraints here?(primary keys – foreign keys)

Page 12: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

12

Example: MiniBank

What happens if:We try to insert a customer with an existing custNo?We try to insert an account with a not existing custNo?

Let’s try in MS SQL Server

FEN 2014-02-06

Page 13: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

13

Example: MiniBankTable definitions (schemas):

FEN 2014-02-06

Constraint

Page 14: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

14

Quering a relational database

Database Languages:Data Definition DDL

Should provide constructs for defining all the previous (as “create table)

Data Manipulation DML (queries, insert, delete, update)procedural (How?)nonprocedural (What?)The Relational Algebra is a procedural DMLSQL includes a (sort of) nonprocedural DML

FEN 2014-02-06

Page 15: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

15

The Relational Algebra

Data Manipulation in the Relational ModelOperates on relations, which are input to the operations is tables and the result is a tableOperations

Row selection (RESTRICT/SELECT)Column selection (PROJECT)Combining tables (JOIN)Set operations (UNION, INTERSECTION, DIFFERENCE, PRODUCT)More advanced operations (OUTER (LEFT/RIGTH) JOIN)

FEN 2014-02-06

Page 16: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

16

Relational Algebra - Overview

FEN 2014-02-06

Page 17: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

17

Example: MiniBankRetrieve information about customer number 3:

Row selection on custNo = 3 from Customer

Retrieve account number, balance and customer number for accounts with a balance between 1000 and 2000:

Row selection on 1000 <= balance and balance <= 2000 from AccountColumn selection on accNo, balance, custNo

Retrieve information about customer Tommy and his accounts:

Row selection on name = ‘Tommy’ from CustomerJoin with Account on custNo

FEN 2014-02-06

What if we have more than one customer called ‘Tommy’?

And what if we have no ‘Tommy’?

Page 18: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

18

Informal Terms Formal Terms

Table Relation

Column Attribute/Domain

Row Tuple

Values in a column Domain

Table Definition Schema of a Relation

Definitions - Summary

FEN 2014-02-06

Page 19: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

19

Another example: Company:

FEN 2014-02-06

Page 20: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

20

Company: Sample data

FEN 2014-02-06

Page 21: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

21

Company: Sample data – cont…

FEN 2014-02-06

Page 22: FEN 2014-02-061  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model

22

Company: Exercise

FEN 2014-02-06

Do exercise 1 onCompanyExercise.pdf(Exercise 2, phase 1 and 2)