CS 370 Database Systems Lecture 12 Introduction to SQL.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
CS 166: Database Management Systems
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
E-R Diagram for a Banking Enterprise
Domain Relational Calculus and Query-by-Example CS157a John Eagle.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
SQL.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Multimedia Information Systems CS Outlines Introduction to DMBS Relational database and SQL B + - tree index structure.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Introduction to SQL Basics --- SQL in 45 Minutes Chapter 5.
CS 370 Database Systems Lecture 13 Introduction to SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
3.1 Chapter 3: SQL. 3.2 Chapter 3: SQL Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
Chapter 3 Introduction to SQL Yonsei University 1 st Semester, 2015 Sanghyun Park.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5B Introduction to SQL.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language” (create.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
CS 370 Database Systems Lecture 11 Relational Algebra.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts, 5 th Edition, Oct 5, 2006 SQL Data Definition Basic Query Structure Set Operations Aggregate.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
SQL CS 186, Spring 2007, Lecture 7 R&G, Chapter 5 Mary Roth   The important thing is not to stop questioning. Albert Einstein Life is just a bowl of.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
Basic SQL Lecture 6 Fall
Chapter 3: SQL.
CS 405G: Introduction to Database Systems
| Website for Students | VTU NOTES | Question Papers
SQL: The Query Language Part 1
CS 405G: Introduction to Database Systems
Presentation transcript:

CS 370 Database Systems Lecture 12 Introduction to SQL

Structured Query Language The most widely used relational query language The standard for relational database management systems (RDBMS) SQL Overview

Two sublanguages: –Data Manipulation Language (DML) – This subset of SQL allows users to pose queries and to insert, delete, and update rows. –Data Definition Language (DDL) – This subset of SQL supports the creation, deletion, and modification of definitions for tables and views. SQL Overview

1970 – E. Codd develops relational database concept – IBM Sequel language developed as part of System R project at the IBM San Jose Research Laboratory Renamed Structured Query Language (SQL) 1986 – ANSI SQL standard released Current – SQL is supported by most major database vendors ANSI and ISO standard SQL: –SQL-86 –SQL-89 –SQL-92 –SQL:1999 –SQL:2003 History

Specify syntax/semantics for data definition and manipulation Define data structures Enable portability Allow for later growth/enhancement to standard Purpose of SQL Standard

The basic form of an SQL query is:- SELECT[DISTINCT] select-list FROMfrom-list WHEREqualification Clauses of the SELECT statement: –SELECT List the columns (and expressions) that should be returned from the query –FROM Indicate the table(s) or view(s) from which data will be obtained –WHERE Indicate the conditions under which a row will be included in the result Basic SQL Query

CS370 Spring 2007 SELECT DISTINCT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors sidbidday 11029/ / /13 Reserves sid 1 2 Find sailors that have reserved at least one boat SELECT DISTINCT

CS370 Spring 2007 How about: SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid sid SELECT DISTINCT

CS370 Spring 2007 How about: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 4Bilbo532 Sailors sidbidday 11029/ / /13 Reserves sname Frodo Bilbo SELECT DISTINCT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid sname Frodo Bilbo vs: SELECT DISTINCT

CS370 Spring 2007 Semantics of an SQL query are defined in terms of the following conceptual evaluation strategy: 1.FROM clause: compute cross-product of all tables 2.WHERE clause: Check conditions, discard tuples that fail. (called “selection”). 3. SELECT clause: Delete unwanted fields. (called “projection”). 4. If DISTINCT specified, eliminate duplicate rows. Query Semantics

SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select A 1, A 2, …, A n from R 1, R 2, …, R m where P - A i represent attributes - R i represent relations - P is a predicate. Basic Structure

Projection The select clause corresponds to the projection operation of the relational algebra. It is used to list the attributes desired in the result of a query. Find the names of all branches in the loan relation select branch-name from loan Equivalent to:  branch-name (loan) An asterisk in the select clause denotes “all attributes” select * from loan

Duplicate Removal SQL allows duplicates in relations as well as in query results. Use select distinct to force the elimination of duplicates. Find the names of all branches in the loan relation, and remove duplicates select distinct branch-name from loan The keyword all specifies that duplicates not be removed. select all branch-name from loan force the DBMS to remove duplicates force the DBMS not to remove duplicates

Arithmetic Operations on Retrieved Results The select clause can contain arithmetic expressions involving the operators, , ,  and , and operating on constants or attributes of tuples. The query: select branch-name, loan-number, amount * 100 from loan would return a relation which is the same as the loan relations, except that the attribute amount is multiplied by 100

The where clause specifies conditions that tuples in the relations in the from clause must satisfy. Find all loan numbers for loans made at the Perryridge branch with loan amounts greater than $1200. select loan-number from loan where branch-name=“Perryridge” and amount >1200 SQL allows logical connectives and, or, and not. Arithmetic expressions can be used in the comparison operators. The where Clause

The where clause specifies conditions that tuples in the relations in the from clause must satisfy. Find all loan numbers for loans made at the Perryridge branch with loan amounts greater than $1200. select loan-number from loan where branch-name=“Perryridge” and amount >1200 SQL allows logical connectives and, or, and not. Arithmetic expressions can be used in the comparison operators. The where Clause

The where Clause (Cont.) SQL includes the between operator for convenience. Find the loan number of those loans with loan amounts between $90,000 and $100,000 (that is,  $90,000 and  $100,000) select loan-number from loan where amount between and

The from Clause The from clause corresponds to the Cartesian product operation of the relational algebra. Find the Cartesian product borrower  loan select * from borrower, loan It is rarely used without a where clause. Find the name and loan number of all customers having a loan at the Perryridge branch. select distinct customer-name, borrower.loan-number from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = “Perryridge”

The Rename Operation Renaming relations and attributes using the as clause: old-name as new-name Find the name and loan number of all customers having a loan at the Perryridge branch; replace the column name loan-number with the name loan-id. select distinct customer-name, borrower.loan-number as loan-id from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = “Perryridge”

Set Operations The set operation union, intersect, and except operate on relations and correspond to the relational algebra operations ,  and . Each of the above operations automatically eliminates duplicate; to retain all duplicates use the corresponding multiset versions union all, intersect all and except all. Suppose a tuple occurs m times in r and n times in s, then, it occurs: –m + n times in r union all s –min(m,n) times in r intersect all s –max(0,m-n) times in r except all s

Set operations Find all customers who have a loan, an account, or both: (select customer-name from depositor) union (select customer-name from borrower) Find all customers who have both a loan and an account. (select customer-name from depositor) intersect (select customer-name from borrower) Find all customers who have an account but no loan. (select customer-name from depositor) except (select customer-name from borrower)

CS370 Spring 2007 Find sids of sailors who’ve reserved a red or a green boat sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors Reserves bidbnamecolor 101Ninared 102Pintablue 103Santared 105Titanicgreen Boats sidbidday 11029/ / /13 X sid 2 4 SELECT R.sid FROM Boats B,Reserves R WHERE(B.color=‘red’ OR B.color=‘green’) AND R.bid=B.bid AND, OR, UNION & INTERSECT

CS370 Spring 2007 SELECT R.sid FROM Boats B,Reserves R WHERE(B.color=‘red’ AND B.color=‘green’) AND R.bid=B.bid Find sids of sailors who’ve reserved a red and a green boat sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors Reserves sidbidday 11019/ / /13 X bidbnamecolor 101Ninared 102Pintablue 103Santared 105Titanicgree n Boats AND & OR

CS370 Spring 2007 SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘red’ AND R.bid=B.bid INTERSECT SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘green’ AND R.bid=B.bid Reserves sidbidday 11019/ / /13 bidbnamecolor 101Ninared 102Pintablue 103Santared 105Titanicgree n Boats sid  = 1 Use INTERSECT instead of AND

CS370 Spring 2007 Reserves sidbidday 11029/ / /13 bidbnamecolor 101Ninared 102Pintablue 103Santared 105Titanicgree n Boats sid 2 4  = 2 4 SELECT R.sid FROM Boats B, Reserves R WHERE B.color = ‘red’ AND R.bid=B.bid UNION SELECT R.sid FROM Boats B, Reserves R WHERE B.color = ‘green’ AND R.bid=B.bid Could also use UNION for the OR query

SELECT S.sid FROM Sailors S EXCEPT SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid Find sids of sailors who have not reserved a boat sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Reserves sidbidday 11029/ / /13 Sailors First find the set of sailors who have reserved a boat… and then compare it with the rest of the sailors sid 3 ESCEPT: Set Difference