SlideShare a Scribd company logo
1 of 63
Chapter 3:
Using SQL Queries to Insert,
Update, Delete, and View Data
Guide to Oracle 10g
Guide to Oracle 10g 2
Lesson A Objectives
After completing this lesson, you should be able to:
• Run a script to create database tables
automatically
• Insert data into database tables
• Create database transactions and commit data to
the database
• Create search conditions in SQL queries
• Update and delete database records and truncate
tables
Guide to Oracle 10g 3
Lesson A Objectives (continued)
• Create and use sequences to generate surrogate
key values automatically
• Grant and revoke database object privileges
Guide to Oracle 10g 4
Using Scripts to Create Database
Tables
• Script
– Text file that contains one or more SQL commands
• Run a script
– Type start at SQL prompt
– Blank space
– Full path and filename of script file
Guide to Oracle 10g 5
Using the INSERT Command
• Basic syntax for inserting into every column:
INSERT into tablename
VALUES (column1_value,
column2_value, … );
• Basic syntax for inserting into selected columns
INSERT into tablename (columnname1,
columnname2, … );
VALUES (column1_value,
column2_value, … );
Guide to Oracle 10g 6
Using the INSERT Command
(continued)
• Ensure all foreign keys that new row references
have already been added to database
Guide to Oracle 10g 7
Format Models
• Also called format mask
• Used to specify different output format from
default
• For NUMBER data types
– 9 represents digit
• For DATE/TIMESTAMP data types
– Choose formats for year day, date, etc.
Guide to Oracle 10g 8
Inserting Date and Interval Values
• Inserting values into DATE columns
– Use TO_DATE function to convert string to DATE
– Syntax
•TO_DATE('date_string',
'date_format_model')
• Inserting values into INTERVAL columns
– Syntax
•TO_YMINTERVAL('years-months')
•TO_DSINTERVAL('days HH:MI:SS.99')
Guide to Oracle 10g 9
Inserting LOB Column Locators
• Oracle stores LOB data in separate physical
location from other types of data
• LOB locator
– Structure containing information that identifies
LOB data type
– Points to alternate memory location
• Create blob locator
– EMPTY_BLOB()
Guide to Oracle 10g 10
Creating Transactions and
Committing New Data
• Transaction
– Represents logical unit of work
– All of action queries must succeed or no
transactions can succeed
• Commit
– Save changes in transaction
• Rollback
– Discard changes in transaction
Guide to Oracle 10g 11
Creating Transactions and
Committing New Data (continued)
• Purpose of transaction processing
– Enable users to see consistent view of database
• New transaction begins when SQL*Plus started
and command executed
• Transaction ends when current transaction
committed
• COMMIT command commits transaction
• ROLLBACK command restores database to point
before last commit
Guide to Oracle 10g 12
Creating Search Conditions in SQL
Queries
• Search condition
– Expression that seeks to match specific table rows
• Syntax
– WHERE columnname
comparison_operator
search_expression
• Comparison operators include:
– Equality and inequality operators
– Set operators
Guide to Oracle 10g 13
Defining Search Expressions
• NUMBER example
– WHERE f_id = 1
• Character data example
– WHERE s_class = 'SR'
• DATE example
– WHERE s_dob =
TO_DATE('01/01/1980',
‘MM/DD/YYYY')
Guide to Oracle 10g 14
Creating Complex Search Conditions
• Complex search condition
– Combines multiple search conditions using logical
operators
• AND logical operator
– True if both conditions true
• OR logical operator
– True if one condition true
• NOT logical operator
– Matches opposite of search condition
Guide to Oracle 10g 15
Updating Table Rows
• UPDATE action query syntax
UPDATE tablename
SET column1 = new_value1, column2 =
new_value2, …
WHERE search condition;
Guide to Oracle 10g 16
Deleting Table Rows
• SQL DELETE action query
– Remove specific rows
• Truncate table
– Remove all rows
• DELETE query syntax
DELETE FROM tablename
WHERE search condition;
Guide to Oracle 10g 17
Deleting Table Rows (continued)
• Child row
– Row’s value is foreign key
– Cannot delete row if it has child row
• Unless first delete row in which foreign key value
exists
• TRUNCATE syntax
– TRUNCATE TABLE tablename;
• Cannot truncate table with foreign key constraints
– Must disable constraints first
Guide to Oracle 10g 18
Creating New Sequences
• CREATE SEQUENCE command
– DDL command
– No need to issue COMMIT command
Guide to Oracle 10g 19
General Syntax Used to Create a New
Sequence
Guide to Oracle 10g 20
Viewing Sequence Information
• Query USER_SEQUENCES data dictionary view
– sequence_name column displays sequence names
Guide to Oracle 10g 21
Using Sequences
• Pseudocolumn
– Acts like column in database table
– Actually command that returns specific value
• CURRVAL
– Returns most recent sequence value retrieved
• NEXTVAL
– Next available sequence value
– sequence_name.NEXTVAL
Guide to Oracle 10g 22
Using Sequences (continued)
• DUAL
– Simple table in system user schema
– More efficient to retrieve pseudocolumns from
DUAL
SELECT sequence_name.NEXTVAL
FROM DUAL;
• DBMS uses user sessions
– To ensure that all sequence users receive unique
sequence numbers
Guide to Oracle 10g 23
Database Object Privileges
Guide to Oracle 10g 24
Granting Object Privileges
• SQL GRANT command
– Syntax
GRANT privilege1, privilege2, …
ON object_name
TO user1, user2, …;
Guide to Oracle 10g 25
Revoking Table Privileges
• REVOKE command
– Syntax
REVOKE privilege1, privilege2, …
ON object_name
FROM user1, user2, …;
Guide to Oracle 10g 26
Lesson B Objectives
After completing this lesson, you should be able 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
Guide to Oracle 10g 27
Retrieving Data From a Single
Database Table
• Syntax
SELECT columnname1, columnname2, …
FROM ownername.tablename
[WHERE search_condition];
• Retrieve all of columns
– Use asterisk ( * ) as wildcard character in SELECT
clause
– SELECT * from …
Guide to Oracle 10g 28
Suppressing Duplicate Rows
• SQL DISTINCT qualifier
– Examines query output before it appears on screen
– Suppresses duplicate values
• Syntax
– SELECT DISTINCT columnname;
Guide to Oracle 10g 29
Using Search Conditions in SELECT
Queries
• Use search conditions to retrieve rows matching
specific criteria
– Exact search conditions
• Use equality operator
– Inexact search conditions
• Use inequality operators
• Search for NULL or NOT NULL values
– WHERE columnname IS NULL
– WHERE columnname IS NOT NULL
Guide to Oracle 10g 30
Using Search Conditions in SELECT
Queries (continued)
• IN comparison operator
– Match data values that are members of a set of
search values
• LIKE operator
– Use to match part of character string
– Syntax
•WHERE columnname LIKE 'string'
• Character string should contain wildcard character
%, or _, or both
Guide to Oracle 10g 31
Sorting Query Output
• ORDER BY clause
– Sort query output
– Syntax for select with ordered results
SELECT columnname1, columnname2, …
FROM ownername.tablename
WHERE search_condition
ORDER BY sort_key_column;
– Sort can be ascending or descending
– Can specify multiple sort keys
Guide to Oracle 10g 32
Using Calculations in SQL Queries
• Perform many calculations directly within SQL
queries
– Very efficient way to perform calculations
• Create SQL queries
– Perform basic arithmetic calculations
– Use variety of built-in functions
Guide to Oracle 10g 33
Performing Arithmetic Calculations
• Perform arithmetic calculations on columns that
have data types
– NUMBER
– DATE
– INTERVAL
• SYSDATE pseudocolumn
– Retrieves current system date
• Use + and – to calculate differences between dates
Guide to Oracle 10g 34
Oracle 10g SQL Functions
• Built-in functions perform calculations and
manipulate retrieved data values
• Called single-row functions
– Return single result for each row of data retrieved
• To use:
– List function name in SELECT clause followed by
required parameter in parentheses
Guide to Oracle 10g 35
Oracle 10g SQL Group Functions
• Group function
– Performs operation on group of queried rows
– Returns single result such as column sum
• To use:
– List function name followed by column name in
parentheses
Guide to Oracle 10g 36
Using the COUNT Group Function
• COUNT group function
– Returns integer representing number of rows that
query returns
• COUNT(*) version
– Calculates total number of rows in table that satisfy
given search condition
– Includes NULL values.
• The COUNT(columnname) version
– Does not include NULL values
Guide to Oracle 10g 37
Using the GROUP BY Clause to
Group Data
• GROUP BY clause
– Group output by column with duplicate values
– Apply group functions to grouped data
• Syntax
– GROUP BY group_columnname;
– Follows FROM clause
• All columns listed in SELECT clause must be
included in GROUP BY clause
Guide to Oracle 10g 38
Using the HAVING Clause to Filter
Grouped Data
• HAVING clause
– Place search condition on results of queries that
display group function calculations
• Syntax
– HAVING group_function
comparison_operator value
• Example
– HAVING sum(capacity) >= 100
Guide to Oracle 10g 39
Creating Alternate Column Headings
• Column headings for retrieved columns are names
of database table columns
• Specify alternate output heading text
SELECT columnname1 "heading1_text
",
columnname2 "heading2_text", …
Guide to Oracle 10g 40
Creating Alternate Column Headings
(continued)
• Alias
– Alternate name for query column
– Syntax
•SELECT columnname1 AS alias_name1…
Guide to Oracle 10g 41
Modifying the SQL*Plus Display
Environment
• SQL*Plus page consists of:
– Specific number of characters per line
– Specific number of lines per page
• linesize property
– Specifies how many characters appear on line
• pagesize property
– Specifies how many lines appear on page
• Modify using environment dialog box
Guide to Oracle 10g 42
Formatting Data Using Format
Models
• TO_CHAR function
– Convert column to character string
– Apply desired format model to value
– Syntax
•TO_CHAR(column_name,
'format_model')
– Use for data types
• DATE
• INTERVAL
• NUMBER
Guide to Oracle 10g 43
Lesson C Objectives
After completing this lesson, you should be able to:
• Create SQL queries that join multiple tables
• Create nested SQL queries
• Combine query results using set operators
• Create and use database views
Guide to Oracle 10g 44
Joining Multiple Tables
• Join
– Combine data from multiple database tables using
foreign key references
• Syntax
SELECT column1, column2, …
FROM table1, table2
WHERE table1.joincolumn =
table2.joincolumn
AND search_condition(s);
Guide to Oracle 10g 45
Joining Multiple Tables (continued)
• Must qualify column name in SELECT clause
– Specify name of table that contains column
followed by period then column name
• Join condition
– Specifies table names to be joined and column
names on which to join tables
• SQL supports multiple types of join queries
Guide to Oracle 10g 46
Inner Joins
• Simplest type of join
• VALUES in one table equal to values in other
table
• Also called:
– Equality join
– Equijoin
– Natural join
• Query design diagram
Guide to Oracle 10g 47
Deriving a SQL Query From a Query
Design Diagram
Guide to Oracle 10g 48
Outer Joins
• Returns all rows from one table
– Called inner table
• And matching rows from second table
– Called outer table
• Syntax
– inner_table.join_column =
outer_table.join_column(+)
Guide to Oracle 10g 49
Self-joins
• Query that joins table to itself
• Must create table alias
– Alternate name assigned to table in query’s FROM
clause
– Syntax
•FROM table1 alias1, …
Guide to Oracle 10g 50
Creating Nested Queries
• Nested query
– Consists of main query and one or more subqueries
– Main query
• First query that appears in SELECT command
– Subquery
• Retrieves values that main query’s search condition
must match
Guide to Oracle 10g 51
Creating Nested Queries with
Subqueries that Return a Single Value
Guide to Oracle 10g 52
Using Multiple Subqueries Within a
Nested Query
• Use AND and OR operators
– To join search conditions associated with
subqueries
Guide to Oracle 10g 53
Creating Nested Subqueries
• Nested subquery
– Subquery that contains second subquery that
specifies its search expression
Guide to Oracle 10g 54
UNION and UNION ALL
• UNION set operator
– Joins output of two unrelated queries into single
output result
– Syntax
•query1 UNION query2;
• UNION ALL operator
– Same as UNION but includes duplicate rows
Guide to Oracle 10g 55
INTERSECT
• Finds intersection in two queries
• Requires that both queries have same number of
display columns in SELECT statement
• Automatically suppresses duplicate rows
Guide to Oracle 10g 56
MINUS
• To find difference between two unrelated query
result list
Guide to Oracle 10g 57
Creating and Using Database Views
• Source query
– Used to create view
– Specify subset of single table’s columns or rows or
join multiple tables
• Updatable views
– Can be used to update database
Guide to Oracle 10g 58
Creating Views
• Syntax
CREATE VIEW view_name
AS source_query;
– Or
CREATE OR REPLACE VIEW view_name
AS source_query;
Guide to Oracle 10g 59
Executing Action Queries Using Views
• Use view to execute action queries that:
– Insert
– Update
– Delete data in underlying source tables
• Can also execute update action queries and delete
action queries using view
– Just as with database table
Guide to Oracle 10g 60
Retrieving Rows from Views
• Query view using SELECT statement
Guide to Oracle 10g 61
Removing Views
• DROP VIEW command
– Remove view from user schema
– Syntax
•DROP VIEW view_name;
Guide to Oracle 10g 62
Summary
• INSERT action query
• SQL search condition
– Match one or more database rows
• UPDATE action query
• DELETE command
• SELECT query
– DISTINCT qualifier
• Single row and group functions
Guide to Oracle 10g 63
Summary (continued)
• Can change appearance of SQL*Plus environment
• Join multiple tables in SELECT query
– Inner join
– Outer join
• Nested queries
• Set operators
• Views

More Related Content

What's hot (20)

Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Mysql
MysqlMysql
Mysql
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
4. plsql
4. plsql4. plsql
4. plsql
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
 
SQL
SQLSQL
SQL
 
SQL
SQLSQL
SQL
 

Similar to Using SQL Queries to Insert, Update, Delete, and View Data.ppt

Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)Dhani Ahmad
 
