COS 346 Day 3 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition

Slides:



Advertisements
Similar presentations
Introduction to Structured Query Language (SQL)
Advertisements

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 2 Structured Query Language.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2.
Introduction to Structured Query Language (SQL)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
Getting Started (Excerpts) Chapter One DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Fundamentals, Design, and Implementation, 9/e Chapter 6 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.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation Chapter Two: Introduction to Structured Query Language.
Concepts of Database Management Sixth Edition
ASP.NET Programming with C# and SQL Server First Edition
Chapter 3 Single-Table Queries
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Concepts of Database Management Seventh Edition
Database Technical Session By: Prof. Adarsh Patel.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Two: Introduction to Structured Query Language.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
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.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
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.
Database Queries aka SQL (pronounced “ sequel ” ).
DAVID M. KROENKE’S DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Two: Introduction to Structured Query Language.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Enterprise Database Systems Introduction to SQL Server Dr. Georgia Garani Dr. Theodoros Mitakos Technological.
1 Structured Query Language (SQL) Pertemuan 09 Matakuliah: F0712 / Lab MS Access Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
CSIS 115 Database Design and Applications for Business Dr. Meg Fryling “Dr. Meg” Fall #csis115 © 2012 Meg Fryling.
3 A Guide to MySQL.
COS 346 Day 3 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
CSIS 115 Database Design and Applications for Business
TABLES AND INDEXES Ashima Wadhwa.
Advanced Accounting Information Systems
SQL and SQL*Plus Interaction
Using SQL*Plus.
CSIS 115 Database Design and Applications for Business
JDBC.
Using SQL*Plus.
Using SQL to Prepare Data for Analysis
ORACLE SQL Developer & SQLPLUS Statements
Database Processing: David M. Kroenke’s Chapter One: Introduction
David M. Kroenke and David J
DATABASE MANAGEMENT SYSTEM
Chapter 7 Working with Databases and MySQL
Chapter 8 Working with Databases and MySQL
Database Processing: David M. Kroenke’s Chapter One: Introduction
Chapter 22 - SQL, MySQL, DBI and ADO
Database Principles Constructed by Hanh Pham based on slides from: “Database Processing, Fundamentals, Design, and Implementation”, D. Kroenke, D. Auer,
Chapter 1 Introduction.
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Using SQL*Plus.
Getting Started Chapter One DATABASE CONCEPTS, 5th Edition
Getting Started Chapter One DATABASE CONCEPTS, 4th Edition
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

COS 346 Day 3 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Agenda Questions? WebCt accounts should be available Assignment 1 is DUE on Monday There was some confusion due to WebCT not being accessible in the first class Begin Intro to SQL Chap 2 in DP Chap 1 in SQL Good news! Looks like I’ll be getting CA to donate 12 or so copies of AllFusion Data Modeling Suite Bundles which includes ERwin to UMFK. $150,000+ donation DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Accessing the Cape Codd Database Download an MS access version from WebCT under Database section (chapter_2.mdb) Use the Oracle Server LittleBlack.advtech.local Database SID: CapeCodd You all have read access Account: Cos346 Password: Cos346 At a command Prompt type (temp fix) \\littleblack\oracle10g\namefile\fixnames.bat DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Using SQL*Plus DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

