Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5B Introduction to SQL.

Similar presentations


Presentation on theme: "COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5B Introduction to SQL."— Presentation transcript:

1 COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5B Introduction to SQL

2 22 L5 SQL Part I Conceptual Data Modeling Entity Relationship Model Enhanced E-R Model (UML) Relational Data Model Basic Concepts Transformation Rules E-R Model to RM Relational Algebra Data Normalization Functional Dependencies Normal Forms Normalization Review of The First Weeks

3 33 L5 SQL Part I Overview of SQL Basic SQL Query Aggregate Functions Set Operations Today’s Agenda

4 44 L5 SQL Part I SQL - The Structured Query Language SQL is the standard declarative query language for RDBMS Describing what data we are interested in, but not how to retrieve it. Based on SEQUEL Introduced in the mid-1970’s as the query language for IBM’s System (Structured English Query Language) ANSI standard since 1986, ISO-standard since 1987 1989: revised to SQL-89 1992: more features added – SQL-92 1999: major rework – SQL:1999 (SQL 3) SQL:2003 – ‘bugfix release’ of SQL:1999 plus SQL/XML

5 55 L5 SQL Part I Benefits of a Standardized DB Language Reduced training costs Productivity Application portability Limited by vendor-specific variations! Application longevity Reduced dependency on a single vendor Cross-system communication

6 66 L5 SQL Part I SQL Overview DDL (Data Definition Language) Create, drop, or alter the relation schema DML (Data Manipulation Language) The retrieval of information stored in the database A Query is a statement requesting the retrieval of information The portion of a DML that involves information retrieval is called a query language The insertion of new information into the database The deletion of information from the database The modification of information stored in the database DCL (Data Control Language) Commands that control a database, including administering privileges and users

7 77 L5 SQL Part I Overview of SQL Basic SQL Query Aggregate Functions Set Operations Today’s Agenda

8 88 L5 SQL Part I SELECT Statement Used for queries on single or multiple tables Clauses of the SELECT statement: SELECTLists the columns (and expressions) that should be returned from the query FROMIndicate the table(s) from which data will be obtained WHEREIndicate the conditions to include a tuple in the result GROUP BYIndicate the categorization of tuples HAVINGIndicate the conditions to include a category ORDER BYSorts the result according to specified criteria The result of an SQL query is a relation A SFW-query is equivalent to the relational algebra expression  A1, A2,..., An (  condition (R 1 x R 2 x... x R m ) )

9 99 L5 SQL Part I Running Example

10 1010 L5 SQL Part I Example: Basic One-table SQL Query List the names of all customers from Sydney. select customer_name from customer where customer_city=‘Sydney’ Corresponding relational algebra expression customer_name ( customer_city=‘Sydney’ (customer) ) Note: SQL does not permit the ‘-’ character in names, Use, e.g., branch_name instead of branch-name in a real implementation. Note: SQL names are case insensitive, i.e. you can use capital or small letters. You may wish to use upper case where-ever we use bold font.

11 1111 L5 SQL Part I The WHERE Clause The where clause specifies conditions that database tuples must satisfy, in order to contribute to the result The where clause is a boolean that gets evaluated on each tuple corresponds to the selection predicate of the relational algebra. Comparison operators in SQL: =, >, >=, Comparison results can be combined using the logical connectives and, or, and not. To find all loan number 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

12 1212 L5 SQL Part I The WHERE Clause (ctd) Comparisons etc can be applied to columns from the database relation(s) named in the FROM clause Each column can be named simply (eg amount) or qualified (ie as tablename.columnname) Comparisons can be applied to results of arithmetic expr. To find all loan number for loans made at the Perryridge branch with loan amounts greater than $1200. SELECT loan.loan_number FROM loan WHERE loan.branch_name = ‘Perryridge’ AND loan.amount > 1200 SELECT loan_number FROM loan WHERE branch_name = ‘Perryridge’ AND (amount - 1200) > 0

13 1313 L5 SQL Part I The WHERE Clause (cont’d) SQL includes a between comparison operator (called “range queries”) e.g. 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 90000 AND 100000

14 1414 L5 SQL Part I String Operations SQL includes a string-matching operator for comparisons on character strings. LIKE is used for string matching Patterns are described using two special characters (“wildcards”): percent (%). The % character matches any substring. underscore (_). The _ character matches any character. Find the names of all customers whose street includes the substring “Main”. select customer_name from customer where customer_street like ‘%Main%’ SQL supports a variety of string operations such as concatenation (using “||”) converting from upper to lower case (and vice versa) finding string length, extracting substrings, etc.

15 1515 L5 SQL Part I The SELECT clause The SELECT clause indicates what appears in the output relation A common possibility is to name columns from the database relation Simply (columnname) or qualified (tablename.columnname) These columns (from those rows that satisfy the WHERE clause) are placed into the output relation Corresponds to projection operator in relational algebra An asterisk in the select clause denotes all attributes SELECT * FROM loan This is equivalent to SELECT loan_number, branch_name, amount FROM loan The benefit of using * is that query still works if schema changes Eg extra columns are added

16 1616 L5 SQL Part I The SELECT Clause (ctd) The select clause can contain arithmetic expressions involving the operations +, -, * and /, and operating on constants or attributs of tuples. The query: SELECT loan_number, branch_number, amount*100 FROM loan would return a relation which is the same as the loan relation except that the amount-values are multiplied by 100. Naming expressions: we can give a name to attribute in the output relation by as new_name SELECT loan_number, branch_number, amount*100 AS percent_amount FROM loan

17 1717 L5 SQL Part I Order By Clause List all customers (name) from Sydney in alphabetical order. select customer_name from customer where customer_city=‘Sydney’ order by customer_name Ordering: Two options ASC ascending oder (default) DESC descending order You can order by more than one attribute e.g., order by country desc, name asc

18 1818 L5 SQL Part I Duplicates In contrast to the relational algebra, SQL allows duplicates in relations as well as in query results. To force the elimination of duplicates, insert the keyword distinct after select. Find the names of all branches in the loan relations, and remove duplicates select distinct branch_name from loan The keyword all specifies that duplicates not be removed. select all branch_name from loan

19 1919 L5 SQL Part I Multi-table queries The from clause lists the relations involved in the query corresponds to the Cartesian product operation of the relational algebra. join-predicates must be explicitly stated in the where clause Remember to qualify any attribute name that occurs in several tables Examples: Find the Cartesian product borrower x loan SELECT * FROM borrower, loan Find the name, loan number and loan amount of all customers having a loan at the Perryridge branch. SELECT customer_name,borrower,loan_number,amount FROM borrower, loan WHERE borrower.loan-number = loan.loan_number AND branch-name = ‘Perryridge’

20 2020 L5 SQL Part I Join Example What grades where achieved by the students? SELECT name, number, grade FROM Students, Enrolled WHERE Students.sid = Enrolled.sid; Join involves multiple tables in FROM clause WHERE clause performs the equality check for common columns of the two tables

21 2121 L5 SQL Part I Aliases Some queries need to refer to the same relation twice In this case, aliases are given to the relation name Example: For each employee, retrieve the employee's name, and the name of his or her immediate supervisor. SELECTE.FNAME, E.LNAME, S.FNAME,S.LNAME FROMEMPLOYEE E, EMPLOYEE S WHEREE.SUPERSSN = S.SSN We can think of E and S as two different copies of EMPLOYEE ; E represents employees in role of supervisees and S represents employees in role of supervisors Alias can also be used just to shorten the query text SELECT name, number, grade FROM Students S, Enrolled E WHERE S.sid = E.sid;

