Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relations, Relational database Idea of relation Relational data base From the diagram ERD to database schema.

Similar presentations


Presentation on theme: "Relations, Relational database Idea of relation Relational data base From the diagram ERD to database schema."— Presentation transcript:

1 Relations, Relational database Idea of relation Relational data base From the diagram ERD to database schema

2 Relations Definition Relation (in sense of database) is a subset of cross-product of finite number of simple fields (domains) Field (domain) is simple, if its elements are indivisible (atomic). Let D 1, D 2,…, D n be simple fields, 0<n<∞, D 1 X D 2 X …X D n –cross product of these simple fields (domains): The set of all tuples (d 1, d 2, …,d n ) that, d 1 Є D 1, d 2 Є D 2, … d n Є D n Number n is the relation degree Relation is a set of n-element tuples (d 1, d 2, …,d n ), d i Є D i, i=1,…,n, where D i are simple fields (domains) Individual position of tuples in the relation are called attributes We formulate the requirement of atomocity of attributes’ domains of the relation also as follows: Relation is in the first normal form (1NF)

3 Relations - examples Let N=4 D 1 – set of natural numbers D 2 – set of string characters beginning with “T” D 3 – set of dates in XX century D 4 – set of letters of Latin alphabet Examples : relations –1. {(1, “T01”, 12/01/1987, A), (43, “TABLE”, 01/01/1901,m) (6, “TTTFRTHH”, 31/12/1998,x), (143, “T”, 3/3/1933, A)} –2. {(1000, “T11”, 2/11/1987,B)} –3. {}

4 Relation representation Relation can be represented in the form of a table, in which each row corresponds to one tuple, and each column corresponds to one attribute Column’s name is the same name of the attribute. Names cann’t be repeated Table is not a relation. It is just a form to represent a relation. (e.g. In tables, rows are well ordered, but tuples in relation need not be ordered) NoNameAge 123-456-78-90Ahmad22 345-634-34-85Said21 987-543-65-32Ali23 234-765-734-87Kemal19 An example of a table representing a relation with three attributes:

5 Proporties of the Relation 1. In a relation, there are no two identical tuples. (because relation is a mathematical set) 2. Tuples are not ordered (because relation is a mathematical set) 3. Values of attributes are atomic (single values) In each position of the tuple (in one cell of the table, that represents a relation) there is only one, elementary indivisible value. In other words, a database relation is in the first normal form (1NF) A relation is in the first normal form (1NF) if every field contains only atomic values, that is, not lists or sets. Mathematical relation need not be 1NF – domains may be not atomic.

6 Relation schema Relation schema: is the relation name and attributes’ names (optionally, with their fields) Examples of relation’s schemas: –Nomeaning(Nember, String, Date, Letter) –Clients (No, Name, address, Status) No Є (set of legal numbers) Name, address Є (String of characters) Status Є (set of natural numbers) –Books (ISBN, Author, Title, Year) ISBN Є (set of ISBN numbers) Author, Title Є (String of characters) Year Є (set of years)

7 Relation’s Keys Let R be a relation with attributes A 1, A 2, …, A n. Let us study set of attributes: K = {A i, A j,…, A k } –Uniqueness condition: There is no possibility for two tuples – in the same time- to have the same values for A i attribute, the same values for A j attribute,…, the same values for A k attribute –Minimality condition: No possibility to remove any attribute from A i, A j,…, A k without violation of the first property (unique). –Relation’s Key (Candidate Key) – Attribute (or set of attributes) that fulfill the conditions: uniqueness and minimality Simple key – has only one attribute Composite key – is composed of many keys

8 Relation’s Keys Primary Key of the relation : is a candidate key arbitrarily chosen for the relation. The rest of keys (other candidate keys, if they exist) – will be alternative keys Note that: –Since the relation is a set, there will always at least one key. –In a set there are no repeated elements. Therefore, there are no two identical tuples. Moreover, it will always exist some combination of attributes that fulfill the two conditions: unique and minimal. in extreme case, it can be a combination of all attributes in the relation. Consequently: Each relation has a primary key

9 Keys of relations- notations Primary keys: Books (ISBN, Author, Title, Year) Clients (No, Name, address, Status) Books (ISBN, Author, Title, Year) Simple primary key Composite primary key Foreign keys Books (ISBN, Author, Title, Year, Id-pub REF Publisher) Publisher (Ident, Name, City) Foreign key corresponds to this primary key