USING SQL*Plus Worksheet DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Using MS Access - Results DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Using MS SQL Server [SQL Query Analyzer] DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Using Oracle [SQL*Plus] DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Using Oracle [Quest Software’s TOAD] http://www.toadsoft.com/toad_oracle.htm DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Using MySQL [MySQL Command Line Client] DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Using MySQL [MySQL Query Browser] DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Sorting the Results: ORDER BY SELECT * FROM Chapter_2.ORDER_ITEM ORDER BY OrderNumber, Price; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Sort Order: Ascending and Descending SELECT * FROM Chapter_2.ORDER_ITEM ORDER BY Price DESC, OrderNumber ASC; NOTE: The default sort order is ASC – does not have to be specified. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE Clause Options: AND SELECT * FROM Chapter_2.SKU_DATA WHERE Department = 'Water Sports' AND Buyer = 'Nancy Meyers'; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE Clause Options: OR SELECT * FROM Chapter_2.SKU_DATA WHERE Department = 'Camping’ OR Department = 'Climbing’; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE Clause Options:- IN SELECT * FROM Chapter_2.SKU_DATA WHERE Buyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE Clause Options: NOT IN SELECT * FROM Chapter_2.SKU_DATA WHERE Buyer NOT IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE Clause Options: Ranges with BETWEEN SELECT * FROM Chapter_2.ORDER_ITEM WHERE ExtendedPrice BETWEEN 100 AND 200; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE Clause Options: Ranges with Math Symbols SELECT * FROM Chapter_2.ORDER_ITEM WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE Clause Options: LIKE and Wildcards The SQL keyword LIKE can be combined with wildcard symbols: SQL 92 Standard (SQL Server, Oracle, etc.): _ = Exactly one character % = Any set of one or more characters MS Access (based on MS DOS) ? = Exactly one character * = Any set of one or more characters DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE Clause Options: LIKE and Wildcards (Continued) SELECT * FROM Chapter_2.SKU_DATA WHERE Buyer LIKE 'Pete%'; MS ACCESS Buyer LIKE ‘Pete*’; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE Clause Options: LIKE and Wildcards (Continued) SELECT * FROM Chapter_2.SKU_DATA WHERE SKU_Description LIKE '%Tent%'; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE Clause Options: LIKE and Wildcards SELECT * FROM Chapter_2.SKU_DATA WHERE SKU LIKE '%2__'; MS ACCESS SKU LIKE ‘*2??’; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Built-in Functions There are five SQL Built-in Functions: COUNT SUM AVG MIN MAX DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Built-in Functions (Continued) SELECT SUM (ExtendedPrice) AS Order3000Sum FROM Chapter_2.ORDER_ITEM WHERE OrderNumber = 3000; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Built-in Functions (Continued) SELECT SUM (ExtendedPrice) AS OrderItemSum, AVG (ExtendedPrice) AS OrderItemAvg, MIN (ExtendedPrice) AS OrderItemMin, MAX (ExtendedPrice) AS OrderItemMax FROM Chapter_2.ORDER_ITEM; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Built-in Functions (Continued) SELECT COUNT(*) AS NumRows FROM Chapter_2.ORDER_ITEM; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Built-in Functions (Continued) SELECT COUNT (DISTINCT Department) AS DeptCount FROM Chapter_2.SKU_DATA; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Arithmetic in SELECT Statements SELECT Quantity * Price AS EP, ExtendedPrice FROM Chapter_2.ORDER_ITEM; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

String Functions in SELECT Statements (T-SQL) SELECT DISTINCT RTRIM (Buyer) + ' in ' + RTRIM (Department) AS Sponsor FROM Chapter_2.SKU_DATA; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

String Functions in SELECT Statements (SQL*PLUS) SELECT DISTINCT CONCAT(RTRIM(Buyer), CONCAT(' in ',RTRIM(Department))) AS Sponsor FROM Chapter_2.SKU_DATA; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

The SQL keyword GROUP BY SELECT Department, Buyer, COUNT(*) AS Dept_Buyer_SKU_Count FROM Chapter_2.SKU_DATA GROUP BY Department, Buyer; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

The SQL keyword GROUP BY (Continued) In general, place WHERE before GROUP BY. Some DBMS products do not require that placement, but to be safe, always put WHERE before GROUP BY. The HAVING operator restricts the groups that are presented in the result. There is an ambiguity in statements that include both WHERE and HAVING clauses. The results can vary, so to eliminate this ambiguity SQL always applies WHERE before HAVING. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

The SQL keyword GROUP BY (Continued) SELECT Department, COUNT(*) AS Dept_SKU_Count FROM chapter_2.SKU_DATA WHERE SKU <> 302000 GROUP BY Department ORDER BY Dept_SKU_Count; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

The SQL keyword GROUP BY (Continued) SELECT Department, COUNT(*) AS Dept_SKU_Count FROM Chapter_2.SKU_DATA WHERE SKU <> 302000 GROUP BY Department HAVING COUNT (*) > 1 ORDER BY Dept_SKU_Count; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Querying Multiple Tables: Subqueries SELECT SUM (ExtendedPrice) AS Revenue FROM chapter_2.ORDER_ITEM WHERE SKU IN (SELECT SKU FROM chapter_2.SKU_DATA WHERE Department = 'Water Sports'); Note: The second SELECT statement is a subquery. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Querying Multiple Tables: Subqueries (Continued) SELECT Buyer FROM chapter_2.SKU_DATA WHERE SKU IN (SELECT SKU FROM chapter_2.ORDER_ITEM WHERE OrderNumber IN (SELECT OrderNumber FROM chapter_2.RETAIL_ORDER WHERE OrderMonth = 'January' AND OrderYear = 2004)); DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Querying Multiple Tables: Joins SELECT Buyer, ExtendedPrice FROM Chapter_2.SKU_DATA, Chapter_2.ORDER_ITEM WHERE SKU_DATA.SKU = ORDER_ITEM.SKU; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Querying Multiple Tables: Joins (Continued) SELECT Buyer, SUM(ExtendedPrice) AS BuyerRevenue FROM Chapter_2.SKU_DATA, chapter_2.ORDER_ITEM WHERE SKU_DATA.SKU = ORDER_ITEM.SKU GROUP BY Buyer ORDER BY BuyerRevenue DESC; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Querying Multiple Tables: Joins (Continued) SELECT Buyer, ExtendedPrice, OrderMonth FROM Chapter_2.SKU_DATA, chapter_2.ORDER_ITEM, chapter_2.RETAIL_ORDER WHERE SKU_DATA.SKU = ORDER_ITEM.SKU AND ORDER_ITEM.OrderNumber = RETAIL_ORDER.OrderNumber; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Subqueries versus Joins Subqueries and joins both process multiple tables. A subquery can only be used to retrieve data from the top table. A join can be used to obtain data from any number of tables, including the “top table” of the subquery. In Chapter 7, we will study the correlated subquery. That kind of subquery can do work that is not possible with joins. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

