Creating and Maintaining

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

© Abdou Illia MIS Spring 2014
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.
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Introduction to Structured Query Language (SQL)
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
A Guide to Oracle9i1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
1 Chapter 3: Using Oracle to Add, View, and Update Data.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Guide to Oracle 10g1 Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Introduction to Structured Query Language (SQL)
1 Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Microsoft Access 2010 Chapter 7 Using SQL.
Guide to Oracle10G1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
Concepts of Database Management, Fifth Edition
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Chapter 9 Joining Data from Multiple Tables
ITBIS373 Database Development
Oracle Database Administration Lecture 2 SQL language.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Concepts of Database Management Seventh Edition
Using Special Operators (LIKE and IN)
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Lecture3b - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data Guide to Oracle 10g ITBIS373 Database Development.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
 CONACT UC:  Magnific training   
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Enhanced Guide to Oracle 10g
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Client/Server Databases and the Oracle 10g Relational Database
Database Systems: Design, Implementation, and Management Tenth Edition
David M. Kroenke and David J
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 4 Indexes.
CH 4 Indexes.
Structured Query Language
A Guide to SQL, Eighth Edition
CH 4 Indexes.
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Contents Preface I Introduction Lesson Objectives I-2
Query Functions.
Spreadsheets, Modelling & Databases
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Joins and other advanced Queries
Displaying Data from Multiple Tables
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Creating and Maintaining Database Systems Creating and Maintaining Database Objects Part 2 Teaching Tip: If you are using this slideshow in a computer classroom, students can enter commands to the MY_FACULTY and MY_STUDENTS tables as you introduce them. Before using this slideshow, create the tables using the following commands: CREATE TABLE my_faculty (f_id NUMBER(6), f_name VARCHAR2(30)); CREATE TABLE my_students (s_name VARCHAR2 (30), s_dob DATE, s_class CHAR(2), s_age NUMBER(2)); Then have students enter commands as they are introduced.

Date Arithmetic To find a date that is a specific number of days before or after a known date, add or subtract the number from the known date Example: SELECT order_date + 30 FROM cust_order;

Date Arithmetic To find the number of days between two known dates, subtract the later date from the earlier date Example: SELECT SYSDATE – s_dob FROM my_students;

Date Functions ADD_MONTHS Example: returns a date that is a specific number of months after a given date Example: SELECT ADD_MONTHS(SYSDATE, 6) FROM dual;

Date Functions LAST_DATE Example: Returns the date that is the last day of the month specified in the current date Example: SELECT LAST_DATE(order_date) FROM cust_order WHERE order_id = 1057;

Date Functions MONTHS_BETWEEN Example: Returns the number of months between two input dates Example: SELECT MONTHS_BETWEEN(order_date, SYSDATE) FROM cust_order WHERE order_id = 1057;

Group Functions Used to perform an operation on a field from a group of retrieved records AVG (average of all retrieved values) COUNT (number of records retrieved) MAX (maximum value retrieved) MIN (minimum value retrieved) SUM (sum of all retrieved values)

Group Function Examples SELECT AVG (s_age) FROM my_students; SELECT MAX (s_age) FROM my_students; SELECT MIN (s_age) FROM my_students; SELECT SUM (s_age) FROM my_students;

Using the GROUP BY Clause GROUP BY must be used if some columns in the SELECT clause are used in a group function and some are not Group all fields that are not included in the group function Example: SELECT s_class, AVG(s_age) FROM my_students GROUP BY s_class;

Creating Alternate Column Headings in SQL*Plus Syntax: SELECT column1 “heading1”, column2 “heading2”, … Example: SELECT (SYSDATE – s_dob) “Student Age” FROM my_students;

Creating a Column Alias Column alias: alternate column name that can be referenced in the ORDER BY and GROUP BY clauses Syntax: SELECT column1 AS alias1 … Example: SELECT (SYSDATE – s_dob) AS age_alias ORDER BY age_alias

Dynamic SQL Queries Queries that allow users to specify search conditions at runtime Approaches Substitution Values Runtime Variables

Using Substitution Values Created when search expression is prefaced with an ampersand (&) System then prompts user for value

Using Runtime Variables Runtime variable: variable defined in SQL*Plus environment Syntax: DEFINE variable_name = variable_value; You can then substitute the variable name for a query search condition value

Using Runtime Variables Example:

Formatting Data Using the TO_CHAR Function Used to display NUMBER and DATE values using a specific format mask Syntax: TO_CHAR(fieldname, ‘format_mask’);

Join Queries Retrieve data from multiple tables by joining tables using foreign key references Join query types: Inner (equality) Outer Self Inequality

Inner Joins One record is retrieved for each matching row

Inner Joins Syntax: SELECT column1, column2, … FROM table1, table2 WHERE table1.join_column = table2.join_column You must include a join condition for every link between 2 tables Join condition

