Updating Column Values and Deleting Rows. Copyright 2008, Oracle. All rights reserved.

Similar documents
Database Programming with SQL

ÇALIŞMA TEST SORULARI

Database Programming - Section 7. Instructor Guide

Creating Other Schema Objects

DUE: CD_NUMBER TITLE PRODUCER YEAR 97 Celebrate the Day R & B Inc Holiday Tunes for All Tunes are US 2004

EXISTS NOT EXISTS WITH

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

Join, Sub queries and set operators

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Creating Other Schema Objects. Copyright 2004, Oracle. All rights reserved.

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Retrieving Data from Multiple Tables

KORA. RDBMS Concepts II

Database Programming with SQL

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

Database Programming - Section 11. Instructor Guide

HR Database. Sample Output from TechWriter 2007 for Databases

Intermediate SQL: Aggregated Data, Joins and Set Operators

Assignment Grading Rubric

Introduction to Oracle9i: SQL

Database Programming with SQL

Data Manipulation Language

CS 275 Winter 2011 Problem Set 3

1Z0-007 ineroduction to oracle9l:sql

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

Introduction to Relational Database Concepts. Copyright 2011, Oracle. All rights reserved.

CBO SQL TRANSFORMER Document describes a few examples of transformations made by CBO.

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

School of Computing and Information Technology Session: Spring CSCI835 Database Systems (Bridging Subject) Sample class test 23 July 2018

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Database Programming with SQL

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

School of Computing and Information Technology. Examination Paper Autumn Session 2017

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Additional Practice Solutions

RETRIEVING DATA USING THE SQL SELECT STATEMENT

DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

King Fahd University of Petroleum and Minerals

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Database Programming - Section 3. Instructor Guide

Oracle Database SQL Basics

Three types of sub queries are supported in SQL are Scalar, Row and Table sub queries.

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Database Programming - Section 10. Instructor Guide

Database Programming with PL/SQL

Tables From Existing Tables

Database Design. 9-2 Basic Mapping: The Transformation Process. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Database Programming with PL/SQL

Misc. Triggers Views Roles Sequences - Synonyms. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering

Basic Mapping: The Transformation Process. Copyright 2011, Oracle. All rights reserved.

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Introduction to Explicit Cursors. Copyright 2008, Oracle. All rights reserved.

Oracle Database 10g Express

The Relational Model

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

Oracle MOOC: SQL Fundamentals

Série n 6 Bis : Ateliers SQL Data Modeler (Oracle)

Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Database Programming with PL/SQL

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

BraindumpsVCE. Best vce braindumps-exam vce pdf free download

Outer Join, More on SQL Constraints

Database Programming with PL/SQL

Database Management Systems,

Exam : 1Z Title : Introduction to Oracle9i: SQL

Databases. Purpose Of A Database. To store information

Database Programming with SQL

Objectives. After completing this lesson, you should be able to do the following:

Monitoring and Resolving Lock Conflicts. Copyright 2004, Oracle. All rights reserved.

Using the Set Operators. Copyright 2004, Oracle. All rights reserved.

Database Programming with SQL

Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

Graphical Joins in More Detail

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

Oracle Database 11g: SQL Fundamentals I

Database Design. 2-3 Entity Relationship Modeling and ERDs. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

CS425 Fall 2016 Boris Glavic Chapter 2: Intro to Relational Model

SYSTEM CODE COURSE NAME DESCRIPTION SEM

Entity-Relationship Modelling. Entities Attributes Relationships Mapping Cardinality Keys Reduction of an E-R Diagram to Tables

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

Database Programming with SQL

Exam Code: 1z Exam Name: Ineroduction to oracle9l:sql. Vendor: Oracle. Version: DEMO

MIS2502: Data Analytics Relational Data Modeling. Jing Gong

Chapter 6: Entity-Relationship Model. The Next Step: Designing DB Schema. Identifying Entities and their Attributes. The E-R Model.

Exam: 1Z Title : Introduction to Oracle9i: SQL. Ver :

Fravo.com. Certification Made Easy. World No1 Cert Guides. Introduction to Oracle9i: SQL Exam 1Z Edition 1.0