Lec 1 = introduction to structured query language (sql)
Lec 1 = introduction to structured query language (sql)Lec 1 = introduction to structured query language (sql)
Lec 1 = introduction to structured query language (sql)Faisal Anwar
 
Introduction to Structured Query Language (SQL) (1).ppt
Introduction to Structured Query Language (SQL) (1).pptIntroduction to Structured Query Language (SQL) (1).ppt
Introduction to Structured Query Language (SQL) (1).pptComputerScienceDepar6
 
Synapseindia dot net development chapter 8 asp dot net
Synapseindia dot net development  chapter 8 asp dot netSynapseindia dot net development  chapter 8 asp dot net
Synapseindia dot net development chapter 8 asp dot netSynapseindiappsdevelopment
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-dbuncleRhyme
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 Richie Rump
 
Dan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New FeaturesDan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New FeaturesEmbarcadero Technologies
 
Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012Brij Mishra
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objectssiavosh kaviani
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusChhom Karath
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsNirav Shah
 

Similar to Using SQL Queries to Insert, Update, Delete, and View Data.ppt (20)

Advanced sql
Advanced sqlAdvanced sql
Advanced sql
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
15925 structured query
15925 structured query15925 structured query
15925 structured query
 
Lec 1 = introduction to structured query language (sql)
Lec 1 = introduction to structured query language (sql)Lec 1 = introduction to structured query language (sql)
Lec 1 = introduction to structured query language (sql)
 
