4. Inserting, Updating, and Deleting
Inserting a New Record
We want to insert a new record into a table.
Inserting Default Values
A table can be defined to take default values for specific columns. You can insert a row of default values without specifying those values. We can the use of the DEFAULT keyword as a way of explicitly specifying the default value for a column.
Overriding a Default Value with NULL
We want to override the default value by setting the column to NULL.
Copying Rows from One Table into Another
We want to copy rows from one table to another by using a query. For example, we want to copy rows from the DEPT table to the DEPT_EAST table. The DEPT_EAST table has already been created with the same structure (same columns and data types) as DEPT and is currently empty.
Copying a Table Definition
We want to create a new table having the same set of columns definition as an existing table. For example, we want to create a copy of the DEPT table and call it DEPT_2.
Inserting into Multiple Tables at Once
We want to take rows returned by a query and insert those rows into multiple target tables. For example, we want to insert rows from DEPT into tables DEPT_EAST, DEPT_WEST, and DEPT_MID. All three tables have the same structure (same columns and data types) as DEPT and are currently empty.
Blocking Inserts to Certain Columns
We want to prevent users from inserting values into certain table columns. It can be achieve by using View on the table exposing only those columns we want to expose.
Modifying Records in a Table
We want to modify values for some or all rows in a table. It can be achieve using Update.
Updating When Corresponding Rows Exist
We want to update rows in one table when corresponding rows exist in another table.
Updating with Values from Another Table
We want to update rows in one table using values from another. One of the most common and straightforward methods is to use a correlated subquery within the UPDATE
statement. We can also use a MERGE
statement, which is particularly useful for complex operations that involve both inserting and updating rows.
Merging Records
We want to conditionally insert, update, or delete records in a table depending on whether corresponding records exist. If a record exists, then update; if not, then insert; if after updating a row fails to meet a certain condition, delete it.
Deleting All Records from a Table
We want to delete all the records from a table.
Deleting Specific Records
We want to delete records meeting a specific criterion from a table.
Deleting a Single Record
We want to delete a single record from a table.
Deleting Referential Integrity Violations
We want to delete records from a table when those records refer to nonexistent records in some other table. For example, some employees are assigned to departments that do not exist. You want to delete those employees.
Deleting Duplicate Records
We want to delete duplicate records from a table.
Deleting Records Referenced from Another Table
We want to delete records from one table when those records are referenced from some other table. Consider the emp and dept_accidents table, we want to delete from EMP the records for those employees working at a department that has three or more accidents.
Last updated
Was this helpful?