Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS530 Database Architecture Models and Design Prof. Ian HORROCKS Dr. Robert Stevens Wednesday - Practical Tables.

Similar presentations


Presentation on theme: "CS530 Database Architecture Models and Design Prof. Ian HORROCKS Dr. Robert Stevens Wednesday - Practical Tables."— Presentation transcript:

1 CS530 Database Architecture Models and Design Prof. Ian HORROCKS Dr. Robert Stevens Wednesday - Practical Tables

2 CS530 - Ian Horrocks and Robert Stevens27/09/2004 2 In this Section… ­ Topics Covered –Functional Dependencies –Normalisation –SQL Data Defn and Manipulation –SQL Query ­Examples Classes

3 CS530 - Ian Horrocks and Robert Stevens27/09/2004 3 Informal guidelines  Semantics of the attributes –easy to explain relation –doesn’t mix concepts  Reducing the redundant values in tuples  Choosing attribute domains that are atomic  Reducing the null values in tuples  Disallowing spurious tuples

4 CS530 - Ian Horrocks and Robert Stevens27/09/2004 4 Functional Dependencies

5 CS530 - Ian Horrocks and Robert Stevens27/09/2004 5 Functional Dependency  an attribute A is functionally dependent on a set of attributes X if and only if –value of A is determined solely by the values of X –values of X uniquely determine a value of A child → mother The value of child implies the value of mother Value of mother does NOT imply value of child Child is the determinant Mother is the dependent/determined mother → child X → A

6 CS530 - Ian Horrocks and Robert Stevens27/09/2004 6 Our case study example studno name given family hons slot labmark exammark STUDENT SCHOOL YEAR ENROL YEARREG REG TUTOR YEARTUTOR STAFF COURSE courseno subject equip name year faculty appraiser appraisee APPRAISAL TEACH m n 1 m 1 1 1 m n m m 1 m 1 roomno STUDENT(studno,givenname,familyname, hons,tutor,slot,year) studno → studno, givenname, familyname, hons tutor, slot, year ENROL(studno,courseno,labmark,exammark) studno, courseno → labmark, exammark COURSE(courseno,subject,equip) courseno → courseno, subject, equip STAFF(lecturer,roomno,appraiser) lecturer → lecturer, roomno, appraiser roomno → lecturer, appraiser, roomno YEAR(year,yeartutor) year → year, yeartutor yeartutor → year, yeartutor SCHOOL(hons,faculty) hons → hons, faculty TEACH(courseno,lecturer) courseno, lecturer → courseno, lecturer

7 CS530 - Ian Horrocks and Robert Stevens27/09/2004 7 More Examples of Functional Dependency part_ number part_ description quantity_in _stock studno courseno labmark name tutor roomno subject

8 CS530 - Ian Horrocks and Robert Stevens27/09/2004 8 Use functional dependencies to … check that a relation is legal or good. e.g keys  K is a superkey of relation R if K → R i.e. whenever t1[k] = t2[k] thent1[R]= t2[R] K functionally determines all attributes in a tuple in R STUDENT (studno,name,hons,tutor,slot,year) studno → studno, name, hons, tutor, slot, year

9 CS530 - Ian Horrocks and Robert Stevens27/09/2004 9 Use functional dependencies to … check that a relation is legal or good. e.g. remove redundancy  Partial Dependency studno, courseno → subject (studno, courseno, subject)  Transitive Dependency studno → yeartutor studno → year year → yeartutor so, studno → yeartutor (studno, yeartutor)  Base functional dependencies F  Set of logically implied functional dependencies CLOSURE F+

10 CS530 - Ian Horrocks and Robert Stevens27/09/2004 10 Normalisation (in Brief)

11 CS530 - Ian Horrocks and Robert Stevens27/09/2004 11 Normalisation Overview ­ Stops information repeating over tables ­ Uses Functional Dependency ­ Uses a number of ‘forms’ 1 through 7 –(1NF, 2NF, 3NF, BCNF, 5NF, DK/NK, 7NF) ­ We shall go to 3 rd Look at Background for more. ­ After you’ve built 10 DBs you’ll just ‘know’ – it’ll become more craft than engineering.

12 CS530 - Ian Horrocks and Robert Stevens27/09/2004 12 UN-Normalised Data ­ To make it 1NF –Remove Repeating Groups

13 CS530 - Ian Horrocks and Robert Stevens27/09/2004 13 1 st NF (the Key) ­ To make it 2NF –Remove Part-Key Dependencies –Every non-primary-key attribute is fully functionally dependant on the primary key.

14 CS530 - Ian Horrocks and Robert Stevens27/09/2004 14 2 nd NF (the Whole Key) ­ To make it 3NF –No Transitive dependencies ­ e.g. A ->B / B ->C therefore A ->C ­ AH06 -> Sony Music / Sony Music -> UK Track Table CD Table

15 CS530 - Ian Horrocks and Robert Stevens27/09/2004 15 3 rd NF (and nothing but the key) Track Table CD Table Company Table

16 CS530 - Ian Horrocks and Robert Stevens27/09/2004 16 Boyce-Codd Normal Form A relation scheme R is in BCNF if, for all functional dependencies that hold on R of the form X → Y where R ⊇ X and R ⊇ Y at least one of the following holds  X → Y is trivial  X is a candidate key for the scheme R i.e. X → R Every attribute must depend on the key, the whole key and nothing but the key  Other Normal Forms: 1NF, 2NF and 3NF... uses primary key only  BCNF... generalised for candidate keys

17 CS530 - Ian Horrocks and Robert Stevens27/09/2004 17 Round Up If column (N) is FD on another column (M) then every value of M must define uniquely the value of N. M->N Student(id, name, staffID, time) Student(jbr, Joe Brown, har, 12-13) Student(spl, Sam Plant, gou, 14-15) Student(spl, Sam Plant, har, 12-13) id->name id->staffID id->time 12-13) name is Functionally Dependant on id staffID is NOT Functionally Dependant on id time is NOT Functionally Dependant on id Meets(spl, har, 12-13) – meeting once Meets(spl, har, 12-13) – meeting many

