You are on page 1of 50

INFORMATION MANAGEMENT

MODULE 7: Data Manipulation Language (DML)


and Transaction Control
MODULE 7 SUBTOPIC 1

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

Insert new row


into the
DEPARTMENTS table.
• Add new rows to a table by using the INSERT statement:
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);

• With this syntax, only one row is inserted at a time.


Adds one or more rows to a table
Inserting into a table

Inserting a record that has some null attributes requires identifying the
fields that actually get data

Inserting from another table


Introduced with SQL:2008

Inserting into a table does not require explicit customer ID entry or field list

INSERT INTO CUSTOMER_T VALUES ( ‘Contemporary Casuals’, ‘1355 S. Himes


Blvd.’, ‘Gainesville’, ‘FL’, 32601);
•Use & substitution in a SQL statement to prompt for values.
•& is a placeholder for the variable value.
INSERT INTO departments
(department_id, department_name, location_id)
VALUES (&department_id, '&department_name',&location);
EMPLOYEES

Update rows in the EMPLOYEES table:


•Modify existing values in a table with the UPDATE
statement: UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];

•Update more than one row at a time (if required).


Modifies data in existing rows
DEPARTMENTS

Delete a row from the DEPARTMENTS table:


You can remove existing rows from a table by using the DELETE
statement:
DELETE [FROM] table
[WHERE condition];
Removes rows from a table
Delete certain rows
• DELETE FROM CUSTOMER_T WHERE CUSTOMERSTATE = ‘HI’;
Delete all rows
• DELETE FROM CUSTOMER_T;
Makes it easier to update a table…allows combination of Insert and
Update in one statement

Useful for updating master tables with new data


END OF SUBTOPIC 1
MODULE 7 SUBTOPIC 2

Transaction Control Commands

■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;

INSERT INTO departments


VALUES (290, 'Corporate Tax', NULL, 1700);

• Commit the changes:


COMMIT;
Discard all pending changes by using the ROLLBACK statement:
• Data changes are undone.
• Previous state of the data is restored.
• Locks on the affected rows are released.

DELETE FROM copy_emp;


ROLLBACK ;
DELETE FROM test;
25,000 rows deleted.

ROLLBACK;
Rollback complete.

DELETE FROM test WHERE id = 100;


1 row deleted.

SELECT * FROM test WHERE id = 100;


No rows selected.

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

UPDATE employees Data


SET salary = 7000 blocks
WHERE last_name = 'Grant';

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.

SELECT employee_id, salary, commission_pct, job_id


FROM employees
WHERE job_id = 'SA_REP'
FOR UPDATE
ORDER BY employee_id;

• Lock is released only when you issue a ROLLBACK or a COMMIT.


• If the SELECT statement attempts to lock a row that is locked by
another user, then the database waits until the row is available, and
then returns the results of the SELECT statement.
• You can use the FOR UPDATE clause in a SELECT statement against multiple
tables.
SELECT e.employee_id, e.salary, e.commission_pct
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;

• 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

Table 6-3: Comparison Operators in SQL


Alias is an alternative column or table name
SELECT CUST.CUSTOMERNAME AS NAME,
CUST.CUSTOMERADDRESS
FROM CUSTOMER_V CUST
WHERE NAME = ‘Home Furnishings’;
•Using the COUNT aggregate function to find totals

SELECT COUNT(*) FROM ORDERLINE_T


WHERE ORDERID = 1004;

Note: With aggregate functions you can’t have single-valued


columns included in the SELECT clause, unless they are included in
the GROUP BY clause.
AND, OR, and NOT Operators for customizing conditions in WHERE
clause

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

With parentheses, you can override normal precedence rules. In


this case parentheses make the OR take place before the AND.
Figure 6-9 Boolean query B with use of parentheses
Sort the results first by STATE, and within a state by the CUSTOMER NAME

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

Like a WHERE clause, but it operates on groups (categories), not on


individual rows. Here, only those groups with total numbers greater than 1
will be included in final result.

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:

CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view


[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]];

• The subquery can contain complex SELECT syntax.

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

You might also like