Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations;

Slides:



Advertisements
Similar presentations
Joining Relations in SQL Objectives of the Lecture : To consider the Natural & Generalised Joins using the SQL1 standard; To consider the Natural & Generalised.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
Virtual training week 4 structured query language (SQL)
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
CIT 613: Relational Database Development using SQL Introduction to SQL.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
Candidate Keys Objectives of the Lecture : To consider Integrity Constraints; To consider the principles of Candidate Keys; To consider Candidate Keys.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
Query Evaluation. SQL to ERA SQL queries are translated into extended relational algebra. Query evaluation plans are represented as trees of relational.
Introduction to Structured Query Language (SQL)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Introduction to Structured Query Language (SQL)
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
Introduction to Structured Query Language (SQL)
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Rutgers University Relational Algebra 198:541 Rutgers University.
Query Design Objectives of the Lecture : To learn a strategy for designing queries. To learn how to use relational algebra concepts to implement the strategy.
Relational Algebra, R. Ramakrishnan and J. Gehrke (with additions by Ch. Eick) 1 Relational Algebra.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Objectives of the Lecture :
DATABASE MANAGEMENT SYSTEMS BASIC CONCEPTS 1. What is a database? A database is a collection of data which can be used: alone, or alone, or combined /
Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Concepts and Terminology Introduction to Database.
The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
Natural vs. Generated Keys. Definitions Natural key—a key that occurs in the data, that uniquely identifies rows. AKA candidate key. Generated key—a key.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Amendments & Transactions Objectives of the Lecture : To consider amendments to a relation; To consider transactions in SQL.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 Relational Algebra and Calculas Chapter 4, Part A.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 17 A First Course in Database Systems.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in.
Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider.
In this session, you will learn to: Describe data redundancy Describe the first, second, and third normal forms Describe the Boyce-Codd Normal Form Appreciate.
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
INTRODUCTION lecture1 1. Data base concept Data is a meaningless static value. What does 3421 means? Information is the data you process in a manner that.
Insert & Delete Objectives of the Lecture : To consider the insertion of tuples into a relation; To consider the deletion of tuples from a relation; To.
CIT 613: Relational Database Development using SQL Introduction to SQL DeSiaMorePowered by DeSiaMore 1.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
Further Consolidation Objectives of the Lecture : To use simple queries to check out a DB. To use insertions, deletions and amendments to maintain a DB.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
SQL and Relational Algebra Edel Sherratt Nigel Hardy Horst Holstein.
LECTURE 3: Relational Algebra
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
INTRODUCTION A Database system is basically a computer based record keeping system. The collection of data, usually referred to as the database, contains.
Presentation transcript:

Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations; To consider the Project operator and its use in SQL retrievals.

Using a Relational DB 2. specifying the action to be taken with it/them A C B There are two parts to using a DB : A C B 1. defining the relation(s) to be used;

Actions on a DB l Actions are expressed in SQL statements. l The keyword or key phrase that starts an SQL statement determines what the statement’s action is. Examples : Create (Table) Alter (Table) Insert (Into), Delete (From) Update, Commit and Rollback. l Data retrieval is another action. It is the raison d’etre of any DB. There would be little point in a DB whose data could not be retrieved. l The SQL keyword that starts a retrieval statement is Select.

Retrieval Actions l The SQL Select statement must comprise the following two phrases : Select ……. From ……. ; Optionally it may contain other phrases. l Retrievals are sometimes known as Queries, because the data retrieved can be thought of as the answer to a question. Example :- “Which employees are married ?” Data from the DB provides the answer. l On this course, the DBMS is set up so as to always retrieve data to our computer screen, so that we can see it. (However SQL Select does permit retrievals to other locations).

Defining Relations for Actions l Relational algebra or relational calculus languages are used to define a relation that is to be acted on. l A defined relation may be : l a part of a DB relation, l A merger of 2 or more DB relations, l part of a ‘merged’ relation. l Relational algebra / calculus language can provide the great power and flexibility, together with conceptual simplicity, needed to define ‘relations-to-be-acted-on’. l SQL is a mixture of relational algebra and calculus plus other ‘ad hocery’  needlessly complex.