18 CS530 - Ian Horrocks and Robert Stevens27/09/2004 18 Background - Next SQL - Slide 76

19 CS530 - Ian Horrocks and Robert Stevens27/09/2004 19 Further Notes on Normalisation

20 CS530 - Ian Horrocks and Robert Stevens27/09/2004 20 Normalisation Given a relation R with a set of functional dependencies F, and a key K We must identify independent attributes 1. the key identifies all the attributes but… 2.... if an attribute only depends on part of the key, then it is independent of the rest of it. Attribute is partially dependent on the key 3.... if an attribute only depends on the key transitively, then it really depends directly on another attribute and is independent of the key. Attribute is transitively dependent on the key

21 CS530 - Ian Horrocks and Robert Stevens27/09/2004 21 Use functional dependencies to … check constraints on the set of legal relations F studno → name, tutor tutor → roomno roomno → tutor courseno → subject studno, courseno → labmark F+ studno, courseno → name partial studno → roomno transitive

22 CS530 - Ian Horrocks and Robert Stevens27/09/2004 22 Consequences of redundancy ­ Wasted space ­ Potential performance cost ­ Potential inconsistency ­ Inability to represent data

23 CS530 - Ian Horrocks and Robert Stevens27/09/2004 23 Use functional dependencies to … check the EER model mapping correctness ReaderBook readerid name finedate bookid title mn Return History readerid → readerid readerid → name bookid → bookid bookid → title Many:many relationships that could be weak entity types because they have hidden partial keys. ReturnHistory(readerid, bookid, date, fine) readerid, bookid → date ? readerid, bookid → fine ?

24 CS530 - Ian Horrocks and Robert Stevens27/09/2004 24 Using Functional Dependencies to... check EER mappings  STUDENT(studno, name, labmark) studno → name studno → labmark ?  COURSE(courseno, subject, roomno) courseno → subject courseno → roomno ?  STAFF(staffname, salary) staffname → salary where is staffname → roomno ? COURSE ENROL m n name STUDENT studno subject courseno labmark n TEACH STAFF m staffname roomno salary Attributes on wrong entities

25 CS530 - Ian Horrocks and Robert Stevens27/09/2004 25  STUDENT(studno, name) studno → name  COURSE(courseno, subject, studno) courseno → subject courseno → studno ? Wrong cardinalities on a relationship type COURSE ENROL n name STUDENT studno subject courseno 1 Using Functional Dependencies to... check EER mappings

26 CS530 - Ian Horrocks and Robert Stevens27/09/2004 26  COURSE (courseno, subject, lecturer,roomno) courseno → subject courseno → lecturer ? courseno → roomno lecturer → roomno Using Functional Dependencies to... check EER mappings Missing 1:many relationship type and entity type or missing multi-valued attribute COURSE subject roomno courseno lecturer

27 CS530 - Ian Horrocks and Robert Stevens27/09/2004 27 Functional Dependencies are hidden in EER Model studno name slot labmark STUDENT ENROL TUTOR STAFF COURSE courseno subjectname 1 m n m roomno

28 CS530 - Ian Horrocks and Robert Stevens27/09/2004 28 Using the EER Model and Functional Dependencies 1. Draw EER model 2. Map EER schema to relational schema 3. For every relation –List the functional dependencies – what does determine every attribute? –Check that every relation is in BCNF  does the key really solely uniquely identify each attribute?  if its not in BCNF then why?  Fix the problem –normalise and/or –trace back to EER model 4. Are there any functional dependencies missing? 5. Optimise the relational schema

29 CS530 - Ian Horrocks and Robert Stevens27/09/2004 29 Database design  Extended Entity Relationship –Top Down –Conceptual/Abstract View  Functional Dependencies –Bottom Up –Implementation View –The Determinancy Approach –Synthesise relations 1. List all attributes 2. Consider the relationships between them  those which determine the values of others are entities  those whose values are determined by other items are attributes.

30 CS530 - Ian Horrocks and Robert Stevens27/09/2004 30 Use functional dependencies to…Synthesise relations STUDENT (studno,givenname,familyname,hons,tutor,slot,year) studno, coursenolabmark studno, coursenoexammark ENROL(studno,courseno,labmark,exammark) courseno subject coursenoequip COURSE(courseno,subject,equip) studno familyname studnogivenname studnohons studnotutor studnoslot studnoyear lecturer roomno lecturerappraiser roomnolecturer roomno appraiser STAFF(lecturer,roomno,appraiser) year yeartutor year yeartutor YEAR(year,yeartutor) honsfaculty SCHOOL(hons,faculty) hons

31 CS530 - Ian Horrocks and Robert Stevens27/09/2004 31 er.… TEACH(courseno,lecturer) courseno, lecturer TEACH(courseno,lecturer, num_of_lectures) courseno, lecturernum_of_lectures

32 CS530 - Ian Horrocks and Robert Stevens27/09/2004 32 Complementary Approaches  Disadvantages of EER Top Down 1.Not all entity types are represented by nouns or noun-phrases - association entity types 2.Not all nouns and noun-phrases correspond to entities - single attribute entities  Disadvantages of determinancy bottom- up 1.Long-winded 2.Hides overall picture of data model

33 CS530 - Ian Horrocks and Robert Stevens27/09/2004 33 The Steps of Normalisation ­ Take one dependency at a time ­ Treat each relation separately and independently ­ Iterative process