Introduction to Structured Query Language (SQL) (1).ppt
Introduction to Structured Query Language (SQL) (1).pptIntroduction to Structured Query Language (SQL) (1).ppt
Introduction to Structured Query Language (SQL) (1).ppt
 
Synapseindia dot net development chapter 8 asp dot net
Synapseindia dot net development  chapter 8 asp dot netSynapseindia dot net development  chapter 8 asp dot net
Synapseindia dot net development chapter 8 asp dot net
 
Store programs
Store programsStore programs
Store programs
 
Access2013 ch10
Access2013 ch10Access2013 ch10
Access2013 ch10
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012
 
Dan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New FeaturesDan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New Features
 
Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012
 
chap13.ppt
chap13.pptchap13.ppt
chap13.ppt
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objects
 
Table functions - Planboard Symposium 2013
Table functions - Planboard Symposium 2013Table functions - Planboard Symposium 2013
Table functions - Planboard Symposium 2013
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*Plus
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 

Recently uploaded

EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一F La
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...ThinkInnovation
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 

Recently uploaded (20)

EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
Decoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionDecoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in Action
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 

Using SQL Queries to Insert, Update, Delete, and View Data.ppt

  • 1. Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data Guide to Oracle 10g
  • 2. Guide to Oracle 10g 2 Lesson A Objectives After completing this lesson, you should be able to: • Run a script to create database tables automatically • Insert data into database tables • Create database transactions and commit data to the database • Create search conditions in SQL queries • Update and delete database records and truncate tables
  • 3. Guide to Oracle 10g 3 Lesson A Objectives (continued) • Create and use sequences to generate surrogate key values automatically • Grant and revoke database object privileges
  • 4. Guide to Oracle 10g 4 Using Scripts to Create Database Tables • Script – Text file that contains one or more SQL commands • Run a script – Type start at SQL prompt – Blank space – Full path and filename of script file
  • 5. Guide to Oracle 10g 5 Using the INSERT Command • Basic syntax for inserting into every column: INSERT into tablename VALUES (column1_value, column2_value, … ); • Basic syntax for inserting into selected columns INSERT into tablename (columnname1, columnname2, … ); VALUES (column1_value, column2_value, … );
  • 6. Guide to Oracle 10g 6 Using the INSERT Command (continued) • Ensure all foreign keys that new row references have already been added to database
  • 7. Guide to Oracle 10g 7 Format Models • Also called format mask • Used to specify different output format from default • For NUMBER data types – 9 represents digit • For DATE/TIMESTAMP data types – Choose formats for year day, date, etc.
  • 8. Guide to Oracle 10g 8 Inserting Date and Interval Values • Inserting values into DATE columns – Use TO_DATE function to convert string to DATE – Syntax •TO_DATE('date_string', 'date_format_model') • Inserting values into INTERVAL columns – Syntax •TO_YMINTERVAL('years-months') •TO_DSINTERVAL('days HH:MI:SS.99')
  • 9. Guide to Oracle 10g 9 Inserting LOB Column Locators • Oracle stores LOB data in separate physical location from other types of data • LOB locator – Structure containing information that identifies LOB data type – Points to alternate memory location • Create blob locator – EMPTY_BLOB()
  • 10. Guide to Oracle 10g 10 Creating Transactions and Committing New Data • Transaction – Represents logical unit of work – All of action queries must succeed or no transactions can succeed • Commit – Save changes in transaction • Rollback – Discard changes in transaction
  • 11. Guide to Oracle 10g 11 Creating Transactions and Committing New Data (continued) • Purpose of transaction processing – Enable users to see consistent view of database • New transaction begins when SQL*Plus started and command executed • Transaction ends when current transaction committed • COMMIT command commits transaction • ROLLBACK command restores database to point before last commit
  • 12. Guide to Oracle 10g 12 Creating Search Conditions in SQL Queries • Search condition – Expression that seeks to match specific table rows • Syntax – WHERE columnname comparison_operator search_expression • Comparison operators include: – Equality and inequality operators – Set operators
  • 13. Guide to Oracle 10g 13 Defining Search Expressions • NUMBER example – WHERE f_id = 1 • Character data example – WHERE s_class = 'SR' • DATE example – WHERE s_dob = TO_DATE('01/01/1980', ‘MM/DD/YYYY')
  • 14. Guide to Oracle 10g 14 Creating Complex Search Conditions • Complex search condition – Combines multiple search conditions using logical operators • AND logical operator – True if both conditions true • OR logical operator – True if one condition true • NOT logical operator – Matches opposite of search condition
  • 15. Guide to Oracle 10g 15 Updating Table Rows • UPDATE action query syntax UPDATE tablename SET column1 = new_value1, column2 = new_value2, … WHERE search condition;
  • 16. Guide to Oracle 10g 16 Deleting Table Rows • SQL DELETE action query – Remove specific rows • Truncate table – Remove all rows • DELETE query syntax DELETE FROM tablename WHERE search condition;
  • 17. Guide to Oracle 10g 17 Deleting Table Rows (continued) • Child row – Row’s value is foreign key – Cannot delete row if it has child row • Unless first delete row in which foreign key value exists • TRUNCATE syntax – TRUNCATE TABLE tablename; • Cannot truncate table with foreign key constraints – Must disable constraints first
  • 18. Guide to Oracle 10g 18 Creating New Sequences • CREATE SEQUENCE command – DDL command – No need to issue COMMIT command
  • 19. Guide to Oracle 10g 19 General Syntax Used to Create a New Sequence
  • 20. Guide to Oracle 10g 20 Viewing Sequence Information • Query USER_SEQUENCES data dictionary view – sequence_name column displays sequence names
  • 21. Guide to Oracle 10g 21 Using Sequences • Pseudocolumn – Acts like column in database table – Actually command that returns specific value • CURRVAL – Returns most recent sequence value retrieved • NEXTVAL – Next available sequence value – sequence_name.NEXTVAL
  • 22. Guide to Oracle 10g 22 Using Sequences (continued) • DUAL – Simple table in system user schema – More efficient to retrieve pseudocolumns from DUAL SELECT sequence_name.NEXTVAL FROM DUAL; • DBMS uses user sessions – To ensure that all sequence users receive unique sequence numbers
  • 23. Guide to Oracle 10g 23 Database Object Privileges
  • 24. Guide to Oracle 10g 24 Granting Object Privileges • SQL GRANT command – Syntax GRANT privilege1, privilege2, … ON object_name TO user1, user2, …;
  • 25. Guide to Oracle 10g 25 Revoking Table Privileges • REVOKE command – Syntax REVOKE privilege1, privilege2, … ON object_name FROM user1, user2, …;
  • 26. Guide to Oracle 10g 26 Lesson B Objectives After completing this lesson, you should be able 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
  • 27. Guide to Oracle 10g 27 Retrieving Data From a Single Database Table • Syntax SELECT columnname1, columnname2, … FROM ownername.tablename [WHERE search_condition]; • Retrieve all of columns – Use asterisk ( * ) as wildcard character in SELECT clause – SELECT * from …
  • 28. Guide to Oracle 10g 28 Suppressing Duplicate Rows • SQL DISTINCT qualifier – Examines query output before it appears on screen – Suppresses duplicate values • Syntax – SELECT DISTINCT columnname;
  • 29. Guide to Oracle 10g 29 Using Search Conditions in SELECT Queries • Use search conditions to retrieve rows matching specific criteria – Exact search conditions • Use equality operator – Inexact search conditions • Use inequality operators • Search for NULL or NOT NULL values – WHERE columnname IS NULL – WHERE columnname IS NOT NULL
  • 30. Guide to Oracle 10g 30 Using Search Conditions in SELECT Queries (continued) • IN comparison operator – Match data values that are members of a set of search values • LIKE operator – Use to match part of character string – Syntax •WHERE columnname LIKE 'string' • Character string should contain wildcard character %, or _, or both
  • 31. Guide to Oracle 10g 31 Sorting Query Output • ORDER BY clause – Sort query output – Syntax for select with ordered results SELECT columnname1, columnname2, … FROM ownername.tablename WHERE search_condition ORDER BY sort_key_column; – Sort can be ascending or descending – Can specify multiple sort keys
  • 32. Guide to Oracle 10g 32 Using Calculations in SQL Queries • Perform many calculations directly within SQL queries – Very efficient way to perform calculations • Create SQL queries – Perform basic arithmetic calculations – Use variety of built-in functions
  • 33. Guide to Oracle 10g 33 Performing Arithmetic Calculations • Perform arithmetic calculations on columns that have data types – NUMBER – DATE – INTERVAL • SYSDATE pseudocolumn – Retrieves current system date • Use + and – to calculate differences between dates
  • 34. Guide to Oracle 10g 34 Oracle 10g SQL Functions • Built-in functions perform calculations and manipulate retrieved data values • Called single-row functions – Return single result for each row of data retrieved • To use: – List function name in SELECT clause followed by required parameter in parentheses
  • 35. Guide to Oracle 10g 35 Oracle 10g SQL Group Functions • Group function – Performs operation on group of queried rows – Returns single result such as column sum • To use: – List function name followed by column name in parentheses
  • 36. Guide to Oracle 10g 36 Using the COUNT Group Function • COUNT group function – Returns integer representing number of rows that query returns • COUNT(*) version – Calculates total number of rows in table that satisfy given search condition – Includes NULL values. • The COUNT(columnname) version – Does not include NULL values
  • 37. Guide to Oracle 10g 37 Using the GROUP BY Clause to Group Data • GROUP BY clause – Group output by column with duplicate values – Apply group functions to grouped data • Syntax – GROUP BY group_columnname; – Follows FROM clause • All columns listed in SELECT clause must be included in GROUP BY clause
  • 38. Guide to Oracle 10g 38 Using the HAVING Clause to Filter Grouped Data • HAVING clause – Place search condition on results of queries that display group function calculations • Syntax – HAVING group_function comparison_operator value • Example – HAVING sum(capacity) >= 100
  • 39. Guide to Oracle 10g 39 Creating Alternate Column Headings • Column headings for retrieved columns are names of database table columns • Specify alternate output heading text SELECT columnname1 "heading1_text ", columnname2 "heading2_text", …
  • 40. Guide to Oracle 10g 40 Creating Alternate Column Headings (continued) • Alias – Alternate name for query column – Syntax •SELECT columnname1 AS alias_name1…
  • 41. Guide to Oracle 10g 41 Modifying the SQL*Plus Display Environment • SQL*Plus page consists of: – Specific number of characters per line – Specific number of lines per page • linesize property – Specifies how many characters appear on line • pagesize property – Specifies how many lines appear on page • Modify using environment dialog box
  • 42. Guide to Oracle 10g 42 Formatting Data Using Format Models • TO_CHAR function – Convert column to character string – Apply desired format model to value – Syntax •TO_CHAR(column_name, 'format_model') – Use for data types • DATE • INTERVAL • NUMBER
  • 43. Guide to Oracle 10g 43 Lesson C Objectives After completing this lesson, you should be able to: • Create SQL queries that join multiple tables • Create nested SQL queries • Combine query results using set operators • Create and use database views
  • 44. Guide to Oracle 10g 44 Joining Multiple Tables • Join – Combine data from multiple database tables using foreign key references • Syntax SELECT column1, column2, … FROM table1, table2 WHERE table1.joincolumn = table2.joincolumn AND search_condition(s);
  • 45. Guide to Oracle 10g 45 Joining Multiple Tables (continued) • Must qualify column name in SELECT clause – Specify name of table that contains column followed by period then column name • Join condition – Specifies table names to be joined and column names on which to join tables • SQL supports multiple types of join queries
  • 46. Guide to Oracle 10g 46 Inner Joins • Simplest type of join • VALUES in one table equal to values in other table • Also called: – Equality join – Equijoin – Natural join • Query design diagram
  • 47. Guide to Oracle 10g 47 Deriving a SQL Query From a Query Design Diagram
  • 48. Guide to Oracle 10g 48 Outer Joins • Returns all rows from one table – Called inner table • And matching rows from second table – Called outer table • Syntax – inner_table.join_column = outer_table.join_column(+)
  • 49. Guide to Oracle 10g 49 Self-joins • Query that joins table to itself • Must create table alias – Alternate name assigned to table in query’s FROM clause – Syntax •FROM table1 alias1, …
  • 50. Guide to Oracle 10g 50 Creating Nested Queries • Nested query – Consists of main query and one or more subqueries – Main query • First query that appears in SELECT command – Subquery • Retrieves values that main query’s search condition must match
  • 51. Guide to Oracle 10g 51 Creating Nested Queries with Subqueries that Return a Single Value
  • 52. Guide to Oracle 10g 52 Using Multiple Subqueries Within a Nested Query • Use AND and OR operators – To join search conditions associated with subqueries
  • 53. Guide to Oracle 10g 53 Creating Nested Subqueries • Nested subquery – Subquery that contains second subquery that specifies its search expression
  • 54. Guide to Oracle 10g 54 UNION and UNION ALL • UNION set operator – Joins output of two unrelated queries into single output result – Syntax •query1 UNION query2; • UNION ALL operator – Same as UNION but includes duplicate rows
  • 55. Guide to Oracle 10g 55 INTERSECT • Finds intersection in two queries • Requires that both queries have same number of display columns in SELECT statement • Automatically suppresses duplicate rows
  • 56. Guide to Oracle 10g 56 MINUS • To find difference between two unrelated query result list
  • 57. Guide to Oracle 10g 57 Creating and Using Database Views • Source query – Used to create view – Specify subset of single table’s columns or rows or join multiple tables • Updatable views – Can be used to update database
  • 58. Guide to Oracle 10g 58 Creating Views • Syntax CREATE VIEW view_name AS source_query; – Or CREATE OR REPLACE VIEW view_name AS source_query;
  • 59. Guide to Oracle 10g 59 Executing Action Queries Using Views • Use view to execute action queries that: – Insert – Update – Delete data in underlying source tables • Can also execute update action queries and delete action queries using view – Just as with database table
  • 60. Guide to Oracle 10g 60 Retrieving Rows from Views • Query view using SELECT statement
  • 61. Guide to Oracle 10g 61 Removing Views • DROP VIEW command – Remove view from user schema – Syntax •DROP VIEW view_name;
  • 62. Guide to Oracle 10g 62 Summary • INSERT action query • SQL search condition – Match one or more database rows • UPDATE action query • DELETE command • SELECT query – DISTINCT qualifier • Single row and group functions
  • 63. Guide to Oracle 10g 63 Summary (continued) • Can change appearance of SQL*Plus environment • Join multiple tables in SELECT query – Inner join – Outer join • Nested queries • Set operators • Views