10 Keys of relations- notations Foreign key corresponding to primary key in the same relation Workers (No-Work, Name, Position, superior REF workers) This foreign key corresponds to primary key Foreign key as a component of the primary key Ordering (Ord-No, Date, Client,…) Pos-Ord (Ord-No REF Ordering, Pos-No, Goods, Quatity,…) This foreign key is a component of a primary key

11 Keys of relations- notations A composite Foreign key Ordering (Ord-No, Date, Client,…) Ord-No: KEY Pos-Ord (Ord-No REF Ordering, Pos-No, Goods, Quatity,…) (Ord-No, Pos-No) KEY Ord-No REF Ordering Description-Pos-Ord (No, Ord-No, Pos-No, Text) (No, Ord-No, Pos-No) KEY (Ord-No, Pos-No) REF Pos-Ord This foreign key is a composite of two attributes The foreign key need not have the the same name that the primary key of relation has. Howover, one should apply this convention because the legibility of database schema makes better.

12 Keys of relations- conditions Integrity of the entity There is no any attribute entering in composition of primary key of the relation that can accept null values –Motivation Primary key identifies the entity, and so the entity is always identified (distinguished) from other entities Referential integrity If the relation R2 contains a foreign key FK corresponding to primary key PK of the relation R1 (need not be different from R2), then each value of FK: –Either it equals the value of PK in some tuple of R1, or –It must be all null value (tuple in the relation R2 does not exist) Motivation If some tuple t2 from the relation R2 corresponds the tuple t1 from the relation R1, then t1 must exist

13 Relations and entity sets Representation of entity sets Each entity set is represented as a separate relation with a primary key equals to the primary key of the entity set Representation of relationships among entity sets “one to one: relationship: we represent it with a foreign key inserted to any of the two relations “one to many”: relationship: we represent it with a foreign key inserted to the n side relation of relationship. “many to many”: relationship: We represent by separate relation with a key being a composition of primary keys of the related entity sets Note that Representing a relationship by a separate relation can be applied also in case of “one to one” and “one to many” relationships –Advantages more elasticity (e.g. the resistance on change of relationship type), disadvantage: one relation more

14 Relations and entity sets - examples –Relational schema of database: Men (National-No, Name,…) Women (National-No, Name,…) Marriage (Nat-No-M REF Men, Nat-No-W REF Women, Date) Question: What will change if we consider all marriages (old and actual)? Men MarriageWomen National-No Nat-N0-MNat-N0-W Date National-No Name

15 Relations and entity sets - examples –Relational schema of database: Men (National-No, Name,…) Women (National-No, Name,…) Marriage (Nat-No-M REF Men, Nat-No-W REF Women, Date) Tylko definition of primary key has been changed because “one to one” relationships were altered to “one to many” relationships, and the foreign keys are on “many” side of the relationship Note that: The additional representation of relationships (Men, Marriage) and (Women, Marriage) is not necessary, because in composition of primary key of the relation Marriage, there are foreign keys correspond the primary keys of the relation Men and the relation Women Men MarriageWomen National No Nat-N0-MNat-N0-W Date National No Name

16 Relations and entity sets - examples Persons Passports National No No Date Name Possible relational database schema: 1. Persons (National-No, Name) Passports (No, Date, National-No REF Persons) 2. Persons (National-No, Name, Pass-No REF Passports) Passports (No, Date) 3. Persons (National-No, Name) Passports (No, Date) Pers-Pass (National-No REF Persons, Pass-No REF Passports) Which schema is the best? Pers-Pass

17 Clients Ordering No NameStatusOrd-NoDate Relations and entity sets - examples Possible relational database schema: 1.Clients (No, Name, Status) Ordering (Ord-No, Date, Client REF Clients) 2.Clients (No, Name, Status) Ordering (Ord-No, Date) Ord-Client (Ord-No REF Ordering, Client REF Clients) Which schema is better?

18 Persons Courses National No No Subject Name learn Date Relations and entity sets - examples Relational schema of database: Persons (National-No, Name) Courses (No, Subject, Date) Pers-Course (Person REF Persons, Course REF Courses) The relation Pers-Course is a proper place where to locate attributes related to participating in the course (e.g. degree): Pers-Course (Person REF Persons, Course REF Courses, degree)