34 CS530 - Ian Horrocks and Robert Stevens27/09/2004 34 Use functional dependencies to…  Systematically create legal relations  Derive relations which avoid anomalies in –Insertion –Deletion –Modification –Accessing  Ensure single valued-ness of facts represented in attributes in keyed relations  Ensure the removal of redundancy in a relation NORMALISE relations

35 CS530 - Ian Horrocks and Robert Stevens27/09/2004 35 Normalisation ­ Given –a universal relation that is unnormalised –a set of functional dependencies on the attributes in the relation –produce a set of relations where each relation is normalised for the functional dependencies on the attributes in the relation –Three approaches: –1. Relational synthesis –2. Step-wise normalisation –3. Using BCNF decomposition

36 CS530 - Ian Horrocks and Robert Stevens27/09/2004 36 The Process of Normalisation  Usually four steps giving rise to –First Normal Form (1NF) –Second Normal Form (2NF) –Third Normal Form (3NF) –Boyce-Codd Normal Form (BCNF) –Fourth Normal Form (4NF)  At each step we consider relationships between the functional dependencies of a relation’s attributes  Normalisation is a: –framework –series of tests UNNORMALISED ENTITY step1 remove repeating groups 1st NORMAL FORM step2remove partial dependencies 2nd NORMAL FORM step3 remove transitive dependencies 3rd NORMAL FORM / Boyce-Codd Normal Form step4 remove multi-dependencies 4th NORMAL FORM

37 CS530 - Ian Horrocks and Robert Stevens27/09/2004 37 First Normal Form  Attributes form Repeating Groups  When a group of attributes has multiple values then we say there is a repeating group of attributes in the relation  An relation is in 1NF if there are no repeating groups of attribute types  Any un-normalised relation is transformed to 1NF –Remove all repeating attribute groups –Repeating attribute groups become new relations in their own right –The key of the original relation must be an attribute (but not necessarily a key) of the derived relation.

38 CS530 - Ian Horrocks and Robert Stevens27/09/2004 38 First Normal Form : Repeating Groups STUDENT (studno, name, tutor, roomno) studno → name, tutor tutor → roomno, roomno → tutor STUDENT_DETAILS (studno, name, tutor, roomno, {courseno, labmark, subject}) studno → name, tutor courseno → subject tutor → roomno, roomno → tutor studno, courseno → labmark ENROL (studno, courseno, subject, labmark) courseno → subject studno, courseno → labmark

39 CS530 - Ian Horrocks and Robert Stevens27/09/2004 39 Benefits from First Normal Form  Any ‘hidden’ relations (entities) are identified  Process results in separation of different objects  BUT anomalies may still exist ENROL (studno, courseno, subject, labmark) –subject appears on every enrolment occurrence. –This may result in anomalies when updating or deleting tuples –The problem in example is that subject is functionally dependent only on courseno which is only part of the key

40 CS530 - Ian Horrocks and Robert Stevens27/09/2004 40 Second Normal Form  A relation is in 2NF if it is in 1NF and each non identifying attribute depends upon the whole key (identifier)  Any relation in 1NF is transformed to 2NF –Identify functional dependencies –Re-write relations so that each non-identifying attribute is functionally dependent on the whole of the key –Decompose ENROL into two relations ENROL (studno, courseno, subject, labmark) courseno → subject studno, courseno → labmark ENROL’ (studno, courseno, labmark) studno, courseno → labmark COURSE (courseno, subject) courseno → subject

41 CS530 - Ian Horrocks and Robert Stevens27/09/2004 41 Second Normal Form STUDENT(studno, name, tutor, roomno) studno → name, tutor tutor → roomno roomno → tutor ENROL’ (studno, courseno, labmark) studno, courseno → labmark COURSE (courseno, subject) courseno → subject

42 CS530 - Ian Horrocks and Robert Stevens27/09/2004 42 Third Normal Form  An relation is in 3NF if it is in 2NF and all non- identifying attributes are independent  Any relation in 2NF is transformed in 3NF  Determine functional dependencies between non identifying attributes  Decompose relation into new relations STUDENT (studno, name, tutor, roomno) studno → name, tutor tutor → roomno roomno → tutor STUDENT (studno, name, tutor) studno → name, tutor TUTOR (tutor, roomno) tutor → roomno roomno → tutor

43 CS530 - Ian Horrocks and Robert Stevens27/09/2004 43 Student Relational Schema in 3NF  STUDENT (studno, name, tutor) studno → name, tutor  TUTOR (tutor, roomno) tutor → roomno roomno → tutor  ENROL (studno, courseno, labmark) studno, courseno → labmark  COURSE (courseno, subject) courseno → subject

44 CS530 - Ian Horrocks and Robert Stevens27/09/2004 44 Decomposition: Lossless or Non- additive Join  R is a relational scheme, F is a set of functional dependencies on R. R1 and R2 form a decomposition of R.  The decomposition of R is non-additive if at least one of the following functional dependencies are in F+ R1 ∩ R2 → R1 R1 ∩ R2 → R2  The decomposition of R is non-additive if for every state r of R that satisfies F (π (r),..., π (r) ) = r where condition is the natural join

45 CS530 - Ian Horrocks and Robert Stevens27/09/2004 45 Decomposition: Lossless or Non-additive Join  ENROL’ ∩ COURSE = courseno  courseno → subject  (courseno, subject) = COURSE ENROL (studno, courseno, subject, labmark) courseno → subject studno, courseno → labmark ENROL’ (studno, courseno, labmark) studno, courseno → labmark COURSE (courseno, subject) courseno → subject

46 CS530 - Ian Horrocks and Robert Stevens27/09/2004 46 Lossless or Non-additive Join  STUDENT1 (tutor = tutor)TUTORS = STUDENT studno → name studno → tutor tutor → roomno roomno → tutor studno → name studno → tutor tutor → roomno roomno → tutor

