SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.

Slides:



Advertisements
Similar presentations
CS411 Database Systems Kazuhiro Minami 06: SQL. SQL = Structured Query Language Standard language for querying and manipulating data Has similar capabilities.
Advertisements

1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation.
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
1 Introduction to SQL Select-From-Where Statements Multirelation Queries Subqueries.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
SQL CSET 3300.
Fall 2001Arthur Keller – CS 18016–1 Schedule Nov. 20 (T) More OQL. u Read Sections Assignment 7 due (late ones by ). Nov. 22 (TH) Thanksgiving.
From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.
IS698: Database Management Min Song IS NJIT. Overview  Query processing  Query Optmization  SQL.
Fall 2001Arthur Keller – CS 1806–1 Schedule Today (TH) Bags and SQL Queries. u Read Sections Project Part 2 due. Oct. 16 (T) Duplicates, Aggregation,
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
Chapter 6 Notes. 6.1 Simple Queries in SQL SQL is not usually used as a stand-alone language In practice there are hosting programs in a high-level language.
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
Databases 1 First lecture. Informations Lecture: Monday 12:15-13:45 (3.716) Practice: Thursday 10:15-11:45 (2-519) Website of the course:
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Onsdag The concepts in a relation data model SQL DDL DML.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
[ Part III of The XML seminar ] Presenter: Xiaogeng Zhao A Introduction of XQL.
Databases 1 Second lecture.
CSCE 520- Relational Data Model Lecture 2. Oracle login Login from the linux lab or ssh to one of the linux servers using your cse username and password.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
Introduction to SQL Introduction Select-From-Where Statements Queries over Several Relations Subqueries.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
1 Database Design: DBS CB, 2 nd Edition SQL: Select-From-Where Statements & Multi-relation Queries & Subqueries Ch. 6.
Select-From-Where Statements Multirelation Queries Subqueries
CPSC-310 Database Systems
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
Computational Biology
COP4710 Database Systems Relational Algebra.
Outerjoins, Grouping/Aggregation Insert/Delete/Update
CMSC-461 Database Management Systems
Databases : More about SQL
CPSC-310 Database Systems
Data Warehouse.
Example: R3 := R1 × R2 R3( A, R1.B, R2.B, C )‏ R1( A, B )‏ R2( B, C )‏ R3( A, R1.B, R2.B, C )‏
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
CPSC-608 Database Systems
06a: SQL-1 The Basics– Select-From-Where
CS 440 Database Management Systems
CPSC-608 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
IST 210: Organization of Data
CPSC-310 Database Systems
Basic Operations Algebra of Bags
The Relational Data Model
CPSC-608 Database Systems
CPSC-608 Database Systems
CS 405G Introduction to Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
CS 405G: Introduction to Database Systems
Presentation transcript:

SQL Group Members: Shijun Shen Xia Tang Sixin Qiang

Relational Model Very simple model. It is the Abstract model that underlies SQL. By using the relational approach, the logic representation(the program) is separated from the physical implementation of the database. This makes the development of programs more effective and less dependent on changes in the physical representation of data.

Introduction to SQL SQL stands for Structured Query Language. It was developed in the 1970s at IBM (Big Blue) as a way to provide users with a standard method of selecting data from many different database formats. SQL is used to gain access to the relations and the desired set of data and the programmer does not have to write algorithms for navigation because the physical implementation of the database is hidden by using the relational approach.

Some Concepts Table = relation.Table = relation. Column headers = attributes.Column headers = attributes. Row = tupleRow = tuple Relation schema = name(attributes).Relation schema = name(attributes). >>Example: Beers(name, manf). >>Example: Beers(name, manf). Relation instance is current set of rows for a relation schema.Relation instance is current set of rows for a relation schema. Database schema = collection of relation schemas.Database schema = collection of relation schemas.

SQL Queries Principle Form:Principle Form: SELECT desired attributes FROM tuple variables WHERE condition

Demonstrate SQL with an Example Relation Schema: Beers(name, manf) Bars(name,addr,license)Drinkers(name,addr,phone)Likes(drinker,beer)Sells(bar,beer,price)Frequents(drinker,bar)

Simplest Case  What beers are made by Anheuser-Busch?  Beers(name, manf)  SELECT name FROM Beers WHERE manf = 'Anheuser-Busch';  Note single quotes for strings.  name Bud Bud Lite Michelob

Operational Semantics of SQL For each tupleFor each tuple Check if it satisfies the WHERE clause; If So, Print the attributes in SELECT.

Star as list of All Attributes   Beers(name, manf)   SELECT * FROM Beers WHERE manf = 'Anheuser-Busch';   namemanf BudAnheuser-Busch Bud LiteAnheuser-Busch MichelobAnheuser-Busch

Renaming Columns   Beers(name, manf)   SELECT name AS beer FROM Beers WHERE manf = 'Anheuser-Busch';   beer Bud Bud Lite Michelob

Trick   If you want an answer with a particular string in each row, use that constant as an expression.   Example: Using relation Likes(drinker, beer) SELECT drinker, ‘likes Bud’ AS whoLikesBud FROM Likes Where beer = ‘Bud’;   whoLikesBud Sally likes Bud Fred likes Bud