CS Reading Packet: "Sub-selects, concatenating columns, and projecting literals"

Basant Group of Institution

The Next Step: Designing DB Schema. Chapter 6: Entity-Relationship Model. The E-R Model. Identifying Entities and their Attributes.

Transcription:

Updating Column Values and Deleting Rows

What Will I Learn? In this lesson, you will learn to: Construct and execute an UPDATE statement Construct and execute a DELETE statement Construct and execute a query that uses a subquery to update and delete data from a table Construct and execute a query that performs updates or deletes based on correlated subqueries Explain how foreign-key and primary-key integrity constraints affect UPDATE and DELETE statements 2

3 Why Learn It? Wouldn't it be a wonderful world if, once you got something done, it never needed to be changed or redone? Your bed would stay made, your clothes would stay clean, and you'd always be getting passing grades. Unfortunately, in databases, as in life, "There is nothing permanent except change." Updating, inserting, deleting, and managing data is a Database Administrator s (DBA s) job. In this lesson, you will become the DBA of your own schema and learn to manage your database.

4 UPDATE The UPDATE statement is used to modify existing rows in a table. It requires four values: the name of the table the name of the column(s) whose values will be modified a new value for each of the column(s) being modified a condition that identifies which rows in the table will be modified. The new value for a column can be the result of a single-row subquery.

5 UPDATE The example shown uses an UPDATE statement to change the phone number of one customer in the Global Fast Foods database. Note that the copy_f_customers table is used in this transaction. UPDATE copy_f_customers SET phone_number='4475582344' WHERE id=123; ID FIRST_NAME LAST_NAME ADDRESS CITY STATE ZIP PHONE_NO 123 Cole Bee 123 Main Street Orlando FL 32838 4475582344 456 Zoe Twee 1009 Oliver Boston MA 12889 7098675309 Avenue 145 Katie Hernandez 92 Chico Way Los Angeles CA 98008 8586667641

6 UPDATE We can change several columns and/or several rows in one UPDATE statement. This example changes both the phone number and the city for two Global Fast Foods customers. UPDATE copy_f_customers SET phone_number='4475582344, city = Chicago WHERE id < 200; ID FIRST_NAME LAST_NAME ADDRESS CITY STATE ZIP PHONE_NO 123 Cole Bee 123 Main Street Chicago FL 32838 4475582344 456 Zoe Twee 1009 Oliver Avenue Boston MA 12889 7098675309 145 Katie Hernandez 92 Chico Way Chicago CA 98008 4475582344

UPDATE Which rows would be updated in the following transaction? UPDATE copy_f_customers SET phone_number='9876543210'; ID FIRST_ LAST_ ADDRESS CITY STATE ZIP PHONE_NO NAME NAME 123 Cole Bee 123 Main Street Orlan do FL 32838 4475582344 456 Zoe Twee 1009 Oliver Avenue 145 Katie Hernandez 92 Chico Way Bosto n MA 12889 7098675309 Los CA 98008 8586667641 Angel e 7

8 Updating a Column with a value from a Subquery We can use the result of a single-row subquery to provide the new value for an updated column. UPDATE copy_f_staffs SET salary = (SELECT salary FROM copy_f_staffs WHERE id = 9) WHERE id = 12; This example changes the salary of one employee (id = 12) to the same salary as another employee (id = 9). As usual, the subquery executes first and retrieves the salary for employee id=12. This salary value is then used to update the salary for employee id=9. ID FIRST_NAME LAST_NAME SALARY 9 Bob Miller 10 12 Sue Doe 10

9 Updating Two Columns with Two Subquery Statements To update several columns in one UPDATE statement, it is possible to write multiple single-row subqueries, one for each column. In this example the UPDATE statement changes the salary and staff type of one employee (id = 12) to the same values as another employee (id = 9). UPDATE copy_f_staffs SET salary = (SELECT salary FROM copy_f_staffs WHERE id = 9), staff_type = (SELECT staff_type FROM copy_f_staffs WHERE id = 9) WHERE id = 12; ID FIRST_NAME LAST_NAME SALARY STAFF_TYPE 9 Bob Miller 10 Cook 12 Sue Doe 10 Cook

