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

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

Chapter 3 : Relational Model
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Relations The Relational Data Model John Sieg, UMass Lowell.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Fundamentals of Relational Database Yong Choi School of Business CSUB, Bakersfield.
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
Entity Relationship Model Real world modeling Entity, key of entity Entity Relationship Model (ERD) examples.
1 Minggu 2, Pertemuan 3 The Relational Model Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
CS424 PK, FK, FD Normalization Primary and Foreign Keys Primary and foreign keys are the most basic components on which relational theory is based. Primary.
Michael F. Price College of Business Chapter 6: Logical database design and the relational model.
Database Systems Relational Model Concepts Toqir Ahmad Rana Database Management Systems 1 Lecture 17.
Database Architecture The Relational Database Model.
Chapter 5 Relational Model Concepts Dr. Bernard Chen Ph.D. University of Central Arkansas.
CS 380 Introduction to Database Systems (Chapter 5: The Relational Data Model and Relational Database Constraints)
1 Relational model concepts Key constraints Referential integrity constraint Steen Jensen, autumn 2013.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Normalization of relational database Data redundance Second Normal Form Third Normal Form Forth Normal Form.
Chapter 4 The Relational Model Pearson Education © 2014.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
Module Title? DBMS E-R Model to Relational Model.
Relational Model Session 6 Course Name: Database System Year : 2012.
Chapter 4 The Relational Model.
1 Introduction to modeling Relational modelling Slides for this part are based on Chapters 11 from Halpin, T. & Morgan, T. 2008, Information Modeling and.
Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems.
Modelling Techniques - Normalisation Description and exemplification of normalisation.Description and exemplification of normalisation. Creation of un-normalised.
Concepts and Terminology Introduction to Database.
Relational databases and third normal form As always click on speaker notes under view when executing to get more information!
Database Management COP4540, SCS, FIU Relational Model Chapter 7.
CG084&085 / / 1 The Relational Data Model Properties of Relations Keys and Constraints.
Fundamentals of Relational Database Yong Choi School of Business CSUB, Bakersfield.
Topic 5 The Relational Data Model and Relational Database Constraints Faculty of Information Science and Technology Mahanakorn University of Technology.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model.
10/9/20151 The Relational Data Model TCU Database Systems Last update: September 2004 Reference: Elmasri 4 th edition, chapter 5.
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Data Model and Relational Database Constraints by Pinar Senkul resources:
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
Relational Database. Database Management System (DBMS)
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
Relational Algebra Relational Operators – Set operations –Special relational operators Relational Algebra Expressions PROJECT (SELECT FROM (A TIMES B)…
1 Functional Dependencies and Normalization Chapter 15.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Relational Model E.F. Codd at IBM 1970 Chapter 3 (ed. 7 – Chap. 5)
1 ER Modeling BUAD/American University Mapping ER modeling to Relationships.
Chapter 5 The Relational Data Model and Relational Database Constraints Copyright © 2004 Pearson Education, Inc.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
Mapping ER to Relational Model Each strong entity set becomes a table. Each weak entity set also becomes a table by adding primary key of owner entity.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
Logical Database Design and Relational Data Model Muhammad Nasir
IT 5433 LM2 ER & EER Model. Learning Objectives: Explain importance of data modeling Define and use the entity-relationship model Define E/R terms Describe.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
Lecture # 14 Chapter # 5 The Relational Data Model and Relational Database Constraints Database Systems.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
Tables and Their Characteristics
Database Design The Relational Model Text Ch5
Lecture # 13 (After 1st Exam)
Constraints AND Examples
Data Base System Lecture 6: Relational Model
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
The Relational Model Transparencies
INSTRUCTOR: MRS T.G. ZHOU
Introduction to modeling
Constraints AND Examples
RELATIONAL DATA MODEL - 1
Presentation transcript:

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

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)

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. {}

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 Ahmad Said Ali Kemal19 An example of a table representing a relation with three attributes:

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.

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)

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

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

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

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

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.

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

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

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

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

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

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?

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)

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

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

Relational database - example NoNameStatus Ahmad Said Ali Kemal90 Ord- No DateClientWh SMALL NEW ONLY HERE SMALL ONLY HERE ABDATE ABC 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)

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

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

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)

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

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

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