Relational databases.  Retrieving data from a database requires pulling data from multiple tables  Tables relate to each other in distinct ways, modelled.

Slides:



Advertisements
Similar presentations
Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
What is a Database By: Cristian Dubon.
Joining Relations in SQL Objectives of the Lecture : To consider the Natural & Generalised Joins using the SQL1 standard; To consider the Natural & Generalised.
Mid-term Exam Answer Keys Week 7 Spring I. Multiple Choices Questions Each may have more than one answer (6 x 4 = 24 points total)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
The Relational Database Model:
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
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.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
Page 1 ISMT E-120 Desktop Applications for Managers Introduction to Microsoft Access.
Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter.
The Relational Database Model
The Relational Database Model
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
Relational Database Concepts. Let’s start with a simple example of a database application Assume that you want to keep track of your clients’ names, addresses,
Over the Past 2 weeks we have looked (in brief) at the basic elements of a relational database.  What we understand by the phrase database?  How the.
DAY 14: ACCESS CHAPTER 1 Tazin Afrin October 03,
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
CTFS Workshop Shameema Esufali Suzanne Lao Data coordinators and technical resources for the network
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
The Relational Database Model
1 Outline  What is a Primary Key?  AutoNumber primary keys  Single-field primary keys  Composite-field primary key  About Foreign Keys  Database.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
Relational Databases.  In week 1 we looked at the concept of a key, the primary key is a column/attribute that uniquely identifies the rest of the data.
Access Review. Access Access is a database application A database is a collection of records and files organized for a particular purpose Access supports.
Database Systems, 9th Edition 1.  In this chapter, students will learn: That the relational database model offers a logical view of data About the relational.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 3 The Relational Database Model.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
CTFS Workshop Shameema Esufali Asian data coordinator and technical resource for the network
Normalisation RELATIONAL DATABASES.  Last week we looked at elements of designing a database and the generation of an ERD  As part of the design and.
Understand Primary, Foreign, and Composite Keys Database Administration Fundamentals LESSON 4.2.
Relational Databases. SQL Sub-queries: queries within queries  So far when data has been filtered the filter has been known and simply added to the Where.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
Chapter 3 The Relational Database Model. Database Systems, 10th Edition 2 * Relational model * View data logically rather than physically * Table * Structural.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Week 2 Lecture The Relational Database Model Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
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.
Year 12 > 13 Applied GCE ICT Unit 7 Using Database Software.
Introduction to Database Programming with Python Gary Stewart
INFO 340 Lecture 3 Relational Databases. Based on the relational model, grounded in mathematic set theories. Three basic elements: Relation, Tuple, and.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
N5 Databases Notes Information Systems Design & Development: Structures and links.
SQL (Structured Query Language)
The Relational Database Model
 2012 Pearson Education, Inc. All rights reserved.
Relational Databases.
Database Systems: Design, Implementation, and Management Tenth Edition
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
SQL 101.
Normalization Referential Integrity
CS4222 Principles of Database System
Databases and Information Management
Displaying Data from Multiple Tables
Database Systems: Design, Implementation, and Management
Presentation transcript:

Relational databases

 Retrieving data from a database requires pulling data from multiple tables  Tables relate to each other in distinct ways, modelled by ERD and other tools  There are a variety of ways to force the relationships and define the ‘joining’ of data as part of the retrieval (select) statements  This week will look in detail at the ways we interlink the tables during the data retrieval.

 Sometimes the answer to a query requires data from two or more table/relations.  There are several kinds of Join operators that will combine two relations into one.  There is no limit to the number of tables/relations that may be joined.  If the result of a join has more data in it than we want, we can prune it down to the required size using select/Project and/or where/Restrict operations before retrieving the final result.

The simplest & crudest way to join 2 tables/relations into one is to apply a Cartesian Product join.  Cartesian Product creates a new relation/temp table whose records are formed by merging every record of the first operand with every record of the second operand, i.e. all possible combinations of records/tuples.  The operands must have no attribute names in common, as otherwise the result would have duplicate attribute names.  The result is rarely useful and should be avoided as they have been known to bring down database servers.