More SQL Queries Find the price Joe’s Bar charge for BudFind the price Joe’s Bar charge for Bud Using relation Schema Sells(bars,beer,price)Using relation Schema Sells(bars,beer,price) SELECT price FROM Sells WHERE bar=‘Joe”s Bar’ AND beer =‘Bud’ Note: two single-quotes in a character string represents one single quote.Note: two single-quotes in a character string represents one single quote. Conditions in WHERE clause can use logical AND,OR, NOT and parentheses in the usual way.Conditions in WHERE clause can use logical AND,OR, NOT and parentheses in the usual way. SQL is case insensitive. Keywords like SELECT or AND can be written upper/lower case as you like.SQL is case insensitive. Keywords like SELECT or AND can be written upper/lower case as you like.

Patterns Patterns:Patterns: –% stands for any string. –_ stands for any one character. –“Attributes LIKE pattern” is a condition that is true if the string value of the attribute matches the pattern. Example: Find drinkers whose phone has extension 555 using relation Drinkers(name,addr,phone).Example: Find drinkers whose phone has extension 555 using relation Drinkers(name,addr,phone). SELECT name FROM Drinkers WHERE phone LIKE ‘%555-’; Note patterns must be quoted, like strings.Note patterns must be quoted, like strings.

Multirelation Queries List of relations in FROM clause.List of relations in FROM clause. Relation-dot-attribute disambiguates attributes from several relations.Relation-dot-attribute disambiguates attributes from several relations. Example: Find the beers that the frequenters of Joe’s Bar like, using relationExample: Find the beers that the frequenters of Joe’s Bar like, using relation Like(drinker,beer) and Frequentes(drinker,bar) Like(drinker,beer) and Frequentes(drinker,bar) SELECT beer FROM Frequents, Likes WHERE Frequents.drinker=Likes.drinker AND bar = ‘Joe”s Bar’;

Operational Semantics Consider a tuple variable for each relation in the FROMConsider a tuple variable for each relation in the FROM –Imagine these tuple variables each pointing to a tuple of their relation, in all combinations(e.g.,nested loops). –If the current assignment of tuple-variables to tuples makes the WHERE true, then output the attributes of the SELECT.

Explicit Tuple Variables Sometimes we need to refer to two more copies of a relationSometimes we need to refer to two more copies of a relation –Use tuple variables as aliases of the relations. Example: Find pairs of beers produced by the same manufacturer, use relation Beers(name,manf)Example: Find pairs of beers produced by the same manufacturer, use relation Beers(name,manf) SELECT b1.name, b2.name FROM Beers b1, Beer b2 WHERE b1.manf = b2.manf AND b1.name < b2.name Note that b1.name < b2.name is needed to avoid producing (Bud,Bud) and to avoid producing a pair in both orders.Note that b1.name < b2.name is needed to avoid producing (Bud,Bud) and to avoid producing a pair in both orders.

Subqueries Result of a select-from-where query can be used in the where-clause of another query.Result of a select-from-where query can be used in the where-clause of another query. Subquery Returns a Single, Unary Tuple.Subquery Returns a Single, Unary Tuple.

Subqueries Example: Find bars that serve Miller at the same price Joe charges for Bud, use relation Sells(bar, beer, price)Example: Find bars that serve Miller at the same price Joe charges for Bud, use relation Sells(bar, beer, price) SELECT bar FROM Sells WHERE beer = ‘Miller’ AND price = (SELECT price FROM Sells WHERE bar=‘Joe”s Bar’ AND beer=‘Bud’) Note the scoping rule: an attribute refers to the most closely nested relation with that attribute.Note the scoping rule: an attribute refers to the most closely nested relation with that attribute. Parentheses around subquery are essential.Parentheses around subquery are essential.

IN operator “Tuple IN relation” is true iff the tuple is in the relation.“Tuple IN relation” is true iff the tuple is in the relation. Example: Find the name and manufacturer of beers that Fred likes, use relations Beers(name,manf) and Likes(drinker,beer)Example: Find the name and manufacturer of beers that Fred likes, use relations Beers(name,manf) and Likes(drinker,beer) SELECT * FROM Beers WHERE name IN (SELECT beer FROM Likes WHERE drinker=‘Fred’); WHERE drinker=‘Fred’); Also: NOT IN, negation of IN.Also: NOT IN, negation of IN.

Quantifiers ANY and ALL behave as existential and universal Quantifiers respectively.ANY and ALL behave as existential and universal Quantifiers respectively. –Beware in common sense, “any” and “all” seem to be synonyms, e.g.,”I am fatter than any of you” vs. “ I am fatter than all of you.” However, this is not the case in SQL. Example: Find the beer(s) sold for the highest price. Use relation Sells(bar,beer,price)Example: Find the beer(s) sold for the highest price. Use relation Sells(bar,beer,price) SELECT beer FROM Sells WHERE price >=ALL (SELECT price FROM Sells);

XQL,OQL and SQL Relational Model underlies SQLRelational Model underlies SQL Object - relational Model and Object - Oriented Model underlies OQLObject - relational Model and Object - Oriented Model underlies OQL XML documents underlies XQLXML documents underlies XQL

XQL, OQL and SQL The result of a SQL query is a table containing a set of rows; this table may serve as the basis for further queries. The result of a XQL query is a list of XML document nodes, which may serve as the basis for further queries. The result of a OQL query have types (set, bag, list, array), which may serve as the basis for further queries.

THE END THE END