David M. Kroenke’s Database Processing Fundamentals, Design, and Implementation (10th Edition) End of Presentation: Chapter Two Part Two DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

CHAPTER 1: INTRODUCTION Oracle SQL BORDOLOI & BOCK DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL SQL, pronounced ‘Sequel’ or simply S-Q-L, is a computer programming language that was developed especially for querying relational databases using a non-procedural approach. The term non-procedural means that you can extract information by simply telling the system what information is needed without telling how to perform the data retrieval. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Extracting information from the database by using SQL is termed querying the database. SQL is a language that is fairly simple to learn in terms of writing queries, but it has considerable complexity because it is a very powerful language. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

DATA AND INFORMATION Information is derived from raw facts known as data. Data have little meaning or usefulness to managers unless they are organized in some logical manner. One of the most efficient ways to organize and manage data is through the use of a database management system (DBMS). DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

DBMS Common relational DBMS products are the Oracle RDBMS, IBM’s DB2, Microsoft’s SQL Server, and Microsoft’s desktop single user RDBMS named Microsoft Access. A DBMS provides both systems development professionals and information users with an easy-to-use interface to their organization’s database. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

DATA Two types of data are stored within a database: User data: Data that must be stored by an organization. System data: Data the database needs to manage user data to manage itself. This is also termed metadata, or the data about data. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Relational Database The most common type of DBMS software in use today is termed a relational DBMS or RDBMS. A relational database stores data in the form of tables. A table is defined as a collection of rows and columns. The tables are formally known as relations; this is where the relational database gets its name. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

TABLE In the figure, rows represent records and columns represent fields in a file processing sense. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

DATABASE MANAGEMENT SYSTEM A database management system (DBMS) manages the data in a database. A DBMS is a collection of programs that enables the users to create and maintain a database. A DBMS also enables data to be shared; information system users and managers can get more information value from the same amount of data when data sharing occurs. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

ORACLE’S RELATIONAL DBMS Oracle Corporation’s RDBMS is one of the widely used RDBMS products. An RDBMS performs all the basic functions of the DBMS software along with a magnitude of other functions that make the relational model easier to understand and to implement. RDBMS provides services that allow information systems professionals to easily change the structure of a database. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

ORACLE The significant features of Oracle are: Security mechanisms Backup and recovery Space management Open connectivity Tools and applications DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL AND ORACLE’S SQL*PLUS Data are manipulated by RDBMS users through the use of special data manipulation language. Database structures can also be defined by the use of data definition language. SQL is the most popular database language and has commands that enable it to be used for both manipulation and definition of databases. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL SQL is used by Oracle for all interaction with the database. SQL statements fall into two major categories: Data Definition Language (DDL): Set of SQL commands that create and define objects in a database. Data Manipulation Language (DML): Set of SQL commands that allows users to manipulate the data in a database. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL SQL is basically a free format language. This means that there are no particular spacing rules that must be followed when typing SQL commands. SQL is a nonprocedural language. This means that the user only has to specify the task for the DBMS to complete, but not how the task is to be completed. The RDBMS parses (converts) the SQL commands and completes the task. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL*PLUS Through SQL*PLUS users can: Enter, edit, store, retrieve, and run SQL commands and PL/SQL blocks. Format, perform calculations on, store, and print query results in the form of reports. List column definitions for any table. Access and copy data between SQL databases. Send messages to and accept responses from an information system user. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