19 Relations and entity sets - examples Private Lorries Reg_No Cars Year_Prod mark Num_Persons Luggage_bag Weight Load capacity IS_A Relational schemas of database: 1. Cars (Reg-No, mark, Year-Prod) Private (Reg-No REF Cars, Num-Persons, Luggage-bag) Lorries (Reg-No REF Cars, Load-capacity, Weight) Other schemas are also possible with IS_A relationship 2. Cars (Reg-No, mark, Year-prod) Private (Reg-No, Mark, Year-Prod, Num-Persons, Luggage-bag) Lorries (Reg-No, Mark, Year-Prod, Load-capacity, Weight) 3.Cars (Reg-No, Type, mark, Year-Prod, Num-Persons, Luggage-bag, Load-capacity, Weight) Which schema is the best? This relation contains other cars than Private and Lorries, if they exist

20 Relations and entity sets - examples Wholesaler Clients Goods No quantity date SWW Ord-no Name ordering Ord-no Serial-no Pos-ord Wholesaler Clients Goods No quantity date SWW Ord-no Name ordering Ord-no Pos-no Pos-ord Clients (No, Name, Status) Wholesaler (Name, Address) Goods (SWW, Name, Size) Ordering (Ord-No, Date, Client REF Clients, Wh REF Wholesaler) Pos-Ord (Ord-No REF Ordering, Pos-no, Good REF Goods, Quantity) Name Status Address Name Size

21 Relational database - example NoNameStatus 123-456-78-90Ahmad10 345-634-34-85Said21 987-543-65-32Ali30 234-765-73-87Kemal90 Ord- No DateClientWh 00112.01.1998 123-456-78-90 SMALL 00213.01.1998 123-456-78-90 NEW 00312.06.1997 345-634-34-85 ONLY HERE 00422.01.1994 987-543-65-32 SMALL 00512.11.1999 345-634-34-85 ONLY HERE 01215.06.1998 123-456-78-90 ABDATE 03412.07.1996 234-765-73-87 ABC 04523.01.1998 234-765-73-87 AAA Clients Ordering An example of tables Clients and Ordering from the relations of the previous database Clients (No, Name, Status) Ordering (Ord-No, Date, Client REF Clients, Wh REF Wholesaler)

22 An example of database – School1 Problem description Teachers of some school are described by the following attributes: identifier: TNO, Name: TNAME, academic title : TITLE, address: CITY. Each teacher may have superior Students of the school are described by: identifier: SNO, name: SNAME, starting study year: SYEAR, address: CITY During the term, each teacher has provided subjects for many students for HOURS hours. (For each student number of hours can be different). Each student has subjects with many teachers

23 An example of database – School1 STUDENTS TEAHING TEACHERS SNO SNAME SYEARCITY HOURS TNOTNAME TITLE CITY

24 An example of database – School1 STUDENTS TEACHERS SNO SNAME SYEARCITY HOURS TNOTNAME TITLE CITY TS SNO TNO Relational database schema TEACHERS(TNO, TNAME, TITLE, CITY, SUPNO REF TEACHERS) STUDENTS(SNO, SNAME, SYEAR, CITY) TS(TNO REF TEACHERS, SNO REF STUDENTS, HOURS)

25 TNOTNAMETITLECITYSUPNO T1BekirProfTaezNull T2SaidPhDSanaT1 T3JamalProfAdenT1 T4ClarkPhDCairoT3 T5AdamMScTaezT4 TEACHERS An example of tables for the database School1

26 SNOSNAMESYEARCITY S1Maher1975Taez S2Jameel1980Ebb S3Jamal1983Turba S4Humam1984Taez S5Fares1990Aden S6Hani1990Aden STUDENTS An example of tables for the database School1

27 TNOSNOHOURS T1S164 T1S264 T2S2100 T2S3120 T2S4120 T3S132 T3S236 T3S360 T3S572 T4S196 T4S396 T4S596 T5S632 An example of tables for the database School1 TS


Download ppt "Relations, Relational database Idea of relation Relational data base From the diagram ERD to database schema."

Similar presentations


Ads by Google