A Guide to Oracle9i1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

© Abdou Illia MIS Spring 2014
Virtual training week 4 structured query language (SQL)
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Introduction to Structured Query Language (SQL)
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
1 Chapter 3: Using Oracle to Add, View, and Update Data.
Transaction Sen Zhang. Creating Transactions and Committing New Data Transaction: series of action queries that represent a logical unit of work User.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
View Sen Zhang. Views are very common in business systems users view of data is simplified a form of security - user sees only the data he/she needs to.
Guide to Oracle 10g1 Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
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.
Using SQL Queries to Insert, Update, Delete, and View Data © Abdou Illia MIS Spring 2015 Wednesday 1/28/2015 Chapter 3A.
Microsoft Access 2010 Chapter 7 Using SQL.
Basic queries Sen Zhang. 2 Objectives Create search conditions in SQL queries Learn how to write SQL queries to retrieve data from a single database table.
Guide to Oracle10G1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
ASP.NET Programming with C# and SQL Server First Edition
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Chapter 9 Joining Data from Multiple Tables
ITBIS373 Database Development
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
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.
Quick review of SQL And conversion to Oracle SQL.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
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 Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
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.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
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.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL Query Getting to the data ……..
Relational Database Design
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Introduction to Oracle9i: SQL
Creating and Maintaining
Manipulating Data.
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
Chapter 8 Advanced SQL.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

A Guide to Oracle9i1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3

A Guide to Oracle9i2 Lesson A Objectives Learn how to run a script to create database tables automatically Learn how to insert data into database tables Learn how to create database transactions and commit data to the database Create search conditions in SQL queries Understand how to update and delete database records, and how to truncate tables Learn how to create and use sequences to generate surrogate key values automatically Learn how to grant and revoke database object privileges

A Guide to Oracle9i3 Using Scripts to Create Database Tables One or more SQL commands saved in a text file Usually have.sql extension To run from SQL*Plus: –Start full file path (c:\temp\myfile.sql) file path –Extension can be omitted if it is.sql

A Guide to Oracle9i4 Inserting Data into Tables INSERT command adds new records Field values should match column order, or be specified in command INSERT INTO faculty (F_ID, F_LAST, F_FIRST, F_MI, LOC_ID) VALUES (1, 'Cox', 'Kim', 'J', 9);

A Guide to Oracle9i5 Format Models Used to format data retrieved from database Can be used to format a date to display time or a number to display as a currency

A Guide to Oracle9i6 Numerical Format Models

A Guide to Oracle9i7 Date Format Models

A Guide to Oracle9i8 Date Format Models

A Guide to Oracle9i9 Inserting Date and Interval Values Use to_date function to convert a character string to a date –Specify date string and matching format model –TO_DATE('08/24/2004', 'MM/DD/YYYY') –TO_DATE('10:00 AM', 'HH:MI AM') Use functions to convert character strings to intervals –TO_YMINTERVAL('4-9') inserts a positive interval of 4 years, 9 months –TO_DSINTERVAL('0 01:15:00') inserts a positive interval of 4 days, 1 hour, 15 minutes, 0 seconds

A Guide to Oracle9i10 Inserting LOBs Before inserting LOB must insert a LOB locator LOB locator: a structure that contains information that identifies the LOB data type and points to the alternate memory location Write a program or use a utility to add LOB data to database Use EMPTY_BLOB() function to insert a LOB locator

A Guide to Oracle9i11 Creating Transactions and Committing New Data Transaction: series of action queries that represent a logical unit of work User can commit (save) changes User can roll back (discard) changes Pending transaction: a transaction waiting to be committed or rolled back Oracle DBMS locks records associated with pending transactions Other users cannot view or modify locked records

A Guide to Oracle9i12 Commit and Roll Back in SQL*Plus Transactions begin automatically with first command Type COMMIT to commit changes Type ROLLBACK to roll back changes

A Guide to Oracle9i13 Savepoints A bookmark that designates the beginning of an individual section of a transaction Changes are rolled back to savepoint