RELATIONAL OPERATIONS SQL operations for creating new tables, inserting table rows, updating table rows, deleting table rows, and querying databases are the primary means of interfacing with relational databases. The SELECT statement is used primarily to write queries that extract information from the database which is a collection of related tables. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

The SELECT Statement The power of SELECT statement comes from its ability to combine data from many tables to produce output in the form of a result table. A select statement can include or exclude specific columns from one or more tables. SELECT emp_ssn, emp_last_name FROM employee; EMP_SSN EMP_LAST_NAME --------- ------------- 999666666 Bordoloi 999555555 Joyner 999444444 Zhu DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Selection Operation A selection operation selects a subset of rows in a table (relation) that satisfies a selection condition. That subset can range from no rows to all rows in a table. SELECT emp_ssn, emp_first_name FROM employee WHERE emp_ssn = '999111111';   EMP_SSN EMP_FIRST_NAME --------- --------------- 999111111 Douglas DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Projection Operation A projection operation selects only certain columns from the table, thus producing a subset of all available columns. The result table can include anything from a single column to all the columns in the table. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

EXAMPLE This SELECT statement selects a subset of columns from the employee table by specifying the columns to be listed. SELECT emp_ssn, emp_first_name, emp_last_name FROM employee;   EMP_SSN EMP_FIRST_NAME EMP_LAST_NAME --------- -------------- ------------------ 999666666 Bijoy Bordoloi 999555555 Suzanne Joyner 999444444 Waiman Zhu more rows will be displayed . . . DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Join Operation A join operation combines data from two or more tables based upon one or more common column values. The relational join is a very powerful operation because it allows users to investigate relationships among data elements. The following SELECT statement displays column information from both the employee and department tables. This SELECT statement also completes both selection and projection operations. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Example The tables are joined upon values stored in the department number columns named emp_dpt_number in the employee table and dpt_no in the department table. SELECT emp_ssn, emp_first_name, emp_last_name, emp_dpt_number FROM employee e, department d WHERE e.emp_dpt_number = d.dpt_no;   EMP_SSN EMP_FIRST_NAME EMP_LAST_NAME EMP_DPT_NUMBER --------- --------------- --------------- -------------- 999666666 Bijoy Bordoloi 1 999555555 Suzanne Joyner 3 999444444 Waiman Zhu 7 more rows will be displayed . . . DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL*PLUS SQL*PLUS enables the user to manipulate SQL commands and to perform many additional tasks as well. Through SQL*PLUS, a user can: Enter, edit, store, and retrieve SQL commands. Format, perform calculations on, store, and print query results in the forms of reports. contd DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL*PLUS List column definitions for any table. Access and copy data between SQL databases. Send messages to and accept responses from a system user. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Starting SQL*PLUS The most common types used are either the standard Oracle SQL*PLUS available for a Windows-type interface or by connecting to an Oracle database via a telnet session. The following Log On session is a connection to an Oracle database stored on a Sun Microsystems server running the Unix O/S. An SQL*PLUS session is begun by typing the command sqlplus at the command prompt($) and entering the username and password information. contd DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Log-On Screen DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Exiting SQL*PLUS The SQL*PLUS session can be ended by typing a command at the SQL prompt to terminate the session. Two commands are available: exit or quit. Either one of these can be used to terminate an SQL*PLUS session. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Running (Executing) a Command File The command to run (execute) a command file is: SQL> START filename.sql A form of shorthand for the word “start” can be used when executing a command file by replacing “start” with the @ (“at” symbol). DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Syntax Conventions Each select statement must follow precise syntactical and structural rules. The following is the minimum structure and syntax required for an SQL SELECT statement. SELECT [DISTINCT | ALL] {* | select_list} FROM {table_name [alias] | view_name} DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Keywords Keywords are words that have a predefined meaning in SQL. In practice, keywords may be entered in upper or lower case letters. SELECT * FROM employee; select * from employee; DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Keywords In some cases keywords can be abbreviated. The allowed abbreviation is shown in upper case letters with the remainder shown in lower case, which means either the full word or only the upper case part can be used. DESCribe: can be entered as either DESC or DESCRIBE. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Naming Conventions Identifiers are the names given by information system developers or system users to database objects such as tables, columns, indexes, and other objects as well as the database itself. There are several rules for naming database objects that must be followed. Identifiers should contain between 1 and 30 characters. The first character must be either alphabetic (a-z, A-Z) or the @ symbol or _ (underscore). DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Naming Conventions After the first character, digits, letters, or the symbols $, #, or _(underscore) must be used. No embedded spaces are allowed in identifiers. SQL keywords cannot be used as an identifier. DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall