Oracle 12c SQL: Data Manipulation and Transaction Control Study Guide
Chapter 5: Data Manipulation and Transaction Control Learning Objectives
• Use the command to add a record to an existing table. • Manage virtual columns in data manipulations. • Use quotes in data values appropriately. • Use a subquery to copy records from an existing table. • Use the command to modify the existing rows of a table. • Use substitution variables with an command. • Delete records using the command. • Manage transactions with transaction control commands: , , and . • Differentiate between a shared lock and an exclusive lock. • Use the command to create a shared lock.
The INSERT Command
• Definition: The command is a Data Manipulation Language () command used to add rows to existing tables. • Usage Constraints: • It can only add one row at a time to a table. • Non-numeric data must be enclosed in single quotes (' '). • If a column list is not provided in the clause, a value must be assigned to every column in the table in the exact order they were defined. • Command Clauses: • : Identifies the target table. • : Specifies the data to be added. • Example 1 (No Column List): • Statement: • Result: rows inserted. • Example 2 (With Column List): • Statement: • Note: This allows skipping columns or providing them in a different order than table definition.
Handling Special Data Entry Scenarios
• Inserting NULL Values: There are three methods to input a value: • Omit the column name from the clause column list. • Use the keyword in the clause. • Substitute two single quotation marks (' ') with no space between them. • Oracle 12c ON NULL Clause: This is an option used with a setting to ensure a default value is used when an attempt is made to insert a value. • Virtual Columns: operations are disallowed on virtual columns because the database system generates these values automatically. Attempting to insert into a virtual column (e.g., ) results in . • DEFAULT Option: To use a column's defined value: • Include a column list in the statement but ignore the specific column. • Explicitly use the keyword as the value for the column in the clause. • Quotes in Data: When a data value contains a literal single quote, two single quotes together must be used to represent it in the statement.
Advanced Insertion and Subqueries
• Copying Records: Records can be copied from an existing table and entered into another existing table by substituting a subquery for the clause. • Syntax: . • Rule: When using a subquery, the clause must be omitted as it is irrelevant.
The UPDATE Command
• Function: The command is used to modify existing rows in a table. It can be used to add values to an existing row (replacing values) or change existing values. • Syntax: • ; • Components: • : Identifies the table. • : Identifies the specific column(s) being changed and their new values. • (Optional): Specifies which row(s) to change. Warning: If the clause is omitted, all rows in the table will be updated. • Example: • Statement: ; • Result: rows updated.
Substitution Variables
• Definition: Substitution variables allow for the creation of interactive scripts by prompting the user for a value during execution. • Syntax: Identified by an ampersand (&$) preceding the variable name. • Example: • Statement: UPDATE customers SET region = '&Region' WHERE state = '&State'; • Interaction: The system will open a prompt asking for the value of REGIONWSTATEDELETE command removes specific rows from a table. • Execution: • The WHERE clause determines which row(s) are removed. • Omitting the WHEREDELETE FROM acctmanager2;DMLDMLCOMMIT occurs. • Key Commands: • COMMIT: Permanently updates the table and allows other users to see the changes. • Explicit: Execution of the COMMIT; command. • Implicit: Occurs when a Data Definition Language (DDL) command is executed or the user properly exits the system. • ROLLBACKROLLBACK; or if the system restarts after a crash. • SAVEPOINT: Marks a specific spot within a transaction. Allows the user to perform a partial rollback to that specific point. • Transaction Example: 1. UPDATE acctmanager SET region = 'E' WHERE amid = 'M500'; 2. COMMIT; (Permanent save). 3. UPDATE acctmanager SET region = 'E' WHERE amid = 'T500'; 4. UPDATE acctmanager SET region = 'E' WHERE amid = 'L500'; 5. SAVEPOINT ONE; (Marker created). 6. UPDATE acctmanager SET amcomm = 6600 WHERE amid = 'T500'; 7. ROLLBACK TO ONE; (Undo only the specific commission update after the savepoint; previous updates remain pending).
Table Locks
• Purpose: To prevent multiple users from chang
ing the same data or objects simultaneously, ensuring data consistency. • Lock Types: • Shared Lock: • Prevents DML operations on the specific portion of the table locked. • Implicitly occurs during UPDATEDELETE operations. • Explicitly set via LOCK TABLE tablename IN SHARE MODE; • Allows other users to place shared locks on other portions but prevents exclusive locks. • Released upon COMMITROLLBACK. • Exclusive Lock: • The most restrictive lock. • Prevents other users from placing any shared or exclusive locks on the table. • Implicitly occurs during DDLCREATEALTER TABLE). • Explicitly set via LOCK TABLE tablename IN EXCLUSIVE MODE; • Released after DDL execution or user exit. • SELECT…FOR UPDATE: • Creates a shared lock on the retrieved portion of the table. • Prevents other users from changing a row while a user is selecting rows intended for change. • Syntax: SELECT columnnames FROM tablename [WHERE condition] FOR UPDATE; • Release: Lock is not released until a transaction control statement is issued, a DDLEXIT$$.