A Guide to Oracle9i14 Creating Search Conditions in SQL Queries An expression that seeks to match specific table records Used in SELECT, UPDATE and DELETE statements WHERE fieldname comparison_operator search_expression WHERE S_ID = 1

A Guide to Oracle9i15 Comparison Operators

A Guide to Oracle9i16 Defining Search Expressions Character strings –Must be enclosed in single quotes –Case sensitive Dates –Use to_date function with date string and format model Intervals –Use to_yminterval and to_dsinterval with interval string format model

A Guide to Oracle9i17 Creating Complex Search Conditions Combines multiple search conditions using the AND,OR, and NOT logical operators. AND – both conditions must be true OR – one or both condition must be true NOT – opposite of actual value Use () to group logical operators

A Guide to Oracle9i18 Updating and Deleting Existing Table Records UPDATE: –Updates field values in one or more records in a table –Only one table may be updated at a time –UPDATE tablename SET field1= new_value1, field2 = new_value2,... WHERE search condition; DELETE: –Removes specific records from a database table –If search condition is omitted, entire table data is removed –DELETE FROM tablename WHERE search condition;

A Guide to Oracle9i19 Updating and Deleting Existing Table Records TRUNCATE –Removes all of the table data without saving any rollback information –Must disable foreign key constraints before truncating table –TRUNCATE TABLE tablename;

A Guide to Oracle9i20 Sequences Sequential lists of numbers to create unique surrogate key values To use a sequence: –SELECT sequence_name.NEXTVAL FROM DUAL; –INSERT INTO location LOC_ID) VALUES(loc_id_sequence.NEXTVAL);

A Guide to Oracle9i21 Database Object Privileges Privileges must be granted so that other users can access objects in user schema GRANT privilege1, privilege2,…ON object_name TO user1,user2,...; REVOKE privilege1, privilege2,... ON object_name FROM user1, user2,...; To grant or revoke privileges for everyone use PUBLIC as user

A Guide to Oracle9i22 Lesson B Objectives Learn how to write SQL queries to retrieve data from a single database table Create SQL queries that perform calculations on retrieved data Use SQL group functions to summarize retrieved data

A Guide to Oracle9i23 Retrieving Data from a Single Database Table SELECT fieldname1, fieldname2,... FROM ownername.tablename WHERE search_condition; To select: –All rows omit where clause –All fields, use *: SELECT * FROM … –Only unique field values: SELECT DISTINCT fieldname Search condition: –Use comparison and logical operators –IS NULL/IS NOT NULL to match/exclude NULL values –IN/NOT IN to match set values –LIKE with wildcards % and _ to match character strings

A Guide to Oracle9i24 Sorting Query Output Use ORDER BY sort_key_field(s) Default order is ascending, use DESC to sort descending

A Guide to Oracle9i25 Using Calculations in SQL Queries Calculations are performed by DBMS, result only sent to client Can use arithmetic operators (+, -, *, /) Place calculation in select clause: SELECT price * quantity FROM … Calculations can be performed on NUMBER, DATE and INTERVAL fields only Single-row functions: built in Oracle functions to perform calculations and manipulate retrieved data values

A Guide to Oracle9i26 Oracle9i SQL Group Functions Group functions: perform an operation on a group of queried rows and returns a single result

A Guide to Oracle9i27 Group By Use to group output by the field with duplicate values and apply group functions to the grouped data

A Guide to Oracle9i28 Having Use to place a search condition on results of group function calculations Like “WHERE” for group functions HAVING group_function comparison_operator value HAVING SUM(capacity) >= 100

A Guide to Oracle9i29 Formatting Output in SQL*Plus To change default column headings: –Specify alternate column headings: SELECT fieldname1 "heading1_text", fieldname2 "heading2_text",... –Use an alias for column headings: SELECT fieldname1 AS alias_name1... To change SQL*Plus line and page size settings –Select Options/Environment on menu bar –Modify linesize and pagesize to desired values

A Guide to Oracle9i30 Formatting Numbers and Dates Use to_char function with format models TO_CHAR(field_name, 'format_model') SELECT inv_id, TO_CHAR(inv_price, '$99,999.99') FROM inventory WHERE item_id = 1 ;

