Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 7: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.

Slides:



Advertisements
Similar presentations
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 6 The Relational Algebra
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
This course has taken from This unique introductory SQL tutorial not only provides easy-to-understand SQL instructions, but it allows.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems More SQL Database Design -- More SQL1.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts - 6 th Edition SQL Schema Changes and table updates instructor teaches.
Microsoft Access 2010 Chapter 7 Using SQL.
IFS Intro. to Data Management Chapter 6 Filtering your data.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Single-Table Queries
Introduction to Databases Chapter 7: Data Access and Manipulation.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.
Chapter 9 Joining Data from Multiple Tables
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
SQL (DDL & DML Commands)
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Lecture3b - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data Guide to Oracle 10g ITBIS373 Database Development.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
More SQL: Complex Queries,
Chapter 3 Introduction to SQL(2)
Writing Basic SQL SELECT Statements
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Chapter # 6 The Relational Algebra and Calculus
Using Subqueries to Solve Queries
Restricting and Sorting Data
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language
CS4222 Principles of Database System
Using Subqueries to Solve Queries
CSC 453 Database Systems Lecture
Shelly Cashman: Microsoft Access 2016
Restricting and Sorting Data
Presentation transcript:

Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 7: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design Instructor: Qing Yan, M.D., Ph.D.

Unit 7 Objectives Use comparison and logical operators in the WHERE clause of an SQL query Analyze how to handle null values in SQL queries Chapter 1 – Database Systems: Architecture and Components 2

Unit 7 To-Do List Complete the reading Textbook and Web Participate in the discussion board 30 points Attend the introductory seminar or complete FLA quiz 20 points Complete the unit assignment 50 points Chapter 1 – Database Systems: Architecture and Components 3

Chapter 11 – Data Manipulation: Relational Algebra and SQL4

5

