Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.

Slides:



Advertisements
Similar presentations
Database Languages Chapter 7. The Relational Algebra.
Advertisements

1 Welcome: To the fifth learning sequence “ Possible representation (2) “ Recap : In the previous learning sequence, we discussed four possible representations.
Unit 5 The Network Model  5.1 The Network Model  5.2 IDMS.
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Chapter 3 An Introduction to Relational Databases.
Wei-Pang Yang, Information Management, NDHU More on Normalization Unit 18 More on Normalization ( 表格正規化探討 ) 18-1.
N-dimensional space navigation is only possible in a logical rather han a physical world. They didn’t have any books to look after - their information.
1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT.
Domains, Relations & Base RelVars (Ch
A Framework for Testing Database Applications Joint work with Phyllis G. Frankl (Polytechnic) Saikat Dan (Polytechnic) Filippos Vokolos (Lucent Technologies)
Unit 2 DB2 and SQL Wei-Pang Yang, Information Management, NDHU Outline of Unit Overview DB2 2.2 Data Definition 2.3 Data Manipulation 2.4.
Introduction Chapter 1. Reference Book  Database Systems Thomas Connolly, Carolyn Begg, Anne Strachan Addison-Wesley 1999 ISBN:
Lecture by Prof.Dr.S.Sridhar 1 DataBase Technology by Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING.
Database Systems Lecture # 8 11 th Feb,2011. The Relational Model of Data The term relation is basically just a mathematical term for a table. DBMS products.
1 SELECT statement. 2 Sample database Supplier Part supplies (0,n) colour s# snamep# pname amount citydob price qualitydate.
CS370 Spring 2007 CS 370 Database Systems Lecture 7 The Relational model.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with.
6.8 Case Study: E-R for Supplier-and-Parts Database
Logical Database Design Unit 7 Logical Database Design.
1 Exercises of SQL uAnswer the following questions, based on the database below. wSupplier (SNO, SNAME, STATUS, CITY) wPart(PNO, PNAME, COLOR, WEIGHT)
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
Introduction to Data, Information and Knowledge Management Dr. Bhavani Thuraisingham The University of Texas at Dallas Data, Information and Knowledge.
4B-1 Wei-Pang Yang, Information Management, NDHU Introduction to Database CHAPTER 4B ( 補 ) DB2 and SQL  Overview  Data Definition  Data Manipulation.
1 Structured Query Language (SQL) CIS*2450 Advanced Programming Concepts.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
Logical Database Design ( 補 ) Unit 7 Logical Database Design ( 補 )
Wei-Pang Yang, Information Management, NDHU Normalization Unit 7 Normalization ( 表格正規化 ) 7-1.
SQL (Chapter 2: Simple queries; Chapter 7 and 8: Nested and DML queries) Many of the examples in this document are based on the tables in the next slide.
1 Data Manipulation in SQL  Department of Computer Science Northern Illinois University February 2001.
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
Further Normalization I
Data Definition After this lecture, you should be able to:
Unit 5 The Network Model  5.1 Data Modeling Issues  5.2 The Network Model  5.3 IDMS.
Algebra1 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.
1 SQL III CIS2450 Advanced Programming Concepts. 2 The Join Operation It is one of the most important features of a relational system that it allows you.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Perform DFS from A A B C D E F G. Perform BFS from A A B C D E F G.
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
Index in Database Unit 12 Index in Database 大量資料存取方法之研究 Approaches to Access/Store Large Data 楊維邦 博士 國立東華大學 資訊管理系教授.
Unit 2 DB2 and SQL. 2-2 Wei-Pang Yang, Information Management, NDHU Outline of Unit Overview 2.2 Data Definition 2.3 Data Manipulation 2.4 The System.
Views Prof. Yin-Fu Huang CSIE, NYUST Chapter 10. Advanced Database System Yin-Fu Huang 10.1Introduction Example: Var Good_Supplier View (S Where Status.
Data and Applications Security Developments and Directions Dr. Bhavani Thuraisingham The University of Texas at Dallas Lecture #2 Supporting Technologies:
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Relations with Stored and Inherited Attributes Witold Litwin Dauphine University June 13, 2016.
SQL Exercises. 1) Names of suppliers. Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost)
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
STRUCTURE OF PRESENTATION :
Lecture 3 : Structured Query Language (SQL)
תרגול מספר 10 מעבדת SQL מספר 3
Unit 2 DB2 and SQL.
for Computer Professionals
STRUCTURE OF PRESENTATION :
Introduction to Data, Information and Knowledge Management
Chapter 8 Relational Calculus.
Introduction to Database
Introduction to Database
پايگاه داده ها.
Φροντιστήριο SQL (από το βιβλίο του Date)
Advanced Database System
Analyzing and Securing Social Networks
Unit 7 Normalization (表格正規化).
“ Possible representation (2) “
STRUCTURE OF PRESENTATION :
Question 1: Basic Concepts (45 %)
STRUCTURE OF PRESENTATION :
SQL.
Unit 12 Index in Database 大量資料存取方法之研究 Approaches to Access/Store Large Data 楊維邦 博士 國立東華大學 資訊管理系教授.
Presentation transcript:

Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.  Join Operation  Aliasing of Tables  Brainstorm on Assignment 3 (Part 1). Data Manipulation 2