Relational Algebra (1) l The SQL language is based on a mixture of relational algebra, relational calculus, and ad hoc peculiarities of its own. l Relational algebra is simpler than SQL. l Therefore for simplicity, this course follows the tradition of using relational algebra to define the relation to be retrieved. It is then expressed in SQL for execution. l Relational algebra consists of : l a number of relational operators; a monadic operator operates on one operand, i.e.one relation, to produce a single relation as a result; a dyadic operator operates on two operands to produce a single result relation. l A way of combining relational operators together to form a relational expression.

Relational Algebra (2) l “Closure Under the Algebra” In 3 - ( ( 5  6 ) + 2 ) ( 5  6 ) results in a number, 30; so the next calculation is (30 + 2), giving 32; the last calculation is (3 - 32), giving -29. Each operator generates another number, i.e. closure. Similarly each relational operator generates another relation. In both cases, arbitrarily complex formulae can be built up. Relational algebra is based on the same concepts as arithmetic algebra. l Intuitively useful operators : Examples :

Designing Retrievals l Designing a retrieval means defining the relation to be retrieved, since it is only necessary to prefix the SQL definition with Select for it to be retrieved. l As each relational algebra operator encapsulates an intuitively useful concept, with a universally accepted name for referencing it, the following method is used to learn how to write queries : l Learn a number of individual algebra operators and how to express them in SQL. l Learn how to combine operators to create powerful expressions, and how to write the expressions in SQL. l SQL uses a standard fixed format for writing expressions. This is straightforward for simple and reasonable queries, but can be constraining for complex queries.

ACBD R B Example of Projection (1) B R Project[ B, D ] D D

R Project[ A, C ] ACBD R A Example of Projection (2) A R Project[ A, C ] C C

Definition of ‘Project’ 1. Creates a new relation containing only the specified attributes. 2. Any would-be duplicate tuples are removed, to ensure the result is a set of tuples. 3. The result’s attribute names are those of the operand’s attribute names specified in the parameter to the Project operation. If the specified attribute(s) include a candidate key, then there can be no duplicate tuples to remove.

Characteristics of Projection l Don’t need to know about any constraints on the operand relation in order to be able to do a projection. l Boundary Cases : l If all the attributes are specified, then result  operand l If no attributes are specified, then result  nullary relation i.e. result has no attributes. l In principle, the unwanted attributes can be specified instead, i.e. those to be removed. Example : in relation R( A, B, C, D ) R Project[ ~ B, D ]  R Project[ A, C ]

SQL : Projection Principles : l Put the operand relation’s name in the From phrase. l Put the attributes to be projected out in the Select phrase. l This SQL statement also retrieves the newly created relation from the DB. Examples : SQL equivalent of the 2 example projections on relation ‘R’ :- SelectB, DSelectA, C FromR ;FromR ;

SQL : Duplicate Rows l SQL does not remove duplicate rows automatically. Problem ! l SQL must be instructed to remove duplicates, by inserting the Distinct keyword between the Select keyword and the list of attribute names. Examples :- Select Distinct B,D FromR ; Select Distinct A, C FromR ; This does a real projection; the earlier version did not. As there were no duplicate tuples, Distinct is strictly unnecessary; but it is still a correct statement.

SQL : Removing Duplicate Rows l The reason why SQL is not designed to remove duplicate tuples automatically is because, originally, this was thought to need too much computing power. l This issue is now contentious : 1.Computers are now much more powerful. 2.Much more serious performance optimisation problems can be created by retaining duplicates than by removing them. Advice : l Always initially insert Distinct, unless a candidate key in the result ensures no problems of duplicates. l IF performance cannot then be made fast enough THEN decide whether performance or lack of duplicates is more important, and choose the preferred option.

SQL : Projection Characteristics l To project out all the attributes in a relation, use * in the Select phrase. Example :- Select * From R ; The result is the same as relation ‘R’. (Thus a retrieval of a whole table is in fact a retrieval of a projection of all the attributes). l Nullary relations. These are not permitted in SQL. If nothing is put between the Select and From keywords, a syntax error will be issued. l SQL does not allow the specification of unwanted attributes in the Select phrase.