Key Concepts Unary operator- A relational algebra operator that “operates” on a single relation. Select operator- The unary relational algebra operator used to select a horizontal subset of the tuples that satisfy a selection condition from a relation. Cartesian product operator- The binary relational algebra operator that combines tuples from any two relations in a combinatorial fashion. Chapter 1 – Database Systems: Architecture and Components 6 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Key Concepts DISTINCT qualifier- When specified as part of the SELECT statement, causes duplicate rows in queries to be removed. LIKE operator- The SQL-92 operator used in conjunction with pattern matching. VARCHAR() data type- A data type, where represents the length of the column, has a maximum size of 2000 characters in most DBMSs. Chapter 1 – Database Systems: Architecture and Components 7 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Key Concepts IN [NOT IN] operator- When used in conjunction with a subquery, evaluates if rows processed by the outer query are equal [not equal] to any of the values returned by the subquery. ANY operator- When used in conjunction with a subquery, specifies that the condition be true for at least one value from the set of values. ALL operator- When used in conjunction with a subquery, specifies that the condition be true for all values from the set of values. Chapter 1 – Database Systems: Architecture and Components 8 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL9 Querying Using SQL From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL10 The SELECT-FROM-WHERE Block SELECT FROM WHERE <-- WHERE clause is optional is a list of column names whose values are to be retrieved by the query is a list of the table names required to process the query is a conditional (Boolean) expression that identifies the rows to be retrieved by the query. From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL11 Optional Add-ons To SELECT Clause GROUP BY group_by expression HAVING group_condition ORDER BY column name(s) group_by_expression forms groups of rows with the same value group_condition filters the groups subject to some condition column name(s) specifies the order of the output. From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL12 COURSE Relation From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL13 SECTION Relation Se_section# Se_qtr Se_year Se_time Se_maxst Se_room Se_co_course# Se_pr_profid A 2007 T QA375 HT A 2006 W Rhodes IS270 SK A 2006 H Lindner IS270 SK S 2006 T Lindner IS330 SK S 2006 H Lindner IS330 CC W 2007 M Braunstien IS832 CC A 2007 W1800 Baldwin ECES212 RR U 2007 T QA375 HT A 2007 H Lindner IS330 SK S 2007 T QA375 HT W 2007 T QA375 HT54347 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL14 Which courses are three-hour courses? SELECT COURSE.CO_NAME, COURSE.CO_COURSE#, COURSE.CO_CREDIT, COURSE.CO_COLLEGE, COURSE.CO_HRS, COURSE.CO_DPT_DCODE FROM COURSE WHERE COURSE.CO_HRS = 3; SELECT * FROM COURSE WHERE COURSE.CO_HRS = 3; Co_name Co_course# Co_credit Co_college Co_hrs Co_dpt_dcode Intro to Economics 15ECON112 U Arts and Sciences 3 1 Supply Chain Analysis 22QA411 U Business 3 3 Principles of IS 22IS270 G Business 3 7 Programming in C++ 20ECES212 G Engineering 3 6 Optimization 22QA888 G Business 3 3 Financial Accounting 18ACCT801 G Education 3 4 Database Principles 22IS832 G Business 3 7 Systems Analysis 22IS430 G Business 3 7 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL15 Which courses offered by Department 7 are three-hour courses? SELECT * FROM COURSE WHERE COURSE.CO_DPT_DCODE = 7 AND COURSE.CO_HRS = 3; Result: Co_name Co_course# Co_credit Co_college Co_hrs Co_dpt_dcode Principles of IS 22IS270 G Business 3 7 Database Principles 22IS832 G Business 3 7 Systems Analysis 22IS430 G Business 3 7 From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL16 Which sections have a maximum number of students greater than 30 or are offered in Linder 110? SELECT * FROM SECTION WHERE SECTION.SE_MAXST > 30 OR SECTION.SE_ROOM = 'Lindner 110'; From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL17 Use of AND and/or OR When AND and OR appear in the same WHERE clause, all the ANDs are performed first Example: WHERE COURSE.CO_CREDIT = 'U' and COURSE.CO_COLLEGE = 'Business' or COURSE.CO_COLLEGE = 'Engineering‘ Every condition yields either true or false NOT operator From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL18 Use of IN and NOT IN IN or NOT IN can also be used as comparison operators. IN is evaluated in the context of being equal to any member of a set of values A nested query can follow the IN/NOT IN operator From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL19 Use of IN and NOT IN (continued) SELECT * FROM COURSE WHERE COURSE.CO_CREDIT = 'U‘ AND (COURSE.CO_COLLEGE = 'Business' OR COURSE.CO_COLLEGE = 'Engineering'); SELECT * FROM COURSE WHERE COURSE.CO_CREDIT = 'U‘ AND COURSE.CO_COLLEGE IN ('Business', 'Engineering'); From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL20 Which professors earn more than $6,000? SELECT * FROM PROFESSOR WHERE PROFESSOR.PR_SALARY/12 > 6000; Also: TRUNC, ROUND From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL21 Use of AS in Column Aliases SELECT PROFESSOR.PR_NAME, PROFESSOR.PR_SALARY/12 AS "Monthly Salary" FROM PROFESSOR WHERE PROFESSOR.PR_SALARY/12 > 6000; From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL22 Use of BETWEEN and NOT BETWEEN SELECT PROFESSOR.PR_NAME, PROFESSOR.PR_SALARY/12 AS "Monthly Salary" FROM PROFESSOR WHERE PROFESSOR.PR_SALARY/12 BETWEEN 6000 AND 7000; Note: BETWEEN is inclusive From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL23 Which colleges offer courses? SELECT COURSE.CO_COLLEGE FROM COURSE; From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL24 Use of DISTINCT (Versus ALL) SELECT DISTINCT COURSE.CO_COLLEGE FROM COURSE; From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL25 What is the age (in years) of each professor in Department 3 when hired? SELECT PROFESSOR.PR_NAME, TRUNC((PROFESSOR.PR_DATEHIRED - PROFESSOR.PR_BIRTHDATE)/365.25,0) "Age When Hired" FROM PROFESSOR WHERE PROFESSOR.PR_DPT_DCODE = 3; accounts for leap years,0 indicates the decimals AS is optional, here it is left out From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL26 NULL Values Please note that two ways of inserting null values exist: (1) use the word NULL, or (2) define a character sting that is zero characters long Example: INSERT INTO Textbook VALUES (‘ ’, ‘Null Publisher 1’, 2004, null); INSERT INTO Textbook VALUES (‘ ’, ‘Null Publisher 2’, 2004, ‘’); INSERT INTO Textbook VALUES (‘ ’, ‘Single Space Publisher’, 2004, ‘ ’); What does the following SQL statement do? SELECT TEXTBOOK.TX_PUBLISHER FROM TEXTBOOK; From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL27 TEXTBOOK Relation space From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL28 NULL Values Example SELECT TEXTBOOK.TX_PUBLISHER FROM TEXTBOOK; From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL29 IS NULL and IS NOT NULL Instead: SELECT TEXTBOOK.TX_PUBLISHER FROM TEXTBOOK WHERE TEXTBOOK.TX_PUBLISHER IS NOT NULL; Note that the only operator that can be used with null values IS NULL and IS NOT NULL! Do not use =, >, <, etc. From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL30 Pattern Matching Use the LIKE operator in conjunction with the two wildcard characters percent sign (%) and the underscore symbol (_) The percent sign represents a series of one or more unspecified characters while the underscore symbol represents exactly one character. From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL31 The LIKE Operator SELECT TEXTBOOK.TX_TITLE FROM TEXTBOOK WHERE TEXTBOOK.TX_TITLE LIKE '_i%'; Remember that character strings are case-sensitive! From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Chapter 11 – Data Manipulation: Relational Algebra and SQL32 The LIKE Operator (continued) SELECT TEXTBOOK.TX_TITLE FROM TEXTBOOK WHERE TEXTBOOK.TX_TITLE LIKE '%'; Note that varchar and char can make a difference when e.g., searching for a string that ends in a particular character (e.g., ‘%s’) From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Summary SELECT-WHERE-FROM clause of an SQL query AND, OR IN, NOT IN BETWEEN, NOT BETWEEN IS NULL, IS NOT NULL LIKE, ‘%’, ‘_’

Q & A Questions?