2 Review: Get the S#'s, cities, and statuses of the suppliers in Athens or Paris. Sort the resultant table in the descending order of statuses. sno | city | status s3 | Paris | 30 s5 | Athens | 30 s2 | Paris | 10 select sno, city, status from s where city in (‘Athens’, ‘Paris’) order by status desc; Table S sno | sname | status | city s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens

Data Manipulation 23 Select Statements  Select columns – Projection  From – Database tables  Where -- Selection condition  Group By – Partitioning data into groups  Having -- Group selection condition  Order By -- Sorting Select [Distinct] column(s) From table(s) Where condition [ Group By field(s) ] [ Having condition ] [ Order By field(s) ] ;

Data Manipulation 24 Get the names and cities of the suppliers who supplied P4. Table S Table SP sno | sname | status | city sno | pno | qty s1 | Smith | 20 | London s1 | p1 | 300 s2 | Jones | 10 | Paris s1 | p4 | 200 s3 | Blake | 30 | Paris s2 | p3 | 400 s3 | p4 | 100 select sname, city from s, sp where s.sno = sp.sno and sp.pno = ‘p4’; sname city Smith London Blake Paris Join Operation

Data Manipulation 25 Who supplied blue parts? Table S Table P sno | sname | sts | city pno | pname | color | wgt | city s1 | Smith | 20 | London p1 | nut | red | 12 | London s2 | Jones | 10 | Paris p2 | bolt | green | 17 | Paris p3 | screw | blue | 17 | Rome Table SP sno | pno | qty s1 | p1 | 300 s2 | p3 | 200 select sname from s, sp, p where s.sno = sp.sno and sp.pno = p.pno and color = 'blue' ; sname Jones

Data Manipulation 26 List the names and the colors of the parts supplied by the suppliers located in London. select pname, color from s, p, sp where s.sno = sp.sno and sp.pno = p.pno and s.city = ‘London’ ; pname | color nut | red bolt | green Table S Table P sno | sname | sts | city pno | pname | color | wgt | city s1 | Smith | 20 | London p1 | nut | red | 12 | London s2 | Jones | 10 | Paris p2 | bolt | green | 17 | Rome p3 | screw | blue | 17 | Rome Table SP sno | pno | qty s1 | p1 | 300 s1 | p2 | 250 s2 | p3 | 200

Data Manipulation 27 Aliasing of Tables Which parts are of the same color? select px.pno, py.pno, px.color from p px, p py where px.color = py.color and px.pno < py.pno ; px.pno | py.pno | color p2 | p3 | blue p1 | p4 | red Table P pno | pname | color p1 | nut | red p2 | screw | blue p3 | cam | blue p4 | cog | red Table P PX Table P PY pno | pname | color p1 | nut | red p1 | nut | red p2 | screw | blue p2 | screw | blue p3 | cam | blue p3 | cam | blue p4 | cog | red p4 | cog | red

Data Manipulation 28 Get the (distinct) names of the cities where both blue and green parts are located? select distinct px.city from p px, p py where px.color = ‘blue’ and py.color = ‘green’ and px.city = py.city; City London Rome Table P pno | pname | color | wgt | city p1 | nut | blue | 12 | London p2 | bolt | green | 17 | Rome p3 | screw | blue | 15 | Rome p4 | cog | green | 18 | London p5 | cam | red | 19 | Athens

Data Manipulation 29 Get the names of the suppliers who supplied p1 and p2. Table S Table SP sno | sname | status | city sno | pno | qty s1 | Smith | 20 | London s1 | p1 | 300 s2 | Jones | 10 | Paris s1 | p3 | 200 s3 | Blake | 30 | Paris s2 | p1 | 400 s2 | p2 | 100 s2 | p3 | 50 s3 | p2 | 80 select sname from s, sp spx, sp spy where s.sno = spx.sno and spx.pno = ‘p1’ and s.sno = spy.sno and spy.pno = ‘p2’; sname Jones