Week 2 The Relational Data Model

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Relational Database Operators
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
The Relational Database Model
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
The Relational Database Model
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
ITBIS373 Database Development
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.
The Relational Database Model
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Relational Algebra – Part 2
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 3 The Relational Database Model.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
3 1 Database Systems The Relational Database Model.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter (6) The Relational Algebra and Relational Calculus Objectives
COMP3017 Advanced Databases
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
The Relational Database Model
Oracle Join Syntax.
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Database Systems: Design, Implementation, and Management Tenth Edition
Chapter 4 Relational Model Characteristics
Displaying Data from Multiple Tables
Lecture 2 The Relational Model
Displaying Data from Multiple Tables
Using Subqueries to Solve Queries
Theory behind the relational engine
Theory behind the relational engine
David M. Kroenke and David J
Chapter 3 The Relational Database Model
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Creating and Maintaining
Structured Query Language
(SQL) Displaying Data from Multiple Tables
Using Subqueries to Solve Queries
DATABASE SYSTEM.
The Relational Database Model
The Relational Algebra
Contents Preface I Introduction Lesson Objectives I-2
Chapter # 3 The Relational Database Model.
DCT 2053 DATABASE CONCEPT Chapter 2.2 CONTINUE
Subqueries Schedule: Timing Topic 25 minutes Lecture
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Systems: Design, Implementation, and Management
Presentation transcript:

Week 2 The Relational Data Model CSE9002 Week 2 The Relational Data Model

Joins of Tables The joining of attributes depends on certain types of relationships; Consider two attributes C1 and C2 which are join attributes There are 4 types of relationships possible (a) the values of C1 and C2 are equal (b) the values of C1 are a subset of those of C2 (or vice versa) (c) the values of C1 and C2 are conjoint - they have some values in common (d) the values of C1 and C2 are disjoint - they have no values in common

Joins of Tables In set theory, these take the forms (a) C1 = C2 (b) C1  C2 or C2  C1 (c) C1 - C2  0 or C2 - C1  0 (d) C1 - C2 = C1 and C2 - C1 = C2

Joins of Tables There are a number of possible ‘join’ types allowable in the relational model They are: 1. Thetajoin 2.Equijoin 3. Natural join 4. Inner join 5. Outer join 6. Left Outer join 7. Right Outer join 8. Full Outer join

Joins of Tables 1. Thetajoin This is the case when the ‘condition of join’, or comparator, is of the form < = > ! or any combination (>=) as in the case select * from parts, rates where parts.partno (< > =) rates.partno 2. If the comparator is =, the join is a equijoin 3. If the select clause contains the name of only one of the join attributes, this results in a natural join (if the clause had been stated as select parts.partno, rates.partno, then both these attributes would have been in the result table)

Joins of Tables 4. Inner Join This is based on there being 2 tables - e.g. members and penalties as in a video club members, and penalty tables. This results in only that data which is supported by the comparator being presented in the result table. The only results will be those rows which satisfy the join condition of (in this case) = (equality). Any other non-matched rows will not be displayed. It is to be expected that not all members will have penalties, and so only some of the members will be listed.

Joins of Tables 5. Outer join. There may be other data which the user wanted to ‘see’ but it would have been excluded. A case could be where a librarian wants to see who has incurred a fine for late or non return of videos, but also wants to see that the other customers (a) are listed (b) are shown as 0 penalty (even though this is not a value as the row would not exist in the penalties table) This generally extends the query to a union and a substitute attribute.

Joins of Tables 6. Left Outer join This is a type of outer equijoin, and takes its name from the use of the ‘left’ table as the dominant table. The result table will only include values from the secondary table where the equijoin is valid. The result is the ‘intersection’ of the populations of the 2 tables involved. The ‘left’ table is the first named table in the from clause

Joins of Tables 7. Right outer join The dominant attributes are those from the table named as the rightmost position in the from clause. 8. Full Outer Join All rows from both tables named in the from clause are processed. This is the same as a union of the left and right outer joins of the tables involved.

Joins of Tables Some pointers: 1. Some versions of SQL allow for the nomination of the join type (see example) 2. The correct use of the various joins depends on the knowledge of table content and of course the clear understanding and statement of the required result table content 3. The complex joins invariably require sub-queries which may require the exist or not exist or the in or not in operators, and the union function

Remote Presentation Client/Server Desktop Host Presentation Services Data Services Presentation Logic File Services Business Logic Data Logic

3 Tiered Client Server 3 Tiered Client/Server Database Desktop Application Server DataBase Server Presentation Services Business Logic Data Services Presentation Logic Data Logic File Services

Mirror Tables Base Tables (disk located) and Virtual Tables (memory) Consider a table ‘employee’ which contains staff detail supervisor detail manager details Select a.name,b.name,c.name,d.name,e.name from employee a, employee b, employee c, employee d, employee e; would produce a results table of all names in 5 columns of output

Joining a Table to Itself Typical Query: For each employee, list the employee number, name Manager and Manager’s name Select X.EMPNO, X.NAME, X.MGR, Y.NAME from EMP X, EMP Y where X.MGR = Y.EMPNO Result: EMPNO NAME MGR NAME 10 SMITH 40 BROWN 20 JONES 40 BROWN 30 BLACK 40 BROWN 40 BROWN 50 WHITE The Primary Key and the Foreign are both in the same table Two virtual tables are created for joining (‘alias’ feature)

DIVISION Divides a BINARY relation by a UNARY relation and produces a UNARY relation as a result. skill-reqd result emp-skill empno empno skillcode skillcode E2 S2 S4 E1 E2 E3 E5 E6 S1 S2 S3 S4 S5 S6 Divide emp-skill by skill-reqd to give result Special note: JOIN, INTERSECTION and DIVISION can be defined in terms of the other 5 operators (which are known as the ‘primitive’ operators).

A DIVISION example In the Air Transport Industry, pilot’s records contain details of the aircraft they are qualified to fly. And there are also records of the number and types of aircraft in the hangars and which Company owns what. In this case, the table of pilot’s names and the planes they can fly is the dividend The details of the planes in the hangars is the quotient

A DIVISION example The query is to obtain the names of the pilots who can fly every type of plane in the hangars

Suggested Solution create table pilotskill (pilot vchar (150) not null, plane vchar(15) not null); create table hangar (plane vchar(15)); select pilot from pilotskill ps1, hangar h1 where ps1.plane = h1.plane group by ps1.pilot having count(ps1.plane = select count(*) from hangar);

Division [notice the absence of any ‘division’ operator - this is effectively performed by the execution plan]

Division Examples A B C J K L 1 3 1 J 1 K 1 L 2 J 2 K 3 K 3 L 3 J Result J K L 1 3 1 J 1 K 1 L 2 J 2 K 3 K 3 L 3 J

Division Examples Name Degree D1 D2 B Sc M Sc D3 B Sc Jones B Sc Jensen B Sc Jensen M Sc Jensen PhD Smith B Sc Smith M Sc Rogers B Sc Rogers PhD M Sc B Sc PhD R3 Jones Jensen Smith Rogers R2 Jensen Smith R1 Jensen