22 2222 L5 SQL Part I Exercise Example 1 varchar int empidnameroom lecturers sidnumberempidmark assessment sidnumber enrolled grade numbertitle credit_ points courses lecturer intvarcharint varchar sidnamebirthdate students country intvarchardatevarchar int char

23 2323 L5 SQL Part I Example Queries List all courses by title select title from courses List all courses. select * from courses Who is teaching “COMP5138”? select name from courses, lecturers where number=‘COMP5138’ and lecturer=empid

24 2424 L5 SQL Part I Find the students with marks between 0 and 10. select sid from assessment where mark between 0 and 10 List students who got a distinction or better in COMP5138. Tip: use in (not between) comparison operator for sets select sid from enrolled where number =‘COMP5138’ and grade in (‘D’, ‘H’) List in alphabetic order the names of all students select * from students order by name Example Queries

25 2525 L5 SQL Part I Overview of SQL Basic SQL Query Aggregate Functions Set Operations Today’s Agenda

26 2626 L5 SQL Part I Aggregate Functions These functions operate on the multiset of values of a column of a relation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Note: with aggregate functions there is a single row in the result relation so you can’t also have single-valued columns in the select clause (except with grouping, see next lecture)

27 2727 L5 SQL Part I Aggregate Functions Step A: Use FROM clause and WHERE clause to determine a set of rows (possibly from a cartesian product) which meet the where condition Call the relation Q Step B: The output is a single row, whose entries are each obtained by applying some aggregate function to one or more columns of the relation Q

28 2828 L5 SQL Part I Examples for Aggregate Functions How many students are enrolled? Two possible interpretations (give different answers!) select count(*) from Enrolled select count(distinct sid) from Enrolled What was the average mark for COMP5138? select avg(mark) AS avg_mark from assessment where number=‘COMP5138’ Which was the best and worst mark for COMP5138? select max(mark) AS max_mark, min(mark) AS min_mark from assessment where number = ‘COMP5138’

29 2929 L5 SQL Part I Overview DDL Statements Basic SQL Query Aggregate Functions Set Operations Today’s Agenda

30 3030 L5 SQL Part I Set Operations The set operations union, intersect, and except (Oracle: minus) operate on relations and correspond to the relational algebra operations  Each of the above operations automatically eliminates duplicates; 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 (not supported by Oracle)

31 3131 L5 SQL Part I 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)

32 3232 L5 SQL Part I Examples for Set Operations List all names in the database. select name from students union select name from lecturers Which students did not enroll in any course? select sid from students minus select sid from enrolled Find students who enrolled both for ‘COMP5138’ and ‘COMP5318’. select sid from enrolled where number=‘COMP5138’ intersect select sid from enrolled where number=‘COMP5318’

33 3333 L5 SQL Part I R1 S1 B1 Exercise Example 2

34 3434 L5 SQL Part I Find sid ’ s of sailors who ’ ve reserved a red or a green boat SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’) SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ UNION SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’ Example Queries

35 3535 L5 SQL Part I Find sid ’ s of sailors who ’ ve reserved a red and a green boat SELECT S.sid FROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color=‘red’ AND B2.color=‘green’) 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 SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’ Example Queries

36 3636 L5 SQL Part I Wrap-Up SQL Basic Query SELECT … FROM … WHERE … ORDER BY … Aggregate Functions Set Operations Union, Intersect, and Except

37 3737 L5 SQL Part I Remember: SELECT Statement Clauses of the SELECT statement: SELECTLists the columns (and expressions) that should be returned from the query FROMIndicate the table(s) from which data will be obtained WHEREIndicate the conditions to include a tuple in the result GROUP BYIndicate the categorization of tuples HAVINGIndicate the conditions to include a category ORDER BYSorts the result according to specified criteria The result of an SQL query is a relation A SFW-query is equivalent to the relational algebra expression  A1, A2,..., An (  condition (R 1 x R 2 x... x R m ) )


Download ppt "COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5B Introduction to SQL."

Similar presentations


Ads by Google