Database Languages Chapter 7. The Relational Algebra.

Slides:



Advertisements
Similar presentations
February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL.
Advertisements

SQL Rohit Khokher.
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Chapter 3 An Introduction to Relational Databases.
Concepts of Database Management Sixth Edition
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Domains, Relations & Base RelVars (Ch
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
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.
Concepts of Database Management, Fifth Edition
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
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 ( 補 )
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Concepts of Database Management Seventh Edition
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Data Definition After this lecture, you should be able to:
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Getting to Know SQL. © Jim Hope 2002 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement TRANSFORM.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.
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.
Algebra1 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL – Structured Query Language
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
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.
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.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
(SQL - Structured Query Language)
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
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.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Chapter 3: Relational Databases
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
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.
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.
SQL - Training Rajesh Charles. Agenda (Complete Course) Introduction Testing Methodologies Manual Testing Practical Workshop Automation Testing Practical.
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
Teacher Workshop Database Design Pearson Education © 2014.
Structured Query Language
SQL Query Getting to the data ……..
STRUCTURE OF PRESENTATION :
Unit 2 DB2 and SQL.
Chapter 8 Relational Calculus.
پايگاه داده ها.
Φροντιστήριο SQL (από το βιβλίο του Date)
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language – The Fundamentals
Unit 7 Normalization (表格正規化).
Contents Preface I Introduction Lesson Objectives I-2
STRUCTURE OF PRESENTATION :
SQL: Structured Query Language
Question 1: Basic Concepts (45 %)
Presentation transcript:

Database Languages Chapter 7

The Relational Algebra

The relational Algebra  The relational algebra is a complete set of operations on relations which allows to select data from a relational database.  Cartesian product  Union, Intersection, Difference  Projection   -join  Division

Sample database R r1r2r3 S1 s1s2 1x33p 4x34q 3y44p 2z7 S2 s1s2 4q 2m

Cartesian product R x S2r1r2r3s1s2 1x34q 4x34q 3y44q 2z74q 1x32m 4x32m 3y42m 2z72m

Projection,  -join Projection R [ r2, r3 ] r2r3 x3 y4 z7  -join R [ r3 > s1 ] S1 r1r2r3s1s2 3y43p 2z73p 2z74q 2z74p

Left Outer-join r1r2r3s1s2 1x33p 4x33p 3y44q 3y44p 2z7 R[r3 = l s1]S1

Union, Intersection, Difference UNION S1    S2 s1s2 3p 4q 4p 2m Intersection S1   S2 s1s2 4q Difference S1 \ S2 s1s2 3p 4p

Division S#P# s1p1 s1p2 s1p3 s1p4 s1p5 s1p6 s2p1 s2p2 s3p2 s4p2 s4p4 s4p5 Divide by ÷ Result DEND/DOR DEND DOR 1 P# p1 DOR 2 DOR 3 P# p2 p4 P# p1 p2 p3 p4 p5 p6 S# s1 s2 S# s1 s4 S# s1

Example S S#Sname 1Jones 2Duval 3Codd 4Carter SP S#P# Give all parts 2.Give names of suppliers supplying part 15 3.Give those suppliers that do not supply part 15 4.Give those suppliers that supply something else than part 15 5.Give those suppliers that supply something but not part 15

Solutions 1.SP [ P# ] 2. ( ( S [ S# = S# ]SP )[ P# ÷ 15] [15 ]) [ Sname ] 3.

Data Description Language

Sample Database SS#SNAMESTATUSCITY S1Smith20London S2Jones10Paris S3Blake30Paris S4Clark 20London S5Adams30Athens PP#PNAMECOLORWEIGHTCITY P1NutRed12London P2BoltGreen17Paris P3ScrewBlue17Rome P4SrewRed14London P5CamBlue19London P6CogRed19London SPS#P#QTY S1P1300 S1P2200 S1P3400 S1P5200 S1P6100 S2P1300 S2P2400 S3P2200 S4P2200 S4P4300 S4P5400

CREATE CREATE TABLE base-table-name ( base-table-element - commmalist ) where base-table-element is a column-definition or a base-table-constraint-definition column-definition: column representation [ default definition ] default definition: NOT NULL, NULL, current-date,....) Create Table

Base-table-constraint  candidate key: UNIQUE ( column-commalist )  primary key: PRIMARY KEY ( column-commalist )  foreign key: FOREIGN KEY ( column-commalist ) REFERENCE base-table [ ( column-commalist ) ] [ ON DELETE option ] [ ON UPDATE option ] option: NO ACTION, CASCADE, SET DEFAULT, SET NULL  check constraint: CHECK ( conditional-expression )

CREATE table example CREATE TABLE SP (S# S# NOT NULL, P# P# NOT NULL, QTY QTY NOT NULL, PRIMARY KEY ( S#, P# ) FOREIGN KEY ( S# ) REFERENCE S ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY ( P# ) REFERENCE P ON DELETE CASCADE ON UPDATE CASCADE, CHECK ( QTY > 0 AND QTY < 5001 ) ) ;

DDL - table modification ALTER table ALTER TABLE base-name-table ADD column-name data-type ; ( “not null” is not permitted ) DROP table DROP TABLE base-table-name

DDL - Indexes CREATE index CREATE [ UNIQUE ] INDEX index-name ON base-table-name ( column-name [ ORDER ] [, column-name [ ORDER ]... ) DROP index DROP INDEX index-name ;

Data Manipulation Language

DML - Data Manipulation Language SELECT [ DISTINCT ] field(s) FROMtable(s) [ WHERE predicate ] [ GROUP BY field(s) [ HAVING predicate ] ] [ ORDER BY field(s) ] ; SQL

Simple Retrieval - SQL Get part numbers for all parts supplied SELECT P# FROM SP ; SELECT DISTINCT P# FROM SP ; P# P1 P2 P3 P4 P5 P6 P1 P2 P4 P5 P# P1 P2 P3 P4 P5 P6

Simple retrieval - QBE Get part numbers for all parts supplied SP S#P# QTY P._PX SP S#P# QTY P.ALL._PX

Retrieval of expressions For all parts get the part number and the weight in grams ( in the table weights are in pounds ). SELECT P.p#, P.weight*454 FROM P ; PP#PnameColorweightCity P._PXP.Weight *454 P# P15448 P27718 P37718 P46356 P55448 P68626 OUTPUT

Simple retrieval of table Get full details of all suppliers SELECT * FROM S ; SS#SnameStatusCity P._SX P._SN P._ST P._SC SS#SnameStatusCity P. SQL QBE

Qualified retrieval 1 Get supplier numbers for suppliers located in Paris or with status greater than 20. SELECT S# FROM S WHERE City = ‘PARIS’ OR Status > 20 ; SS#SnameStatusCity P._SX > 20 P._SY Paris

Qualified retrieval - 2 Get supplier numbers for suppliers located in Paris with status greater than 20. SELECT S# FROM S WHERE City = ‘PARIS’ AND Status > 20 ; SS#SnameStatusCity P._SX > 20 Paris

Qualified retrieval with ordering Get supplier numbers and status for suppliers in Paris in descending order of status SELECT S#, Status FROM S WHERE City = ‘Paris’ ORDER BY Status DESC ; SS#SnameStatus City P._SX P.DO._ST Paris

Simple Equi-join Get all combinations of supplier and part information such that the supplier and part in question are located in the same city SELECT S.*, P.* FROM S, P WHERE S.City = P.City ; SS#SnameStatus City P. _X P. PP#PnameColorweightCity _X SQL QBE

Greater-than join Get all combinations of supplier and part information such that the supplier city follows the part city in alphabetical order. SELECT S.*, P.* FROM S, P WHERE S.City > P.City SS#SnameStatus City P. > _X P. PP#PnameColorweightCity _X SQL QBE

Join with additional condition Get all combinations of supplier and part information such that the supplier and part are colocated, but omitting suppliers with status > 20. SELECT S.*, P.* FROM S, P WHERE S.City = P.City AND S.Status NOT > 20 ; SS#SnameStatus City P. NOT > 20 _X P. PP#PnameColorweightCity _X SQL QBE

Retrieving specified fields from a join Get all part-number / supplier-number combinations such that supplier and part are colocated SELECT S.S#, P.P# FROM S, P WHERE S.City = P.City SS#SnameStatus City P._SX _X P._PX PP#PnameColorweightCity _X SQL QBE

Join of three tables Get all pairs of city names such that a supplier located in the first city supplies a part stored in the second city SELECT DISTINCT S.City, P.City FROM S, P, SP WHERE S.S# = SP.S# AND SP.P# = P.P# ; SS#SnameStatus City _X P._CS _Y PP#PnameColorweightCity P._PC SQL QBE SP S# P# QTY _X _Y

Function in a select clause Get the total number of suppliers SQL QBE SELECT count ( * ) FROM S ; SS# Sname StatusCity P.COUNT._SX

Function in select clause with predicate Get the total number of suppliers supplying part 2. SQL QBE SELECT count ( * ) FROM SP WHERE P# = ‘P2’ ; SP S# P# QTY P.CNT.ALL._SX P2

Join of table with itself Get all pairs of supplier numbers such that the two suppliers are colocated. SELECT FIRST.S#, SECOND.S# FROM S FIRST, S SECOND WHERE FIRST.City = SECOND.City AND FIRST.S# < SECOND.S# ; SS# Sname StatusCity _SX _SY _CZ Conditions _SX < _SY RESULT FIRST SECOND P. _SX _SY

Use of GROUP BY For each part supplied, get the part number and the total shipment quantity for that part SQL QBE SELECT P#, SUM(QTY) FROM SP GROUP BY P# ; SP S# P# QTY P.G._PX P.SUM.ALL._QX

Use of HAVING Get part numbers for all parts supplied by more than one supplier. SQL QBE SELECT P# FROM SP GROUP BY P# HAVING COUNT(*) > 1 ; SP S# P# QTY _SX P._PX NOT._SX _PX or SP S# P# QTY CNT.ALL._SX> 1 P.G._PX

Retrieval involving a subquery Get supplier names for suppliers who supply part P2. SELECT UNIQUE SNAME FROM S, SP WHERE S.S# = SP.S# AND SP.P# = ‘P2’ ; SELECT UNIQUE Sname FROM S WHERE S# IN (SELECT S# FROM SP WHERE P# = ‘P2’) ; SELECT Sname FROM S WHERE ‘P2’ IN (SELECT P# FROM SP WHERE S#= S.S#) ; SP S# P# QTY SS# Sname StatusCity _X P._SN _X P2

Single-record Update Change color of part P2 to yellow SQL: Update distinct P Set color = “yellow” where P# = P2 ; QBE: P p# pname color weight city p2 U.yellow

Multiple Update Double the status of all suppliers in London SQL Update S Set status = status * 2 where City = “London” ; QBE S S# sname status city _SX_ST London U. _SX 2 * _ST

Update involving sub-query Set quantity to zero for all suppliers in London SQL Update SP Set qty = 0 where “London” = ( select city from S where s# = SP.s# ) ; QBE SP s# p# qty U. _SX 0 S s# sname status city _SX London

Views CREATE VIEW name AS SELECT statement ; Example: CREATE VIEW good-suppliers AS SELECT s#, status, city FROM S WHERE status = 15 ; The VIEW-definition is stored in the directory but the select is not performed

VIEWS - 2 Example: CREATE VIEW PQ ( P#, sumqty ) AS SELECT p#, SUM(qty) FROM SP GROUP BY p# ;  Views can be defined in terms of other views  Some views are updateble  Views can be dropped

VIEWS - usage VIEWS can be used just like base tables CREATE VIEW LONDON-SUPPLIERS AS SELECT s, sname, status FROM S WHERE city = ‘London’ ; Two formulations with the same result SELECT *SELECT s#, sname, status FROM LONDON-SUPPLIERSFROM SWHERE status < 50 ORDER by s#;AND city = ‘London’ ORDER BY s# ;

SQL System Catalog The system catalog is also a relational database SYSTABLES ( name, creator, colcount,... ) SYSCOLUMNS ( name, tbname, coltype,... ) SYSINDEX ( name, tbname, creator,... ) SELECT tbname FROM SYSCOLUMNS WHERE name = ‘s#’ ; SELECT name FROM SYSCOLUMNS WHERE tbname = ‘S’ ; Examples: Updating the catalog is not possible

QBE dictionary Retrieval of table names : Get all tables known to the system P. Creation of new table I. S S# Sname Status City domain S# Sname Status City type char 5 char 20 fixed char 15 key Y U.N U.N U.N invers Y U.N U.N U.N