Relational data integrity 1 Lecture 8 Relational data integrity.

25
Relational data integrity 1 Lecture 8 Relational data integrity

Transcript of Relational data integrity 1 Lecture 8 Relational data integrity.

Page 1: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

1

Lecture 8

Relational data integrity

Page 2: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

2

Outline

integrity constraints and data definition candidate keys foreign keys nulls domains conditional expressions normal forms

Page 3: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

3

Constraints

in real life systems constraints exist between data values

• it would be useful to communicate these constraints to the database system

data is associated with a meaning• stating some constraints on data describing a part of

the meaning

90% should be spent on integrity constraints definition

Page 4: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

4

Examples of possible inaccuracies

PersonsID Name DoB Income Department1 M. Jackson 22/12/1960 34000 MM013 P$%ffY780&&& 01/04/2099 28000 MM013 F. Mercury 07/07/1957 -50000 Dev10

DepartmentsDepartment Name No_of_employeesMM01 Manufacturing management 3PPP Personnel 4

?

?

how would you express, in NL, integrity constraints that would avoid the above situations?

Page 5: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

5

Types of integrity constraints

integrity constraint application specific integrity constraints

examples mechanisms

• domains• conditional expressions• normal forms

generic (inherent to the relational model - resulting from definitions)

• entity integrity : primary key• referential integrity : foreign key

Page 6: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

6

Data definition

integrity constraints definition in a relational language should include primary key definition candidate keys definition foreign key definition - including foreign key rules conditional expressions

Page 7: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

7

Example - SQL data definition (in brief)

CREATE TABLE <relation name> (@<attribute definition><conditional expression>,<primary key definition>,@<candidate key definition>,@<foreign key definition>,@<conditional expressions>

) ;<primary key definition> ::= PRIMARY KEY ( <set of attributes> )<candidate key definition > ::= CANDIDATE KEY ( <set of attributes> )<foreign key definition> ::= FOREIGN KEY ( <set of attributes> )

REFERENCES <relation name> ON DELETE <option> ON UPDATE <option>

CREATE ASSERTION <name> CHECK <conditional expression>

Page 8: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

8

Candidate key - example

Registration-no Name DOB Tel-no Course …

one candidate keyother candidate keys

Page 9: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

9

Candidate key

candidate key uniqueness property irreducibility property

entity integrity constraint simple/composite primary/alternate

Page 10: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

10

Foreign key - example

Address SName CourseLondon M. Jagger CISLondon S. Smiths CISYork S. Smiths MCSLeeds S. Bruce MASBath J. Kelly MAS

SName Course ModuleM. Jagger CIS LanguagesM. Jagger CIS DatabasesM. Jagger CIS CompilersS. Smiths CIS LanguagesS. Smiths CIS DatabasesS. Smiths CIS AIS. Smiths MCS LanguagesS. Smiths MCS CalculusJ. Kelly MAS CalculusJ. Kelly MAS Statistics

StudentsRegistrations

Page 11: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

11

Foreign key

foreign key (FK) corresponding candidate key (CK) in another relation FK CK such that FK = CK (reverse not required)

target/referenced relation/tuple | referring relation/tuple

referential integrity constraint foreign keys and PostgreSQL

Page 12: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

12

Referential diagram

Students Tutors

Registrations Teaching

Modules

Page 13: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

13

Foreign key rules - motivation

Type Name MinDep Interestcurr current 100 2.4s-sav short savings 1000 3.3l-sav long savings 10000 4.5… … … …

Name No Type BalanceM. Smith 0099455321 curr -342.22M. Smith 1011334522 s-sav 1500M. Jagger 0099677432 curr 235.99S. Hunt 1011377856 s-sav 2300… … … …

Page 14: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

14

What happens if ...

short saving accounts are not offered by the bank anymore?

the “code” and name for small saving accounts is to be changed to ‘i-sav’ and ‘instant saving’ respectively?

the interest for s-sav is to be decreased by 0.3%?

Page 15: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

15

Foreign key rules

the modifications are performed in the REFERRED relation

rules• ON DELETE RESTRICT• ON DELETE CASCADE• ON UPDATE RESTRICT• ON UPDATE CASCADE

split into four groups: think of one example for each situation; don’t use the study guide

Page 16: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

16

Examples

on delete restrict• students and books

on delete cascade• employees and children

on update restrict• students and modules

on update cascade• employees and departments

Page 17: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

17

Nulls

Name DOB Sex Course Year1 Year2 Year3A. Johnson 04/66 M CIS 68 72 NULLS. Bruce 02/72 F NULL NULL NULL NULLP. Harris 10/73 NULL MAS 0 48 NULL

Page 18: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

18

Nulls

representing missing/unavailable information primary key and nulls foreign key and nulls

Page 19: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

19

Domains

expressing integrity constraints on scalar values

• constraints on permissible scalar values• constraints on the applicability of scalar operators

SQL • does not support domains• offers other mechanisms

Page 20: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

20

Domains - example of integrity constraints

Page 21: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

21

Conditional expressions

will be studied with SQL pointer forward:

the university’s database:“a student has two choose two options (1/2cu courses) in the final year; if the students chooses an extra optional course than the final year project will count as only 1/2cu”

Page 22: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

22

FDs, MDs, and JDs

particular constraints functional dependencies multiple dependencies join dependencies

expressed by means of normal forms extensively studied in the following lectures

Page 23: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

23

Normal forms - example

ID Name Department YearsInService Qualification SalaryD1 M. Rick Development 5 programmer 28000D2 F. Boyle Development 5 programmer 28000D3 P. Wale Development 5 programmer 28000D4 M. Grin Development 6 analyst 32000D5 F. Weller Development 8 analyst 41000D6 S. Strauss Development 8 analyst 41000F1 P. Johnson Finance 9 accountant 28000

Employees

redundant data

Page 24: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

24

Integrity constraints - further considerations

state and transition integrity constraints• example

when and how are the integrity constraints applied depends on the type

• ‘normal forms’ and ‘domains’ - always

• entity and referential - after each transaction

• conditional expressions– default : after each transaction– other possibilities?

• correct database = the logical AND of the set of integrity constraints is satisfied after each transaction

Page 25: Relational data integrity 1 Lecture 8 Relational data integrity.

Relational data integrity

25

Summary

the relational data model• data objects• operators• integrity constraints

SQL implements the relational model• the subject of the next lectures

you know what a relational model is, but do not know yet how to design one