10 UPDATING ROWS BASED ON ANOTHER TABLE As you may have expected, the subquery can retrieve information from one table which is then used to update another table. In this example, a copy of the f_staffs table was created. Then data from the original f_staffs table was retrieved, copied, and used to populate the copy of the f_staffs table. UPDATE copy_f_staffs SET salary = (SELECT salary FROM f_staffs WHERE id = 9) WHERE id = 9;

11 UPDATING ROWS BASED ON THE SAME TABLE As you already know subqueries can be either stand alone or correlated. In a correlated subquery you are updating a row in a table based on a select from that same table. In the example below, a copy of the department name column was created in the employees table. Then data from the original departments table was retrieved, copied and used to populate the copy of the column in the employees table. UPDATE employees e SET e.department_name = (SELECT d.department_name FROM departments d WHERE e.department_id = d.department_id);

DELETE The DELETE statement is used to remove existing rows in a table. The statement requires two values: the name of the table the condition that identifies the rows to be deleted 12

DELETE The example shown uses the Global Fast Foods database to delete one row, the customer with ID number 123. DELETE FROM copy_f_customers WHERE ID= 123; What do you predict will be deleted if the WHERE clause is eliminated in a DELETE statement? All rows in the table are deleted if you omit the WHERE clause. 13

14 SUBQUERY DELETE Subqueries can also be used in DELETE statements. The example shown deletes rows from the employees table for all employees who work in the Shipping department. Maybe this department has been renamed or eliminated. DELETE FROM employees WHERE department_id = (SELECT department_id FROM departments WHERE department_name = Shipping ');

15 CORRELATED SUBQUERY DELETE The example below deletes rows of all employees who work for a manager that manages more than 2 departments. DELETE FROM employees e WHERE e.manager_id = (SELECT d.manager_id FROM departments d WHERE e.department_id = d.department_id HAVING count (d.department_id) > 2 GROUP BY d.manager_id);

16 INTEGRITY CONSTRAINT ERRORS Integrity constraints ensure that the data conforms to a needed set of rules. The constraints are automatically checked whenever a DML statement which could break the rules is executed. If the any rule would be broken, the table is not updated and an error is returned. This example violates a NOT NULL constraint, because first_name has a not null constraint and id=123 does not exist, so the subquery returns a null result. UPDATE copy_f_staffs SET first_name = (SELECT first_name FROM copy_f_staffs WHERE id = 123); ORA-01407: cannot update ( USQA_JOHN_SQL01_S01. COPY_F_STAFFS. FIRST_NAME ) to NULL

17 INTEGRITY CONSTRAINT ERRORS When will primary key - foreign key constraints be checked? The EMPLOYEES table has a foreign key constraint on department_id, which references the department_id of the DEPARTMENTS table. This ensures that every employee belongs to a valid department. In the DEPARTMENTS table, department_ids 10 and 20 exist but 15 does not. Which of the following statements will return an error? 1. UPDATE employees SET department_id = 15 WHERE employee_id = 100; 2. DELETE FROM departments WHERE department_id = 10; 3. UPDATE employees SET department_id = 10 WHERE department_id = 20;

18 INTEGRITY CONSTRAINT ERRORS When modifying your copy tables (for example copy_f_customers) you may see not null constraint errors, but you will not see any primary key foreign key constraint errors. This is because the CREATE TABLE. AS (SELECT ) statement used to create the copy tables copies the rows to the copy table and copies the not null constraints, but does not copy primary key foreign key constraints. Therefore at present there are no primary key foreign key constraints on the copy tables. Later in the course you will learn how to add these constraints.

Terminology Key terms used in this lesson include: DELETE Integrity constraint UPDATE Correlated subquery UPDATE Correlated subquery DELETE 19

Summary In this lesson you have learned to: Construct and execute an UPDATE statement Construct and execute a DELETE statement Construct and execute a query that uses a subquery to update and delete data from a table Construct and execute a query that performs updates or deletes based on correlated subqueries Explain how foreign-key and primary-key integrity constraints affect UPDATE and DELETE statements 20

21 Summary Practice Guide The link for the lesson practice guide can be found in the course outline.