Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.

Slides:



Advertisements
Similar presentations
Concepts of Database Management Seventh Edition
Advertisements

Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Introduction to Structured Query Language (SQL)
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Microsoft Office 2007 Access Chapter 2 Querying a Database.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Concepts of Database Management, 4th Edition, Pratt & Adamski
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Chapter 2 Querying a Database
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth 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.
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 2 Querying a Database
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Concepts of Database Management, Fifth Edition
With Microsoft Office 2007 Intermediate© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Intermediate.
With Microsoft Access 2007 Volume 1© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access 2007 Volume 1 Chapter.
Chapter 2 Querying a Database MICROSOFT ACCESS 2010.
Chapter 3 Single-Table Queries
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
With Microsoft Office 2007 Introductory© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Introductory.
Concepts of Database Management Seventh Edition
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Microsoft Access 2010 Building and Using Queries.
Analyzing Data For Effective Decision Making Chapter 3.
Microsoft Office 2007 Access Chapter 2 Querying a Database.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Querying a Database Access Project 2. 2 What is a Query?  In general, a query is a form of questioning, in a line of inquiry. A query may also refer.
Chapter 4 Multiple-Table Queries
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
XP. Objectives Sort data and filter data Summarize an Excel table Insert subtotals into a range of data Outline buttons to show or hide details Create.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
1 2 Concepts of Database Management, 4 th Edition, Pratt & Adamski Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
© 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.
1 SQL II CIS*2450 Advanced Programming Concepts. 2 Data Types INTEGER –numbers without a decimal point –range is to SMALLINT –like.
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license.
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Microsoft ® Excel ® 2013 Enhanced Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
1 New Perspectives on Microsoft Access 2016 Module 4: Creating Forms and Reports.
3 A Guide to MySQL.
Access Chapter 2 Querying a Database.
Exchanging Data with Other Programs
Building and Using Queries
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL

Objectives Introduce Structured Query Language (SQL) Use simple and compound conditions in SQL Use computed fields in SQL Use built-in SQL functions Use subqueries in SQL 2 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Objectives (continued) Group records in SQL Join tables using SQL Perform union operations in SQL Use SQL to update database data Use an SQL query to create a table in a database 3 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Introduction SQL (Structured Query Language) –Allows users to query a relational database –Must enter commands to obtain the desired results –Standard language for relational database manipulation 4 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Getting Started with SQL If you are completing the work in this chapter using Microsoft Office Access 2010 or Microsoft Office Access 2013, the following section contains specific information about your DBMS 5 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Getting Started with Microsoft Office Access 2010 or 2013 If you are using the Access 2010 or 2013 version of the TAL Distributors database provided with the Data Files for this text: –Tables in the database have already been created –You will not need to execute the CREATE TABLE commands to create the tables or the INSERT commands to add records to the tables 6 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Getting Started with Microsoft Office Access 2010 or 2013 (continued) To execute SQL commands shown in the figures in Access 2010 or Access 2013: –Open the TAL Distributors database –Click the CREATE tab on the ribbon –Click the Query Design button in the Queries group –Click the Close button in the Show Table dialog box –Click the View button arrow in the Results group on the QUERY DESIGN TOOLS tab, then click SQL View –The Query1 tab displays the query in SQL view, ready for you to type your SQL commands 7 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Table Creation SQL CREATE TABLE command –Creates a table by describing its layout Typical restrictions placed on table and column names by DBMS –Names cannot exceed 18 characters –Names must start with a letter –Names can contain only letters, numbers, and underscores (_) –Names cannot contain spaces 8 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Table Creation (continued) INTEGER –Number without a decimal point SMALLINT –Uses less space than INTEGER DECIMAL(p,q) –P number of digits; q number of decimal places CHAR(n) –Character string n places long DATE –Dates in DD-MON-YYYY or MM/DD/YYYY form 9 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Simple Retrieval SELECT-FROM-WHERE: SQL retrieval command –SELECT clause: lists fields to display –FROM clause: lists table or tables that contain data to display in query results –WHERE clause (optional): lists any conditions to be applied to the data to retrieve Simple condition: field name, a comparison operator, and either another field name or a value 10 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Simple Retrieval (continued) FIGURE 3-1: SQL query to select customer data (Access) 11 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Simple Retrieval (continued) FIGURE 3-3: SQL query to list the complete Item table 12 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Simple Retrieval (continued) FIGURE 3-5: SQL query with WHERE condition 13 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Simple Retrieval (continued) FIGURE 3-6: Query results 14 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Simple Retrieval (continued) FIGURE 3-7: Comparison operators used in SQL commands 15 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Compound Conditions Compound condition –Connecting two or more simple conditions using one or both of the following operators: AND and OR –Preceding a single condition with the NOT operator Connecting simple conditions using AND operator –All of the simple conditions must be true for the compound condition to be true Connecting simple conditions using OR operator –Any of the simple conditions must be true for the compound condition to be true 16 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Compound Conditions (continued) FIGURE 3-15: Query results FIGURE 3-14: Compound condition that uses the AND operator 17 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Compound Conditions (continued) FIGURE 3-17: Query results FIGURE 3-16: Compound condition that uses the OR operator 18 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Compound Conditions (continued) Preceding a condition by NOT operator –Reverses the truth or falsity of the original condition BETWEEN operator –Value must be between the listed numbers 19 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Computed Fields Computed field or calculated field –Field whose values you derive from existing fields –Can involve: Addition (+) Subtraction (-) Multiplication (*) Division (/) 20 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Computed Fields (continued) FIGURE 3-25: Query results FIGURE 3-24: SQL query with a computed field and condition 21 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Using Special Operators (LIKE and IN) Wildcards in Access SQL –Asterisk (*): collection of characters –Question mark (?): any individual character Wildcards in other versions of SQL –Percent sign (%): any collection of characters –Underscore (_): any individual character To use a wildcard, include the LIKE operator in the WHERE clause IN operator provides a concise way of phrasing certain conditions 22 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Using Special Operators (LIKE and IN) (continued) FIGURE 3-27: Query results FIGURE 3-26: SQL query with a LIKE operator 23 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Using Special Operators (LIKE and IN) (continued) FIGURE 3-29: Query results FIGURE 3-28: SQL query with an IN operator 24 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Sorting Sort data using the ORDER BY clause Sort key: field on which to sort data When sorting data on two fields: –Major sort key (or primary sort key): more important sort key –Minor sort key (or secondary sort key): less important sort key 25 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Sorting (continued) FIGURE 3-33: Query results FIGURE 3-32: SQL query to sort data on multiple fields 26 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Built-in Functions Built-in functions (aggregate functions) in SQL –COUNT: calculates number of entries –SUM or AVG: calculates sum or average of all entries in a given column –MAX or MIN: calculates largest or smallest values respectively 27 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Built-in Functions (continued) FIGURE 3-35: Query results FIGURE 3-34: SQL query to count records 28 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Subqueries Subquery: inner query Subquery is evaluated first Outer query is evaluated after the subquery 29 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Subqueries (continued) FIGURE 3-41: Query results FIGURE 3-40: SQL query with a subquery 30 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Grouping Create groups of records that share a common characteristic GROUP BY clause indicates grouping in SQL HAVING clause is to groups what the WHERE clause is to rows 31 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Grouping (continued) FIGURE 3-45: Query results FIGURE 3-44: SQL query to restrict the groups that are included 32 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Joining Tables Queries can locate data from more than one table Enter appropriate conditions in the WHERE clause To join tables, construct the SQL command as: 1.SELECT clause: list all fields you want to display 2.FROM clause: list all tables involved in the query 3.WHERE clause: give the condition that will restrict the data to be retrieved to only those rows from the two tables that match 33 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Joining Tables (continued) FIGURE 3-48: SQL query to join tables 34 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Joining Tables (continued) FIGURE 3-49: Query results 35 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Union Union of two tables is a table containing all rows in the first table, the second table, or both tables Two tables involved must be union compatible –Same number of fields –Corresponding fields must have same data types 36 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Union (continued) FIGURE 3-55: Query results FIGURE 3-54: SQL query to perform a union 37 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Updating Tables UPDATE command makes changes to existing data INSERT command adds new data to a table DELETE command deletes data from the database 38 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Updating Tables (continued) FIGURE 3-57: SQL query to insert a row FIGURE 3-56: SQL query to update data 39 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

40 Updating Tables (continued) FIGURE 3-58: SQL query to delete rows 40 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Creating a Table from a Query INTO clause –Saves the results of a query as a table –Specified before FROM and WHERE clauses Oracle –Create the new table using a CREATE TABLE command –Use an INSERT command to insert the appropriate data into the new table 41 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

42 Creating a Table from a Query (continued) FIGURE 3-59: Query to create a new table (Access) 42 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Summary of SQL Commands Generic versions of SQL commands for every example presented in this chapter In most cases, commands in Access are identical to the generic versions For those commands that differ, both the generic version and the Access version are included 43 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Summary Structured Query Language (SQL) is a language that is used to manipulate relational databases Basic form of an SQL query: SELECT-FROM- WHERE Use CREATE TABLE command to describe table layout to the DBMS, which creates the table In SQL retrieval commands, fields are listed after SELECT, tables are listed after FROM, and conditions are listed after WHERE In conditions, character values must be enclosed in single quotation marks 44 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Summary (continued) Compound conditions are formed by combining simple conditions using either or both of the following operators: AND and OR Sorting is accomplished using ORDER BY clause When the data is sorted in more than one field, can have a major and minor sort key Grouping: use the GROUP BY clause HAVING clause: restricts the rows to be displayed 45 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Summary (continued) Joining tables: use a condition that relates matching rows in the tables to be joined Built-in (aggregate) functions: COUNT, SUM, AVG, MAX, and MIN One SQL query can be placed inside another; the subquery is evaluated first UNION operator: unifies the results of two queries 46 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Summary (continued) Calculated fields: include the calculation, the word AS, the name of the calculated field INSERT command adds a new row to a table UPDATE command changes existing data DELETE command deletes records INTO clause is used in a SELECT command to create a table containing the results of the query 47 ©2015 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.