Professional Documents
Culture Documents
Overview of DML
■At the end of the chapter, the learner should be able to:
• Define terms
• Describe each data manipulation language (DML) statement
• Insert, update, and delete data
• A DML statement is executed when you:
• Add new rows to a table
• Modify existing rows in a table
• Remove existing rows from a table
• A transaction consists of a collection of DML statements that form a
logical unit of work.
Database Systems 1
New
DEPARTMENTS row
Inserting a record that has some null attributes requires identifying the
fields that actually get data
Inserting into a table does not require explicit customer ID entry or field list
■At the end of the chapter, the learner should be able to:
• Write single table queries using SQL SELECT command
• Understand the function of transaction control statements
A database transaction consists of one of the
following:
• DML statements that constitute one consistent change to
the data
• One DDL statement
• One data control language (DCL) statement
Time COMMIT
Transaction
DELETE
SAVEPOINT A
INSERT
UPDATE
SAVEPOINT B
INSERT
ROLLBACK ROLLBACK ROLLBACK
to SAVEPOINT B to SAVEPOINT A
• Create a marker in the current transaction by using the SAVEPOINT
statement.
• Roll back to that marker by using the ROLLBACK TO SAVEPOINT
statement.
UPDATE...
SAVEPOINT update_done;
INSERT...
ROLLBACK TO update_done;
• Make the changes:
DELETE FROM employees
WHERE employee_id = 99999;
ROLLBACK;
Rollback complete.
COMMIT;
Commit complete.
• Read consistency guarantees a consistent view of the data at all times.
• Changes made by one user do not conflict with the changes made by
another user.
• Read consistency ensures that, on the same data:
✓Readers do not wait for writers
✓Writers do not wait for readers
✓Writers wait for writers
User A
Undo
segments
Changed
SELECT * and unchanged
FROM userA.employees; Read- data
consistent
image Before
change
User B (“old” data)
• Locks the rows in the EMPLOYEES table where job_id is SA_REP.
• Rows from both the EMPLOYEES and DEPARTMENTS tables are locked.
• Use FOR UPDATE OF column_name to qualify the column you intend to
change, then only the rows from that specific table are locked.
Projection Selection
Table 1 Table 1
Join
Table 1 Table 2
Used for queries on single or multiple tables
Clauses of the SELECT statement:
• SELECT
• List the columns (and expressions) to be returned from the query
• FROM
• Indicate the table(s) or view(s) from which data will be obtained
• WHERE
• Indicate the conditions under which a row will be included in the result
• GROUP BY
• Indicate categorization of results
• HAVING
• Indicate the conditions under which a category (group) will be included
• ORDER BY
• Sorts the result according to specified criteria
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
Find products with standard price less than $275
Note: The LIKE operator allows you to compare strings using wildcards. For
example, the % wildcard in ‘%Desk’ indicates that all strings that have any
number of characters preceding the word “Desk” will be allowed.
Figure 6-8 Boolean query A without use of parentheses
By default,
processing order of
Boolean operators is
NOT, then AND, then
OR
With parentheses…these override the normal precedence of Boolean
operators
Note: The IN operator in this example allows you to include rows whose
CustomerState value is either FL, TX, CA, or HI. It is more efficient
than separate OR conditions.
Database Systems 1
For use with aggregate functions
• Scalar aggregate: single value returned from SQL query with aggregate function
• Vector aggregate: multiple values returned from SQL query with aggregate function (via GROUP BY)
• You can use single-value fields with aggregate functions if they are included in the GROUP BY clause
Database Systems 1
For use with GROUP BY
Database Systems 1
EMPLOYEES table
Feature Simple Views Complex Views
Number of tables One One or more
Contain functions No Yes
Contain groups of No Yes
data
DML operations Yes Not always
through a view
Views provide users controlled access to tables
Base Table–table containing the raw data
Dynamic View
• A “virtual table” created dynamically upon request by a user
• No data actually stored; instead data from base table made available to user
• Based on SQL SELECT statement on base tables or other views
Materialized View
• Copy or replication of data
• Data actually stored
• Must be refreshed periodically to match corresponding base tables
Database Systems 1
• You embed a subquery in the CREATE VIEW statement:
Database Systems 1
• View has a name.
• View is based on a SELECT statement.
• CHECK_OPTION works only for updateable views and
prevents updates that would create rows not included in the
view.
• Simplify query commands
• Assist with data security (but don't rely on views for security, there are
more important security measures)
• Enhance programming productivity
• Contain most current base table data
• Use little storage space
• Provide customized view for user
• Establish physical data independence
Database Systems 1
• Use processing time each time view is referenced
• May or may not be directly updateable
Database Systems 1
In this lesson, you should have learned how to use the following
statements:
Function Description
INSERT Adds a new row to the table
UPDATE Modifies existing rows in the table
DELETE Removes existing rows from the table
MERGE Combines of Insert and Update in one statement
COMMIT Makes all pending changes permanent
SAVEPOINT Is used to roll back to the savepoint marker
ROLLBACK Discards all pending data changes
FOR UPDATE clause Locks rows identified by the SELECT query
in SELECT
END OF MODULE 7
• Taylor, A. G. (2019). SQL for dummies (9th ed.). Hoboken, NJ: For
Dummies.
• Harrington, J. (2016). Relational Database Design and Implementation
(4th Edition). Morgan Kaufmann
• Juric, N., Vrbsky, S., Nestorov, S. (2016). Database Systems: Introduction
to Databases and Data Warehouses. Prospect Press
• Kroenke, D. M., & Auer, D. J. (2016). Database Concepts. Pearson.
• Sullivan, D. (2015). NoSQL for Mere Mortals (1st ed.). Boston: Addison-
Wesley.
• Hoffer, J., Ramesh, V., Topi, H. (2013). Modern Database Management 11th
Edition, Prentice Hall.
Introduction to Programming