A Guide to Oracle9i31 Lesson C Objectives Learn how to create SQL queries that join multiple tables Learn how to create nested SQL queries Understand how to combine query results using set operators Create and use database views

A Guide to Oracle9i32 Joining Multiple Tables Join: combine data from multiple database tables using foreign key references SELECT field1, field2,... FROM table1, table2 WHERE table1.joinfield = table2.joinfield AND search_condition(s); If tables share field names, must prefix field in select with table name (table1.field1, table2.field1) Join condition: part of where clause indicating how tables are related (table1.foreign_key = table2.primary key) Search conditions can be added to join condition using AND operator

A Guide to Oracle9i33 Inner Join Join two tables based on values in one table being equal to values in another table Also known as equality join, equijoin or natural join Returns results only if records exist in both tables

A Guide to Oracle9i34 Joining Via Linking Table

A Guide to Oracle9i35 Using a Query Design Diagram Helpful for creating complicated queries Can use a formula to derive actual query from diagram

A Guide to Oracle9i36 Outer Join Returns all rows in one table and matching rows in joined table Inner table: all rows are returned Outer table: matching rows are returned Outer table marked with a + in join condition inner_table.join_field = outer_table.join_field(+) Null values are inserted for fields in outer table that are not found

A Guide to Oracle9i37 Self-Join A query that joins a table to itself Used when a table has a foreign key relationship to itself (usually parent-child relationship) Must create a table alias and structure the query as if you are joining the table to a copy of itself FROM table1 alias1,... Use alias, not table name for select and where clauses

A Guide to Oracle9i38 Self-Join Example

A Guide to Oracle9i39 Creating Nested Queries Used to select results based on the result of a query Consists of a main query and one or more subqueries. –Main query: first query that appears in the SELECT command –Subquery retrieves values that the main query’s search condition must match

A Guide to Oracle9i40 Creating Nested Queries Nested queries can return single or multiple values –To match single values use = operator –To match multiple values use IN operator Subqueries can be nested to more than one level (nested subqueries) Nested subqueries are slower than joins and should be used sparingly

A Guide to Oracle9i41 Nested Subquery Example

A Guide to Oracle9i42 Using Set Operators To Combine Query Results Use to select data from multiple tables not connected with foreign key relationships

A Guide to Oracle9i43 Set Operators query1 OPERATOR query2; (where operator is UNION, UNION ALL, INTERSECT, or MINUS) Both queries must have same number of select fields and same data types in same order UNION suppresses duplicate values UNION ALL includes duplicate values INTERSECT takes only matching fields MINUS takes only fields in query1 not query2

A Guide to Oracle9i44 Creating and Using Database Views Similar to storing the result of a query in the database Based on a source query that: –can specify a subset of a single table’s fields or records –can join multiple tables Can be used to enforce security (user has access to view but not underlying table)

A Guide to Oracle9i45 Creating and Using Database Views Views can be updateable if: –SELECT clause contains only fieldnames, no functions or calculations –cannot contain the ORDER BY, DISTINCT, or GROUP BY clauses, group functions, or set operators –search condition cannot contain a nested query Views are used like tables for selecting, inserting, updating and deleting data (only updatable views can be modified)

A Guide to Oracle9i46 Creating and Deleting Views CREATE OR REPLACE VIEW view_name AS source_query; DROP VIEW viewname;

A Guide to Oracle9i47 Summary Use INSERT commands to add data NUMBER, DATE and INTERVAL data types can be converted to and from character strings using format models Database changes are made within a transaction that can be committed or rolled back Use search conditions to specify records to update, delete or select Arithmetic, logical, grouping, and built-in Oracle functions can be used to specify search conditions and manipulate data Query output can be formatted by modifying SELECT clause

A Guide to Oracle9i48 Summary Results from more than one table related through foreign key relationships can be combined in a join Results from more than one unrelated table can be combined using set operators Queries can be “saved” by creating a view Views can be used like tables to select, insert, update and delete data