Updating Column Values and Deleting Rows

Objectives\n\n* The following goals are established for the management of table data:\n * Construct and execute an UPDATEUPDATE statement.\n * Construct and execute a DELETEDELETE statement.\n * Construct and execute a query utilizing a subquery to update and delete data within a table.\n * Construct and execute a query involving a correlated subquery for updating and deleting table data.\n * Explain the influence of foreign-key and primary-key integrity constraints on UPDATEUPDATE and DELETEDELETE statements.\n * Explain the specific purpose of the FOR,UPDATEFOR\\,UPDATE clause within a SELECTSELECT statement.\n\n# Purpose of Database Management\n\n* In database environments, change is the only constant. While one might wish for tasks like cleaning or grading to remain permanent, data must be constantly managed.\n* The role of a Database Administrator (DBADBA) involves updating, inserting, deleting, and managing data records. Mastery of these commands allows a user to act as the DBADBA of their own schema to maintain database integrity and relevance.\n\n# The UPDATE Statement\n\n* The UPDATEUPDATE statement is used specifically to modify existing rows within a table.\n* An UPDATEUPDATE statement requires four distinct values to execute correctly:\n * The name of the target table.\n * The name of the column or columns whose values are being modified.\n * A new value for each column being modified.\n * A condition (WHEREWHERE clause) to identify which specific rows in the table should be modified.\n* Syntactic Recommendations: It is recommended that the UPDATEUPDATE statement be placed on its own line for clarity.\n* Example of a basic update: To change the phone number of a specific employee in the copyemployeescopy\\_employees table, the following syntax is used:\n * UPDATE,copyemployees,SET,phone,number,=,123456,WHERE,employeeid,=,303;UPDATE\\,copy\\_employees\\,SET\\,phone\\,number\\,=\\,'123456'\\,WHERE\\,employee\\_id\\,=\\,303;\n* Updating Multiple Columns and Rows: It is possible to modify several columns and several rows simultaneously. In an example where both the phone number and last name are changed for employees with an ID greater than or equal to 303303, the syntax is:\n * UPDATE,copyemployees,SET,phone,number,=,654321,,lastname,=,Jones,WHERE,employeeid,ge,303;UPDATE\\,copy\\_employees\\,SET\\,phone\\,number\\,=\\,'654321',\\,last\\_name\\,=\\,'Jones'\\,WHERE\\,employee\\_id\\,\\ge\\,303;\n* Danger of Omitted Clauses: Extreme care must be taken when updating. If the WHEREWHERE clause is omitted, every single row in the table will be updated with the new values. For instance, omitting the condition in the previous example would result in every employee in the table having the last name Jones'Jones' and the same phone number.\n\n# Updating with Subqueries\n\n* A new value for a column can be derived from the result of a single-row subquery.\n* Subquery Execution Process: The subquery executes first to retrieve a specific value, which is then passed to the outer UPDATEUPDATE statement as the new value for the designated column.\n* Example (Single Column): Changing the salary of employee 101101 to match the salary of employee 100100:\n * UPDATE,copyemployees,SET,salary,=,(SELECT,salary,FROM,copyemployees,WHERE,employeeid,=,100),WHERE,employeeid,=,101;UPDATE\\,copy\\_employees\\,SET\\,salary\\,=\\,(SELECT\\,salary\\,FROM\\,copy\\_employees\\,WHERE\\,employee\\_id\\,=\\,100)\\,WHERE\\,employee\\_id\\,=\\,101;\n* Updating Two Columns with Two Subqueries: Multiple columns can be updated in one statement by writing a separate single-row subquery for each column.\n * Example: Updating the salary and jobidjob\\_id of employee 206206 to match those of employee 205205:\n * UPDATE,copyemployees,SET,salary,=,(SELECT,salary,FROM,copyemployees,WHERE,employeeid,=,205),,jobid,=,(SELECT,jobid,FROM,copyemployees,WHERE,employeeid,=,205),WHERE,employeeid,=,206;UPDATE\\,copy\\_employees\\,SET\\,salary\\,=\\,(SELECT\\,salary\\,FROM\\,copy\\_employees\\,WHERE\\,employee\\_id\\,=\\,205),\\,job\\_id\\,=\\,(SELECT\\,job\\_id\\,FROM\\,copy\\_employees\\,WHERE\\,employee\\_id\\,=\\,205)\\,WHERE\\,employee\\_id\\,=\\,206;\n* Updating Across Different Tables: A subquery can retrieve data from one table to update a different table. For example, retrieving a salary from the original employeesemployees table to update the copyemployeescopy\\_employees table:\n * UPDATE,copyemployees,SET,salary,=,(SELECT,salary,FROM,employees,WHERE,employeeid,=,205),WHERE,employeeid,=,202;UPDATE\\,copy\\_employees\\,SET\\,salary\\,=\\,(SELECT\\,salary\\,FROM\\,employees\\,WHERE\\,employee\\_id\\,=\\,205)\\,WHERE\\,employee\\_id\\,=\\,202;\n\n# Correlated Subquery for Updates\n\n* Subqueries used in updates can be either standalone or correlated. In a correlated subquery, the update of a row in a table is based on a selection from that same table or a related table that references the outer query's row.\n* Implementation Example: Suppose a new column departmentnamedepartment\\_name is added to the copyemployeescopy\\_employees table and needs to be populated with data from the departmentsdepartments table.\n * First, the table is altered: ALTER,TABLE,copyemployees,ADD,(departmentname,varchar2(30),NOT,NULL);ALTER\\,TABLE\\,copy\\_employees\\,ADD\\,(department\\_name\\,varchar2(30)\\,NOT\\,NULL);\n * Then, the correlated subquery updates the name based on the matching departmentiddepartment\\_id: UPDATE,copyemployees,e,SET,e.departmentname,=,(SELECT,d.departmentname,FROM,departments,d,WHERE,e.departmentid,=,d.departmentid);UPDATE\\,copy\\_employees\\,e\\,SET\\,e.department\\_name\\,=\\,(SELECT\\,d.department\\_name\\,FROM\\,departments\\,d\\,WHERE\\,e.department\\_id\\,=\\,d.department\\_id);\n\n# The DELETE Statement\n\n* The DELETEDELETE statement is used to remove existing rows from a table.\n* The statement requires two primary values:\n * The name of the table.\n * A condition identifying which rows are to be removed.\n* Example: To delete the record of an employee with ID 303303: DELETE,FROM,copyemployees,WHERE,employeeid,=,303;DELETE\\,FROM\\,copy\\_employees\\,WHERE\\,employee\\_id\\,=\\,303;\n* Consequences of Omitting WHERE: If the WHEREWHERE clause is omitted in a DELETEDELETE statement, all rows in the table are deleted. No data will remain in the table, though the table structure itself remains.\n\n# Deleting with Subqueries\n\n* Subqueries can be nested within the WHEREWHERE clause of a DELETEDELETE statement to target rows based on complex criteria.\n* Example (Standard Subquery): Deleting all employees who belong to the Shipping'Shipping' department:\n * DELETE,FROM,copyemployees,WHERE,departmentid,=,(SELECT,departmentid,FROM,departments,WHERE,departmentname,=,Shipping);DELETE\\,FROM\\,copy\\_employees\\,WHERE\\,department\\_id\\,=\\,(SELECT\\,department\\_id\\,FROM\\,departments\\,WHERE\\,department\\_name\\,=\\,'Shipping');\n* Example (Correlated Subquery with Aggregation): Deleting employees who work for a manager who manages fewer than 22 employees using the HAVINGHAVING clause:\n * DELETE,FROM,copyemployees,e,WHERE,e.managerid,IN,(SELECT,d.managerid,FROM,employees,d,HAVING,count(d.departmentid),<,2,GROUP,BY,d.managerid);DELETE\\,FROM\\,copy\\_employees\\,e\\,WHERE\\,e.manager\\_id\\,IN\\,(SELECT\\,d.manager\\_id\\,FROM\\,employees\\,d\\,HAVING\\,count(d.department\\_id)\\,<\\,2\\,GROUP\\,BY\\,d.manager\\_id);\n\n# Integrity Constraints in DML\n\n* Definition: Integrity constraints are rules that ensure data quality and conformity. These rules are automatically verified whenever a Data Manipulation Language (DMLDML) statement, such as UPDATEUPDATE or DELETEDELETE, is executed.\n* Behavior: If a statement violates any rule, the table is not updated, the transaction fails, and an error is returned.\n* NOT NULL Constraint Violations: If an UPDATEUPDATE statement attempts to set a column to NULLNULL via a subquery that returns no result, and that column has a NOT,NULLNOT\\,NULL constraint, an error occurs. For example, trying to fetch the last name of a non-existent employee (ID,=,999ID\\,=\\,999) to update employee 101101 will fail because lastnamelast\\_name cannot be null.\n* Primary Key and Foreign Key Constraints:\n * The employeesemployees table has a foreign key on departmentiddepartment\\_id referencing the departmentsdepartments table to ensure employees belong to valid departments.\n * In a scenario where departmentids,10department\\_ids\\,10 and 2020 exist but 1515 does not, attempting to set an employee's department to 1515 will result in a constraint error.\n* Behavior in Copied Tables: When creating a table using the CREATE,TABLE,dots,AS,(SELECT,dots)CREATE\\,TABLE\\,\\dots\\,AS\\,(SELECT\\,\\dots), the system copies the rows and NOT,NULLNOT\\,NULL constraints. However, it does not copy primary key or foreign key constraints. Consequently, updates on these copied tables will not trigger primary/foreign key integrity errors unless those constraints are manually added later.\n\n# Data Locking with the FOR UPDATE Clause\n\n* Standard SELECTSELECT Behavior: By default, a SELECTSELECT statement does not issue locks on the database rows. This minimizes database overhead.\n* Purpose: Sometimes, it is necessary to ensure that records returned by a query cannot be updated or deleted by other users while you are working on them.\n* Mechanism: The FOR,UPDATEFOR\\,UPDATE clause issues exclusive row-level locks on all rows returned by the SELECTSELECT statement. These locks are held until a COMMITCOMMIT or ROLLBACKROLLBACK command is issued.\n* APEX Limitation: The hosted instance of APEXAPEX utilizes autocommit, which means row-level locks requested via FOR,UPDATEFOR\\,UPDATE will not be maintained as the transaction commits immediately.\n* Multiple Table Join Locking: If a FOR,UPDATEFOR\\,UPDATE clause is used in a query involving multiple tables, every row involved in the join from all tables will be locked.\n * Example: SELECT,e.employeeid,,e.salary,,d.departmentname,FROM,employees,e,JOIN,departments,d,USING,(departmentid),WHERE,jobid,=,STCLERK,AND,locationid,=,1500,FOR,UPDATE,ORDER,BY,e.employeeid;SELECT\\,e.employee\\_id,\\,e.salary,\\,d.department\\_name\\,FROM\\,employees\\,e\\,JOIN\\,departments\\,d\\,USING\\,(department\\_id)\\,WHERE\\,job\\_id\\,=\\,'ST\\_CLERK'\\,AND\\,location\\_id\\,=\\,1500\\,FOR\\,UPDATE\\,ORDER\\,BY\\,e.employee\\_id;\n * Result: If this query returns four rows (e.g., IDs 141141, 142142, 143143, and 144144), those specific rows in both the employee and department tables are locked for the current user.\n\n# Questions & Discussion\n\n* Evaluation of Integrity Constraint Scenarios: Consider three distinct statements and whether they return errors based on standard primary/foreign key constraints:\n 1. UPDATE,employees,SET,departmentid,=,15,WHERE,employeeid,=,100;UPDATE\\,employees\\,SET\\,department\\_id\\,=\\,15\\,WHERE\\,employee\\_id\\,=\\,100; (Error: Department 1515 does not exist).\n 2. DELETE,FROM,departments,WHERE,departmentid,=,10;DELETE\\,FROM\\,departments\\,WHERE\\,department\\_id\\,=\\,10; (Potential Error: If employees are currently assigned to department 1010, this violates foreign key integrity).\n 3. UPDATE,employees,SET,departmentid,=,10,WHERE,departmentid,=,20;UPDATE\\,employees\\,SET\\,department\\_id\\,=\\,10\\,WHERE\\,department\\_id\\,=\\,20; (Success: Moves employees from one valid department to another valid department).", "title": "Updating Column Values and Deleting Rows"}