Player idNameLocation CGEP1Emma-JaneUK CGAT3AnthonyUK CGGM2GlenUK ISGG2GilbertIRL GamePlayerScore SpyroCGEP Dog IslandCGEP Player Game Player idNameLocationGamePlayerScore CGEP1Emma-JaneUkSpyroCGEP CGEP1Emma-JaneUKDog IslandCGEP CGAT3AnthonyUKSpyroCGEP CGAT3AnthonyUKDog IslandCGEP CGGM2GlenUKSpyroCGEP CGGM2GlenUKDog IslandCGEP ISGG2GilbertIRLSpyroCGEP ISGG2GilbertIRLDog IslandCGEP Result Join result

 Effective joins should ‘link’ following the integrity rules laid down in the database creation or ERD  Foreign keys are normally used (we have touched on these but they are covered in much more detail in a week or 2)  Can join on any columns in theory but following RI is the effective and productive way to get the most out of the database systems.

 The same attribute name cannot appear in both operands.  The two attributes compared in a condition must  have the same data type so that it is possible to compare them,  be in different relations, otherwise a tuple from one operand cannot be related to a tuple in the other..  Comparisons can be combined together with Boolean Operators (i.e. AND, OR, and NOT) to form one composite condition but this module will focus on the = comparison as this is the one that is used in practice.

Data from the student table contains the names of the students Data from the enrolled tables contains the details of the subjects the students are studying. The RI between the tables is the student ID Student Enrolled If we want the student name not the id we need to join these 2 tables. Joins can be done in multiple ways, the first is to join in the where clause.

A join can be done in the where clause, this uses the boolean condition check to determine the inclusion of the record/tuple in the result, essentially a Cartesian product is done then the data filtered so only those matching the criteria are in the final result. SQL> select stuname, subjectid 2 from enrolled, student 3 where student.studentid = enrolled.studentid 4 and subjectid = 'COMP0055'; STUNAME SUBJECTI Tony Smith COMP0055 Faye Simpson COMP0055 Thomasina Jones COMP0055 Josiah Roughton COMP0055 Anne-Marrie Jones COMP0055 *THIS IS THE WAY WE HAVE BEEN DOING IT IN THE SEMINARS* This retrieves all data then discards where the studentid’s do not match or the subjectid is not COMP0055 resulting in:

 The previous join is commonly known as an equi join, it requires the 2 values to be identical in format and value. The column/attribute names do not have to be the same but they have to have the same meaning.  Stuid may be the same as studentid  Module may be the same as subjectid etc....  The code should not retrieve both columns that are in the join as they are duplicated (because the = is used they must be the same)  Where the columns have the same name the code must indicate which column is being retrieved by prefixing the attribute/column with the data source.  Student.studentid or enrolled.subjectid

select stuname, studentid, subjectid from enrolled, student where student.studentid = enrolled.studentid and subjectid = 'COMP0055'; * ERROR at line 1: ORA-00918: column ambiguously defined 2 columns have same name systen doesn’t know which to display

SQL> select stuname,student.studentid, subjectid 2 from enrolled, student 3 where student.studentid = enrolled.studentid 4 and subjectid = 'COMP0055'; STUNAME STUDENTID SUBJECTI Tony Smith COMP0055 Faye Simpson COMP0055 Thomasina Jones COMP0055 Josiah Roughton COMP0055 Anne-Marrie Jones COMP0055 Pull studentid from student table

If we join tables in the way described earlier we have 2 primary problems. 1. Duplicate attribute names in the result; because attributes containing the same data in different relations usually have the same names. 2. Duplicate data in the result; due to the ‘=‘ comparison. Solution One  Rename the duplicate attribute(s) in one operand to something unique. This solves the duplicate name problem, then do an equi join.  Use a Projection operation to remove the duplicate attribute data. (This is the most common solution) Solution Two Use a Natural Join operator. (better practice!)

A special case of an Equi Join where :  all the attribute(s) to be compared must have the same name(s) and the same data type(s),  the duplicate attribute(s) are automatically removed from the result by the operator. This is much the most useful join operator in practice. Note :If there are duplicate attribute names in the operands, but they are not to be compared and used in the join, a Natural Join operation will not do and most large scale database systems are developed in teams or evolve over time so many do not have the same names.

 Very effective way of joining tables but does assume shared naming which in industry often doesn’t happen (although often aimed for) SQL> select stuname, subjectid 2 from enrolled natural join student 3 where subjectid = 'COMP0055'; STUNAME SUBJECTI Tony Smith COMP0055 Faye Simpson COMP0055 Thomasina Jones COMP0055 Josiah Roughton COMP0055 Anne-Marrie Jones COMP0055 ‘Natural join’ key word Will assume that the Common attribute Studentid is the Joining attribute

The seminar this week will be looking at retrieving data from multiple tables and joining the tables in the FROM clause using natural or equi joins in place of joining in the WHERE clause as done in week 3 and 4 You will also be required to undertake self study to expand your understanding of the join syntax (this will be build upon in the next lecture)