Inner Joins Example: SELECT s_name, f_name FROM student, faculty WHERE student.f_id = faculty.f_id; If you have N tables in the FROM clause, you must have (N - 1) join conditions

Qualifying Field Names If a field in the SELECT clause exists in multiple tables in the FROM clause, you must qualify the field name by prefacing it with either table’s name

Process for Designing Complex Inner Join Queries Identify all of the tables involved in the query, and label: Display fields Join fields Search fields Write the query List all display fields in the SELECT clause List all table names in the FROM clause List all join condition links in the WHERE clause List all search fields in the WHERE clause

Outer Joins Limitation of inner joins: some records may be omitted if corresponding records don’t exist in one of the tables Example: retrieve records for all students, along with their corresponding ENROLLMENT information

Outer Joins Student 105 (Michael Connoly) does not have any ENROLLMENT records

Outer Joins No records retrieved for Michael:

Outer Joins To include records in first (inner) table, even when they do not have matching records in second (outer) table, place outer join marker (+) beside outer table name in join clause

Outer Joins Outer join marker

Self Joins Used to join a table to itself when the table has a hierarchical relationship

Self Joins To create a self-join, you need to create a table alias, which gives an alternate name to the table so you can create a join condition Syntax to create table alias in FROM clause: FROM table1 alias1, table2 alias2

Self Joins PARENT_PROJECT SUB_PROJECT PROJECT

Self Join Example

Inequality Joins Join created by placing making join condition satisfy an inequality condition

Inequality Joins

Nested Queries Created when a sub-query is nested within a main query Main query: first query listed in SELECT command Sub-query: retrieves one or more values that specify the main query’s search condition

Nested Query Where Sub-query Returns a Single Value Syntax: SELECT column1, column2, … FROM table1, table2, … WHERE join conditions AND search_column1 = (SELECT column1 FROM table1, table2, … WHERE search and join conditions) Sub-query that returns one value

Nested Query Where Sub-query Returns Multiple Values Syntax: SELECT column1, column2, … FROM table1, table2, … WHERE join conditions AND search_column1 IN (SELECT column1 FROM table1, table2, … WHERE search and join conditions) Sub-query that returns multiple values

Using Set Operators in Queries Performs set operations on outputs of two unrelated queries Both queries must have: same number of display fields corresponding display fields must have same data type

Query Set Operators UNION: combines results, suppresses duplicate rows UNION ALL: combines results, displays duplicates INTERSECT: finds matching rows MINUS: returns the difference between returned record sets

Selecting Records For Update In a normal SELECT command, the retrieved records are not locked, and are available for other users to view, updated, and delete Sometimes, you need to select records, and then immediately update them based on the retrieved values Airline seat reservations Inventory items for sale

Selecting Records For Update Syntax: SELECT column1, column2, … FROM table1, table2, … WHERE search and join conditions FOR UPDATE OF column1, column2, … NOWAIT;

Selecting Records For Update All retrieved records are locked until you issue a COMMIT command Fields listed in FOR UPDATE clause are for documentation purposes only NOWAIT clause is optional Makes it so when another user tries to retrieved locked record, their system doesn’t just “hang”

Database Views Logical table based on a query Does not physically exist in the database Presents data in a different format from underlying tables Uses: Security Simplifying complex queries

Database Views Creating a view: CREATE VIEW view_name AS SQL_command; Views can be queried just like tables: SELECT * FROM view_name;

Simple Views Based on SQL query that retrieves data from only one table View can support all table operations: INSERT UPDATE DELETE

Complex Views Based on query that retrieves data from multiple tables Can only be used to support SELECT operations No table operations supported

Indexes Index: Separate table is maintained that shows index keys and physical locations of corresponding records In Oracle, ROWID is translated to physical location of row on disk Improves response time of searches and joins SLName ROWID Brown 13387289 Jones 13879872 Smith 58925789 Helgeson 29875018

Using Indexes Create table index AFTER table is populated with data Indexes make INSERT, UPDATE, and DELETE operations slower because index must also be maintained

Indexing Strategies A table can have indexes on multiple fields Create indexes based on fields used for search or join operations Typically, indexes only speed retrievals when <15% of the table records are involved Each additional index adds processing overhead for INSERT, UPDATE, and DELETE operations In Oracle, primary keys are automatically indexed

Creating Indexes Syntax: CREATE INDEX index_name ON tablename(index_field);

Synonyms Alternate name for a table Allows you to not have to preface table with owner’s username when you are querying a table that belongs to another user

Public Synonyms Can only be created by a DBA Syntax: CREATE PUBLIC SYNONYM synonym_name FOR owner_name.tablename; All users with privileges to use table can then use synonym instead of owner_name.tablename

Private Synonyms You can create private synonyms for any tables that you have privileges to use Only you can use the synonym Syntax: CREATE SYNONYM synonym_name FOR table_name.table_name;

End of Lecture