47 CS530 - Ian Horrocks and Robert Stevens27/09/2004 47 Spurious Tuples Lossless or Non-additive Join TEACH TEACH’ LECTURES

48 CS530 - Ian Horrocks and Robert Stevens27/09/2004 48 Decomposition Algorithm: Decomposition D, relation R  set D := { R } ;  while there is a relation schema Q in D that is not in BCNF do  begin –choose a relation schema Q in D that is not in BCNF; –find a functional dependency X→Y in Q that violates BCNF;  violation means that (X) + fails to find all of Q, so X can’t be a key. –replace Q in D by two schemas  R1 (Q - (Y) + ∪ X) –leave copy of X in relation to be the foreign key for R2 and  R2 (X ∪ (Y) + ) –new relation for functional dependency and its closure, X will be the primary key  end;

49 CS530 - Ian Horrocks and Robert Stevens27/09/2004 49 Lossless or Non-additive Join X YZ X Y X Z XY foreign key

50 CS530 - Ian Horrocks and Robert Stevens27/09/2004 50 Decomposition: Dependency Preservation  When an update is made to a database, should be able to check that update satisfies all functional dependencies.  It is desirable to allow validation of relational database schemes that allow update validation without the computation of joins.  independent manipulation of relations.

51 CS530 - Ian Horrocks and Robert Stevens27/09/2004 51 Dependency Preservation  The union of dependencies that hold on the individual relations in decomposition D must be equivalent to F.  Given F on R, π F (R i ) where R i ⊆ R is the set of dependencies X Y in F + such that the attributes in X ∪ Y are all contained in R i  Decomposition D = {R 1, R 2,..., R m } of R is dependency preserving w.r.t. F if (π F (R 1 )) ∪.... ∪ π F (R m ))) + = F +  Given the restriction of functional dependencies to a relation is the fds that involve attributes of that relation F i for R i n n U Fi ≠ F possible, but... (U F i ) + = F + i=1 i =1

52 CS530 - Ian Horrocks and Robert Stevens27/09/2004 52 Dependency Preservation  STUDENT (studno, name, tutor, roomno, appraiser) studno → name, tutor tutor → roomno, appraiser roomno → tutor, appraiser  STUDENT1 (studno, name, tutor) studno → name, tutor  TUTOR (studno, roomno, appraiser) studno → roomno, appraiser This is in Boyce-Codd Normal Form and is a lossless (nonadditive) join decomposition but we have lost....  tutor → roomno, appraiser roomno → tutor, appraiser

53 CS530 - Ian Horrocks and Robert Stevens27/09/2004 53 STUDENT’ TUTOR = STUDENT studno → name studno → tutor tutor → roomno tutor → appraiser roomno → tutor roomno → appraiser studno → appraiser studno → roomno studno → name studno → tutor studno → appraiser studno → roomno Dependency Preservation

54 CS530 - Ian Horrocks and Robert Stevens27/09/2004 54 Designing a relational schema  Build a relational database –without redundancy  normalisation –without loss of information or gain of data  lossless join decomposition –without losing dependency integrity  dependency preservation

55 CS530 - Ian Horrocks and Robert Stevens27/09/2004 55 Multi-valued Dependencies and Fourth Normal Form

56 CS530 - Ian Horrocks and Robert Stevens27/09/2004 56 Multi-valued Dependencies  a course has many lecturers  a course has many texts  lecturers and texts are independent  a lecturer teaches many courses  a text is used by many courses ­ lecturer and text are independent sets ­ for each courseno there is an associated set of lecturers ­ for each courseno there is an associated set of texts ­ the sets are independent.

57 CS530 - Ian Horrocks and Robert Stevens27/09/2004 57 Multi-valued Dependencies courseno →→ lecturer courseno →→ text This is in BCNF key is {courseno,lecturer,t ext} courseno, lecturer,text →courseno, lecturer,text  trivial dependencies

58 CS530 - Ian Horrocks and Robert Stevens27/09/2004 58 Multi-valued Dependencies Each TEXT is associated with all the LECTURERS that teach a COURSE The attribute TEXT contains redundant values. If TEXT were deleted from rows 1, 2 & 3 the values could be deduced from rows 4,5 & 6

59 CS530 - Ian Horrocks and Robert Stevens27/09/2004 59 Multivalued Dependencies courseno →→ lecturer courseno →→ text  if (c,l,t) and (c,l’,t’) appear then  (c,l,t’) and (c,l’,t) appear also  tuple (c,l,t) appears if c can be taught by l using text t  for each course all possible combinations of lecturer and text appear

60 CS530 - Ian Horrocks and Robert Stevens27/09/2004 60 Multi-Valued Dependencies  Whenever X →→ Y holds in R so does X →→(R - (XY)).  a MVD is trivial if Y ⊂ X or X ∪ Y = R. i.e. the two attributes form the whole relation  non-trivial MV dependencies need at least 3 attributes.

61 CS530 - Ian Horrocks and Robert Stevens27/09/2004 61 Fourth Normal Form  A relation R is in 4NF if it is in 3NF and there are no multi-valued dependencies between its attribute types  A relation R is in 4NF iff whenever there exists a non- trivial multi-valued dependency in F + for R X →→ Y  X is a superkey for R, i.e. all attributes are functionally dependent on X.  Any relation in 3NF is transformed in 4NF –Detect any multi-valued dependencies –Decompose relation

62 CS530 - Ian Horrocks and Robert Stevens27/09/2004 62 Fourth Normal Form courseno →→ lecturer courseno →→ text trivial dependenciesonly

63 CS530 - Ian Horrocks and Robert Stevens27/09/2004 63 Lossless join decomposition into 4NF  Algorithm: Decomposition D, relation R 1.set D := { R } ; 2. while there is a relation schema Q in D that is not in 4NF do begin choose a relation schema Q in D that is not in 4NF; find a non-trivial MVD X →→ Y in Q that violates 4NF; replace Q in D by two schemas (Q -Y) and (X ∪ Y) end;

64 CS530 - Ian Horrocks and Robert Stevens27/09/2004 64 Fourth Normal Form EER modelling  Leads to correctly normalised relational schema COURSE STAFF TEXT teaches recommendation m n m n name texttitle courseno

65 CS530 - Ian Horrocks and Robert Stevens27/09/2004 65 Fourth Normal Form EER modelling  Leads to relational schema that is not in 4NF COURSE STAFF TEXT Course-Staff-Text m p n name courseno texttitle

66 CS530 - Ian Horrocks and Robert Stevens27/09/2004 66 Conclusions  Data Normalisation is a technique that ensures the basic properties of the relational model –no duplicate tuples –no nested relations  Data normalisation is sometimes used as the only technique for database design— implementation view  A more appropriate approach is to complement conceptual modelling with data normalisation

67 CS530 - Ian Horrocks and Robert Stevens27/09/2004 67 Lossless or Non-additive Join Algorithm Decomposition D, relation R 1.set D := {R} ; 2.while there is a relation schema Q in D that is not in BCNF do begin choose a relation schema Q in D that is not in BCNF; find a functional dependency X→Y in Q that violates BCNF; replace Q in D by two schemas R1 (Q - Y) leave copy of X in relation to be foreign key for R2 and R2 (X ∪ Y) new relation for functional dependency and its closure, X will be the primary key end;

68 CS530 - Ian Horrocks and Robert Stevens27/09/2004 68 Example

69 CS530 - Ian Horrocks and Robert Stevens27/09/2004 69 Normalisation Example BEER_DATABASE Additional Notes: Warehouses are shared by breweries. Each beer is unique to the brewer. Each brewery is based in a city.

70 CS530 - Ian Horrocks and Robert Stevens27/09/2004 70 Minimal Sets of Functional Dependencies  A set of functional dependencies F is minimal if: 1. Every dependency F has a single determined attribute A 2. We cannot remove any dependency from F and still have a set of dependencies equivalent to F 3. We cannot replace and dependency X → A in F with a dependency A→ X, where A ⊂ X and still have a set of dependencies that is equivalent to F I.e. a canonical form with no redundancies (beer, brewery, strength, city, region, warehouse, quantity)  beer→ brewery  beer→ strength  brewery → city  city → region  beer, warehouse, → quantity

71 CS530 - Ian Horrocks and Robert Stevens27/09/2004 71 Relational Synthesis Algorithm into 3NF: (beer, brewery, strength, city, region, {warehouse, quantity}) set D := { R } ; P. 426, P. 431 1. Find a minimal cover G for F 2. For each determinant X of a functional dependency that appears in G create a relation schema { X ∪ A1, X ∪ A2…X ∪ Am} in D where X → A1, X → A1, … X → A1m are the only dependencies in G with X as the determinant; 3. Place any remaining (unplaced) attributes in a single relation to ensure attribute preservation property so we don’t lose anything. 4. If none of the relations contains a key of R, create one more relation that contains attributes that form a key for R.  beer→ brewery(beer, brewery, strength)  beer→ strength  brewery → city (brewery, city)  city → region (city, region)  beer, warehouse, → quantity(beer, warehouse, quantity)

72 CS530 - Ian Horrocks and Robert Stevens27/09/2004 72 Step-wise normalisation: (beer, brewery, strength, city, region, {warehouse, quantity})  beer→ brewery, strength partial dependency  brewery → city transitive dependency  city → region transitive dependency  beer, warehouse, → quantity repeating group 1NF remove repeating group (beer, brewery, strength, city, region, {warehouse, quantity}) (beer, warehouse, quantity) beer, warehouse, → quantity (beer, brewery, strength, city, region) beer→ brewery, strength transitive dependency brewery → city transitive dependency city → region

73 CS530 - Ian Horrocks and Robert Stevens27/09/2004 73 (beer, brewery, strength, city, region)  beer→ brewery, strength  brewery → city transitive dependency  city → regiontransitive dependency  2NFno partial dependencies  3NF/BCNFno transitive dependencies (beer, brewery, strength, city, region) (city, region) city → region (beer, brewery, strength, city) beer→ brewery, strength brewery → city (brewery, city) brewery → city (beer, brewery, strength) beer→ brewery, strength Take the most indirect transitive dependencies

74 CS530 - Ian Horrocks and Robert Stevens27/09/2004 74 Using BNCF decomposition algorithm: (beer, brewery, strength, city, region, warehouse, quantity)  beer→ brewery, strength partial dependency  brewery → city transitive dependency  city → region transitive dependency  beer, warehouse, → quantity Directly to BCNF take a violating dependency and form a relation from it. First choose a direct transitive dependency and its closure (beer, brewery, strength, city, region, warehouse, quantity) brewery → city (brewery, city, region) brewery → city city → region transitive dependency (beer, brewery, strength, warehouse, quantity) beer→ brewery, strengthpartial dependency beer, warehouse, → quantity

75 CS530 - Ian Horrocks and Robert Stevens27/09/2004 75 Using BNCF decomposition algorithm: (beer, brewery, strength, city, region, warehouse, quantity)  beer→ brewery, strength partial dependency  brewery → city transitive dependency  city → region transitive dependency  beer, warehouse, → quantity take a violating dependency and form a relation from it. First the partial dependency and its closure (beer, brewery, strength, city, region, warehouse, quantity) beer→ brewery, strength (beer, brewery, strength, city, region) beer→ brewery, strength brewery → city transitive dependency city → region transitive dependency normalise as before... (beer, warehouse, quantity) beer, warehouse, → quantity

76 CS530 - Ian Horrocks and Robert Stevens27/09/2004 76 Keys and Indexes, Data Definition, Relational Manipulation and Data Control Using SQL

77 CS530 - Ian Horrocks and Robert Stevens27/09/2004 77 Keys  SuperKey –a set of attributes whose values together uniquely identify a tuple in a relation  Candidate Key –a superkey for which no proper subset is a superkey…a key that is minimal. –Can be more than one for a relation  Primary Key –a candidate key chosen to be the main key for the relation. –One for each relation  Keys can be composite

78 CS530 - Ian Horrocks and Robert Stevens27/09/2004 78 e.g.: Staff(lecturer,roomno,appraiser) SK = {lecturer,roomno,appraiser}, {lecturer,roomno}, {lecturer, appraiser}, {roomno,appraiser}, {lecturer} and {roomno} CK = {lecturer} and {roomno} PK = {lecturer}

79 CS530 - Ian Horrocks and Robert Stevens27/09/2004 79 Foreign Key  a (set of) attribute(s) in a relation that exactly matches a (primary) key in another relation –the names of the attributes don’t have to be the same but must be of the same domain –a foreign key in a relation A matching a primary key in a relation B represents a  many:one relationship between A and B Student(studno,name,tutor,year) Staff(lecturer,roomno,appraiser)

80 CS530 - Ian Horrocks and Robert Stevens27/09/2004 80 Data Definition and Manipulation

81 CS530 - Ian Horrocks and Robert Stevens27/09/2004 81 Languages of DBMS  Data Definition Language DDL –define the logical schema (relations, views etc) and storage schema stored in a Data Dictionary  Data Manipulation LanguageDML –Manipulative populate schema, update database –Retrieval querying content of a database  Data Control LanguageDCL –permissions, access control etc...

82 CS530 - Ian Horrocks and Robert Stevens27/09/2004 82 Data Definition:Creating tables create table accountants as(select studno, name, tutor, year from student where hons = ‘ca’ );  Can specify column names, default values and integrity constraints (except referential)  Datatypes and lengths derived from query  Not null constraints passed on from query tables

83 CS530 - Ian Horrocks and Robert Stevens27/09/2004 83 Defining a Relation create table student (studentno number(8) primary key, givenname char(20), surname char(20), hons char(3) check ( hons in ( 'cis','cs','ca','pc','cm','mcs' )), tutorid number(4), yearno number(1) not null, constraint year_fk foreign key ( yearno ) references year ( yearno ), constraint super_fk foreign key ( tutorid ) references staff ( staffid ));

84 CS530 - Ian Horrocks and Robert Stevens27/09/2004 84 Data Definition: Create Table create table enrol ( studno number( 8 ), courseno char( 5 ), primary key ( studno, courseno ), cluster ( studno ), labmark number( 3 ) check ( labmark between 0 and 100 ), exammark number( 3 ) check ( exammark between 0 and 100 ), constraint stud_fk foreign key ( studno ) references student, constraint course_fk foreign key ( courseno ) references course );

85 CS530 - Ian Horrocks and Robert Stevens27/09/2004 85 Data Definition: Altering Relations ­ alter table student add ( address char( 20 ), default null); alter table student modify ( name not null);  this won’t work if there are any nulls in the name column

86 CS530 - Ian Horrocks and Robert Stevens27/09/2004 86 Data Manipulation: Insert Operator insert (cs310, elec, sun) into course; Course insert into course (courseno,subject,equip) values (‘cs310’, ‘elec’, ‘sun’) ; insert into course values (‘cs310’, ‘elec’, NULL) ; insert into table where search-condition

87 CS530 - Ian Horrocks and Robert Stevens27/09/2004 87 Inserting Tuples into a Relation insert into weak_students (studno,name,courseno,exammark) where (select s.studno,name,courseno,exammark from enrol, student s where exammark <= 40 and enrol.studno = s.studno );

88 CS530 - Ian Horrocks and Robert Stevens27/09/2004 88 Insertion Anomalies  An insert operation might voliate the uniqueness and minimality properties of the primary key of the referential integrity constraint  insert (cs250,databases,sun) into course Insertion anomalies can be corrected by rejecting the insertion correcting the reason for rejecting the update

89 CS530 - Ian Horrocks and Robert Stevens27/09/2004 89 Data Manipulation: Update Operator  Modifies a tuple or tuples of a relation  Don’t violate constraints as long as the modified attributes are not primary keys or foreign keys  Update of a primary key corresponds to a deletion followed by an insertion  Update of a foreign key attribute is legal only if the new value corresponds to an existing tuple in the referenced relation or is null update enrol set labmark = labmark * 1.1 where courseno = ‘cs250’; update table set column = expression [ where search-condition]

90 CS530 - Ian Horrocks and Robert Stevens27/09/2004 90 Data Manipulation: Delete Operator  Deletes a tuple or a set of tuples from a relation  Might violate the referential integrity constraint  Anomalies can be overcome by –rejecting the deletion –cascading the deletion (delete tuples that reference deleted tuple) –modifying the referencing attribute values delete from table [ where search-condition] delete from course where equip = ‘pc’ ; delete from student where year = ‘3’ and( hons != ‘mi’ or hons <> ‘ si’ );

91 CS530 - Ian Horrocks and Robert Stevens27/09/2004 91 Delete Operator delete from student where studno in (select student.studno from enrol e, teach t, student s where t.lecturer = ‘woods’ and t.courseno = e.courseno and e.studno = s.studno );

92 CS530 - Ian Horrocks and Robert Stevens27/09/2004 92 Data Control: Data Sharing and Security  Permissions, access control etc... ­ create view myyear as select * from student where year in (select year from student where name = user) with check option

93 CS530 - Ian Horrocks and Robert Stevens27/09/2004 93 Data Control: Data Sharing and Security grant privilege, privilege2… | all on table | view to userID | roleID grant select on student to bloggsf;  Grant can be attached to any combination of select, insert, update, delete, alter  Restricting access to parts pf a table can be effected by using the view and grant commands  Privileges can be withdrawn with the revoke command

94 CS530 - Ian Horrocks and Robert Stevens27/09/2004 94 Synonyms for Objects ­ select name from CAROLE.student; ­ create [public] synonym synonym_name for table | view; ­ create synonym student for CAROLE.student; ­ drop synonym mystudent;

95 CS530 - Ian Horrocks and Robert Stevens27/09/2004 95 The Role of the Data Dictionary  A set of tables and views to be used by the RDBMS as a reference guide to the data stored in the database files  Every user retrieves data from views stored in the Data Dictionary  The Data Dictionary stores: –user names of those permitted to access the database –names of tables, space definitions, views, indexes, clusters, synonyms etc –rights and privileges that have been granted

96 CS530 - Ian Horrocks and Robert Stevens27/09/2004 96 Examples Class

97 CS530 - Ian Horrocks and Robert Stevens27/09/2004 97 Relational Query Languages

98 CS530 - Ian Horrocks and Robert Stevens27/09/2004 98 Query Operators  Relational Algebra –tuple (unary)Selection, Projection –set (binary)Union, Intersection, Difference –tuple (binary)Join, Division  Additional Operators –Outer Join, Outer Union

99 CS530 - Ian Horrocks and Robert Stevens27/09/2004 99 A Retrieval DML Must Express  Attributes required in a result –target list  Criteria for selecting tuples for that result –qualifier  The relations that take part in the query –set generators  Independent of the instances in the database  Expressions are in terms of the database schema

100 CS530 - Ian Horrocks and Robert Stevens27/09/2004 100 Relational Algebra

101 CS530 - Ian Horrocks and Robert Stevens27/09/2004 101 SQL Retrieval Statement SELECT[all|distinct] {*|{table.*|expr[alias]|view.*} [,{table.*|expr[alias]}]...} FROM table [alias][,table[alias]]... [WHERE condition] [CONNECT BY condition [START WITH condition]] [GROUP BY expr [,expr]...] [HAVING condition] [{UNION|UNION ALL|INTERSECT|MINUS} SELECT...] [ORDER BY {expr|position} [ASC|DESC][,expr|position}[ASC|DESC]. [FOR UPDATE OF column [,column]... [NOWAIT]]

102 CS530 - Ian Horrocks and Robert Stevens27/09/2004 102 π Project Operator selects a subset of the attributes of a relation Result = π (attribute list) (relation name) attribute list are drawn from the specified relation; if the key attribute is in the list then card(result) = card(relation) resulting relation has only the attributes in the list, in same order as they appear in the list the degree(result) = number of attributes in the attribute list no duplicates in the result

103 CS530 - Ian Horrocks and Robert Stevens27/09/2004 103 π Project Operator π tutor (STUDENT)

104 CS530 - Ian Horrocks and Robert Stevens27/09/2004 104 π Project Operator SELECT select * from student ; select tutor from student ;

105 CS530 - Ian Horrocks and Robert Stevens27/09/2004 105 σ Select Operator selects a subset of the tuples in a relation that satisfy a selection condition Result = σ (selection condition) (relation name) a boolean expression specified on the attributes of a specified relation a relation that has the same attributes as the source relation; stands for the usual comparison operators ‘ ‘, ‘ ‘, ‘>=‘, etc clauses can be arbitrarily connected with boolean operators AND, NOT, OR degree(result) = degree(relation); card(result) <= card(relation)

106 CS530 - Ian Horrocks and Robert Stevens27/09/2004 106 σ Select Operator σ name=‘bloggs’ (STUDENT)

107 CS530 - Ian Horrocks and Robert Stevens27/09/2004 107 retrieve tutor who tutors Bloggs π tutor ( σ name=‘bloggs’ (STUDENT)) select tutor from student where name = ‘bloggs’ ;

108 CS530 - Ian Horrocks and Robert Stevens27/09/2004 108 SQL retrieval expressions ­ select studentno, name from student where hons != ‘ca’ and ( tutor = ‘goble’ or tutor = ‘kahn’ ); ­ select * from enrol where labmark > 50 ; ­ select * from enrol where labmark between 30 and 50 ;

109 CS530 - Ian Horrocks and Robert Stevens27/09/2004 109 ­ select * from enrol where labmark in ( 0, 100 ); ­ select * from enrol where labmark is null; ­ select * from student where name is like ‘b%’ ; ­ select studno, courseno, exammark + labmark total from enrol where labmark is not NULL;

110 CS530 - Ian Horrocks and Robert Stevens27/09/2004 110 Cartesian Product Operator Definition:  The cartesian product of two relations R1(A 1,A 2,...,A n ) with cardinality i and R2(B 1,B 2,...,B m ) with cardinality j is a relation R3 with degree k=n+m, cardinality i*j and attributes (A 1,A 2,...,A n,B 1,B 2,...,B m )  The result, denoted by R1XR2, is a relation that includes all the possible combinations of tuples from R1 and R2  Used in conjunction with other operations

111 CS530 - Ian Horrocks and Robert Stevens27/09/2004 111 Cartesian Product Example

112 CS530 - Ian Horrocks and Robert Stevens27/09/2004 112 X Cartesian Product

113 CS530 - Ian Horrocks and Robert Stevens27/09/2004 113 θ Join Operator Definition: The join of two relations R1(A 1,A 2,...,A n ) and R2(B 1,B 2,...,B m ) is a relation R3 with degree k=n+m and attributes (A 1,A 2,...,A n, B 1,B 2,...,B m ) that satisfy the join condition stands for the usual comparison operators ‘ ‘, ‘ ‘, ‘>=‘, etc comparing terms in the Θ clauses can be arbitrarily connected with boolean operators AND, NOT, OR The result is a concatenated set but only for those tuples where the condition is true. It does not require union compatibility of R1 and R2 Result = R1 (θ join condition) R2

114 CS530 - Ian Horrocks and Robert Stevens27/09/2004 114 θ Join Operator

115 CS530 - Ian Horrocks and Robert Stevens27/09/2004 115 More joins

116 CS530 - Ian Horrocks and Robert Stevens27/09/2004 116 Natural Join Operator  Of all the types of θ-join, the equi-join is the only one that yields a result in which the compared columns are redundant to each other—possibly different names but same values  The natural join is an equi-join but one of the redundant columns (simple or composite) is omitted from the result  Relational join is the principle algebraic counterpart of queries that involve the existential quantifier ∃

117 CS530 - Ian Horrocks and Robert Stevens27/09/2004 117 Self Join: Joins on the same relation π (lecturer, (staff (appraiser = lecturer) staff) roomno,appraiser, approom) select e.lecturer, e.roomno, m.lecturer appraiser, m.roomno approom from staff e, staff m where e.appraiser = m.lecturer

118 CS530 - Ian Horrocks and Robert Stevens27/09/2004 118 Exercise Get student’s name, all their courses, subject of course, labmark for course, lecturer of course and lecturer’s roomno for ‘ca’ students University Schema  STUDENT(studno,name,hons,tutor,year)  ENROL(studno,courseno,labmark,exammar k)  COURSE(courseno,subject,equip)  STAFF(lecturer,roomno,appraiser)  TEACH(courseno,lecturer)  YEAR(yearno,yeartutor)

119 CS530 - Ian Horrocks and Robert Stevens27/09/2004 119 Set Theoretic Operators  Union, Intersection and Difference  Operands need to be union compatible for the result to be a valid relation Definition : Two relations R1(A 1,A 2,...,A n ) and R2(B 1,B 2,...,B m ) are union compatible iff: n = m and, dom(A i )= dom (B i ) for 1 ≤ i ≤ n

120 CS530 - Ian Horrocks and Robert Stevens27/09/2004 120 ∪ Union Operator Definition : The union of two relations R1(A 1,A 2,...,A n ) and R2(B 1,B 2,...,B m ) is a relation R3(C 1,C 2,...,C n ) such that dom(C i )= dom(A i ) =dom (B i ) for 1 ≤ i ≤ n The result R1 ∪ R2 is a relation that includes all tuples that are either in R1 or R2 or in both without duplicate tuples The resulting relation might have the same attribute names as the first or the second relation

121 CS530 - Ian Horrocks and Robert Stevens27/09/2004 121 Retrieve all staff that lecture or tutor Lecturers π (lecturer) TEACH Tutors π (tutor) STUDENT Lecturers ∪ Tutors

122 CS530 - Ian Horrocks and Robert Stevens27/09/2004 122 ∩ Intersection Operator Definition: The intersection of two relations R1(A 1,A 2,...,A n ) and R2(B 1,B 2,...,B m ) is a relation R3(C 1,C 2,...,C n ) such that dom(C i )= dom(A i ) ∩ dom (B i ) for 1 ≤ i ≤ n  The result R1 ∩ R2 is a relation that includes only those tuples in R1 that also appear in R2  The resulting relation might have the same attribute names as the first or the second relation

123 CS530 - Ian Horrocks and Robert Stevens27/09/2004 123 Retrieve all staff that lecture and tutor Lecturers π (lecturer) TEACH Tutors π (tutor) STUDENT Lecturers ∩ Tutors

124 CS530 - Ian Horrocks and Robert Stevens27/09/2004 124 − Difference Operator Definition: The difference of two relations R1(A 1,A 2,...,A n ) and R2(B 1,B 2,...,B m ) is a relation R3(C 1,C 2,...,C n ) such that dom(C i )= dom(A i ) −dom (B i ) for 1 ≤ i ≤ n The result R1 − R2 is a relation that includes all tuples that are in R1 and not in R2  The resulting relation might have the same attribute names as the first or the second relation

125 CS530 - Ian Horrocks and Robert Stevens27/09/2004 125 Retrieve all staff that lecture but don’t tutor Lecturers π (lecturer) TEACH Tutors π (tutor) STUDENT Lecturers -Tutors

126 CS530 - Ian Horrocks and Robert Stevens27/09/2004 126 Relational Algebra Re Cap

127 CS530 - Ian Horrocks and Robert Stevens27/09/2004 127 Relational Algebra  Relational Algebra –tuple (unary)  Selection - Result = σ (selection condition)(relation name)  Projection - Result = π (attribute list)(relation name) - π tutor ( σ name=‘bloggs’ (STUDENT) ) –set (binary)  Union- Lecturers ∪ Tutors  Intersection- Lecturers ∩ Tutors  Difference- Lecturers -Tutors –tuple (binary)  Join- Result = R1 wv (θ join condition) R2  Division- (A / B) Not supported as a primitive operator

128 CS530 - Ian Horrocks and Robert Stevens27/09/2004 128 Relational Algebra –Additional Operators  Outer Join & Outer Union (+) –Pads with nulls –(includes all tuples not just matches - as with Natural / Equi-join) –Aggregation Functions  ƒ (relation name)  How many courses is a student enrolled for?  studno ƒ COUNT courseno (ENROL)

129 CS530 - Ian Horrocks and Robert Stevens27/09/2004 129 For Next Lecture I expect you to have SKIM Read the notes for the next lecture before it’s delivered. The sequence of: skim read; lecture delivery; SAQ will make revision a whole lot easier.


Download ppt "CS530 Database Architecture Models and Design Prof. Ian HORROCKS Dr. Robert Stevens Wednesday - Practical Tables."

Similar presentations


Ads by Google