0% found this document useful (0 votes)
13 views

SQL5a_TCL

Uploaded by

lmelody206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

SQL5a_TCL

Uploaded by

lmelody206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

SECD2523 DATABASE

SQL 5a | TCL

www.utm.my
LECTURE LEARNING OUTCOME
By the end of this lecture, students should be able to:

Construct SQL statements for

01 View

02 Transaction

www.utm.my
2
01 View

02 Transaction

www.utm.my
3
Before we continue this TCL,

we reuse the same database same tables created in DML3

If failed to find the database and tables you created previously,


refer here to get our SQL statements to prepare all tables and sample data…
https://docs.google.com/document/d/1jC5daHE4WhP7N9U6ZxqRzBJcJAzqJxf1/
edit?usp=sharing&ouid=112935562328060013817&rtpof=true&sd=true

www.utm.my
4
Here, our tables with records
select * from locations;

www.utm.my
5
Here, our tables with records
select * from departments;

www.utm.my
6
Here,
our tables with records
select * from employees;

www.utm.my
7
Let’s start…

www.utm.my
8
View

www.utm.my
View
• Virtual table
• is defined by using an SQL SELECT statement to one or more base tables
• looks like a regular table with named columns and rows of data
• doesn’t store the data itself in the database

• To create a view:
MySQL:
CREATE VIEW ViewName [(column_list)]
(optional)

www.utm.my
AS subselect
[WITH CHECK OPTION] (optional);

10
View
CREATE VIEW ViewName [(column_list)]
(optional)
AS subselect
[WITH CHECK OPTION] (optional);
• A subselect is known as the defining query to the view
• If provide a list of column names, it must match the number of
columns produced by the subselect
• If you don't specify column names, the view will automatically use the
column names from the subselect
• You must provide column names if there's any confusion or ambiguity,
such as:

www.utm.my
• When the subselect includes calculated columns without names
(e.g., no AS clause was used).
• When a join produces two columns with the same name. 11
SELECT * FROM employees;

we are going to focus on department_id = 60 in the following examples…

www.utm.my
12
Create a horizontal view
• Example 1:
• Create a view so that the head of department_id=60 can see the details only for staff
who work in his or her department.
CREATE VIEW dept60_view To ensure the head of the department sees only specific rows:
AS • do not give them access to the base table employees.
SELECT * FROM employees • instead, give them access to the view dept60_view.
WHERE department_id = 60;

• Once done created,


execute the following statement
SELECT * FROM
dept60_view;

www.utm.my
to get the result view

13
Create a vertical view
• Example 2:
• Create a view of employees' detail at department_id=60 that exclude salary, so that
only the head of the department can access the salary details for employees who work
at their department. To keep salary details private:
• do not give employees at department_id 60 access to the base table
employees or dept60_view.
CREATE VIEW emp_dept60_view • instead, give them access to the view emp_dept60_view which hides salary.
AS
SELECT employee_id, first_name, last_name, email, phone_number,
job_id FROM employees WHERE department_id = 60;
or
CREATE VIEW emp_dept60_view

www.utm.my
AS
SELECT employee_id, first_name, last_name, email, phone_number,
job_id FROM dept60_view; this view is created from the previous slide
14
Create a grouped and joined view
• Example 3:
• Create a view of employees who manage to count of employees working in each
department within a specific location.
CREATE VIEW DepartmentEmployeeCount
AS
SELECT d.department_name, l.city AS location, COUNT(e.employee_id) AS
employee_count
FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN locations l ON d.location_id = l.location_id
GROUP BYuse
Grouped view: d.department_name,
of a subselect l.city;
Joined view: multiple tables

www.utm.my
• Once done created, execute the following statement
SELECT * FROM
DepartmentEmployeeCount;
to get the result view 15
Removing a View (DROP VIEW)
• To remove a view from the database:
MySQL: DROP VIEW ViewName [RESTRICT | CASCADE]
(optional);
• If CASCADE is specified, DROP VIEW will delete the view and all related objects that depend on it. This means
any views that are based on the view you are dropping will also be deleted.
• If RESTRICT is specified, the command will only work if there are no dependent objects. If any object depends
on the view, the command will be rejected.
• Note: The default setting is RESTRICT. employee

• Example 4: EmployeeInfo
EmployeeInfo view select basic employee information from employee table,
EmployeeDetail view select a part information from EmployeeInfo view. EmployeeDetail

DROP VIEW EmployeeInfo CASCADE;

www.utm.my
it will delete EmployeeInfo and also remove EmployeeDetail
since it depends on EmployeeInfo.
DROP VIEW EmployeeInfo RESTRICT;
it will fail and the view won’t be dropped since EmployeeDetail still depends on EmployeeInfo.
16
View - WITH CHECK OPTION
• CREATE VIEW … WITH CHECK OPTION
• prohibits a row from migrating out of the view
• WITH LOCAL CHECK OPTION:
any row inserted or updated in the view (or any view that depends on it) must remain visible in
the view after the operation. If a row is inserted or updated, it cannot be removed from the
view unless it is also removed from the underlying table or view that the original view is based
on.
• WITH CASCADED CHECK OPTION:
default setting and ensures that any row inserted or updated into a view, and
any views directly or indirectly defined on it
(a row must satisfy all conditions in the chain of dependent views, not just
the immediate view where the operation occurs. If any condition fails, the
operation is rejected.)

www.utm.my
• MySQL: CREATE VIEW ViewName [(column_list)] (optional)
AS subselect
[WITH (LOCAL/CASCADED) CHECK OPTION] 17
View - WITH CHECK OPTION
• Example 5:
• Create a view with check option so that the head of department_id=60 can see the details only for staff
who work in his or her department.
CREATE VIEW dept60_vco
AS SELECT * FROM
employees
WHERE department_id = 60
WITH
• Once doneCHECK
created,OPTION;
now attempt to insert new values
(99,'Mary', 'Mohd', 'MARYMD', '590.423.1111’,
'1999-02-28', 'IT_PROG', 5300, null, 103, 60)
INSERT INTO dept60_vco VALUES (99,'Mary', 'Mohd', 'MARYMD',
'590.423.1111','1999-02-28', 'IT_PROG', 5300, null, 103, 60);

www.utm.my
now attempt to update
the salary for employee_id = 105 from 4800 to 5000
UPDATE dept60_vco SET salary = 5000 WHERE employee_id = 18
Transaction – related to topic 7

www.utm.my
19
Transaction
• COMMIT
• ends the transaction successfully
• making the database changes permanent
• A new transaction starts after COMMIT with the next transaction-
initiating statement.
• ROLLBACK
• aborts the transaction
• backing out any changes made by the transaction
• A new transaction starts after ROLLBACK with the next transaction-
initiating
SETstatement.
TRANSACTION [TRANSACTION

www.utm.my
• MySQL: MODE];
START TRANSACTION;
[SQL Statements];
ROLLBACK or ROLLBACK TO …; 20
COMMIT;
COMMIT / ROLLBACK
Example 1:
• Create Table for bank_accounts with account_id, account_name, and
balance, then insert sample data before we proceed applying commit or
rollback
CREATE TABLE bank_accounts (
account_id INT PRIMARY KEY,
account_name VARCHAR(50),
balance DECIMAL(10,2)
);

INSERT INTO bank_accounts VALUES


(70001001, 'Ben Bob', 4020.00),

www.utm.my
(70001020, 'Melvin Alex', 1020.30),
(70301030, 'Diana Ernst', 30303.03); continue to next slide

21
COMMIT / ROLLBACK
Example 1 (continued…):
• Now, transfer 100.00
from account_id = 70001001 to account_id = 70001020

START TRANSACTION;

UPDATE bank_accounts Before Transfer


SET balance = balance - 100.00
WHERE account_id =
70001001;

UPDATE bank_accounts
SET balance = balance +
100.00
WHERE account_id =

www.utm.my
70001020;

SELECT * FROM
bank_accounts; 22
COMMIT / ROLLBACK
Example 2:
• Now, attempt to transfer 300.00
from account_id = 70001001 to account_id = 70301030
but realize a mistake occurred, and want to cancel it.

START TRANSACTION;
Before Attempt to Transfer

UPDATE bank_accounts
SET balance = balance - 300.00
WHERE account_id =
70001001;

UPDATE bank_accounts
SET balance = balance +

www.utm.my
300.00
WHERE account_id =
70301030;

ROLLBACK; 23
SAVEPOINT
Example 3:
• Now, attempt to transfer 500.00
from account_id = 70001001 to account_id = 70001020,
then a fee of 50.00 is attempted to deduct
from account_id = 70001020, but something goes wrong.
START TRANSACTION;
UPDATE bank_accounts
SET balance = balance - 500.00 Before Attempt to Transfer
WHERE account_id = 70001001;
SAVEPOINT after_deduct_bob;
UPDATE bank_accounts
SET balance = balance + 500.00
WHERE account_id = 70001020;
UPDATE bank_accounts

www.utm.my
SET balance = balance - 50.00
WHERE account_id = 70001020;
ROLLBACK TO
after_deduct_bob;
COMMIT; 24
Summary

Command Purpose

COMMIT Saves all changes made during the transaction.

Undoes all changes made during the transaction, reverting to


ROLLBACK
the start.

Creates a checkpoint within the transaction to partially roll


SAVEPOINT

www.utm.my
back changes.

25
Transaction Modes
• READ ONLY
• ensures no changes can be made to the database within that
transaction
• restrict write operations (like INSERT, UPDATE, or DELETE)
• retrieve data only
• READ WRITE
• (default) allows both reading and modifying data
• MySQL:
SET TRANSACTION [READ ONLY | READ
WRITE];
START TRANSACTION;

www.utm.my
[SQL Statements];
ROLLBACK or ROLLBACK TO …;
COMMIT;
26
SET TRANSACTION [READ ONLY | READ WRITE];
Example 4
• attempt to transfer 500.00 out
but the transaction mode sets to READ ONLY

SET TRANSACTION READ ONLY;

START TRANSACTION;

SELECT * FROM bank_accounts;

UPDATE bank_accounts

www.utm.my
SET balance = balance - 500.00
WHERE account_id = 70001001;

COMMIT;
27
SET TRANSACTION [READ ONLY | READ
WRITE];
Example 5
• attempt to transfer 500.00
from account_id = 70001020 Before Attempt to Transfer
to account_id = 70301030
SET TRANSACTION READ WRITE;

START TRANSACTION;

UPDATE bank_accounts
SET balance = balance - 500.00 WHERE account_id =
70001020;

UPDATE bank_accounts

www.utm.my
SET balance = balance + 500.00 WHERE account_id =
70301030;

COMMIT; 28
Transaction Modes
• ISOLATION LEVEL READ UNCOMMITTED
• allows reading uncommitted changes from other transactions
• ISOLATION LEVEL READ COMMITTED
• ensures that only committed changes are visible (no reading uncommitted changes)
• ISOLATION LEVEL REPEATABLE READ
• (default) ensures that data read within a transaction remains consistent, even if
other transactions modify the data
• ISOLATION LEVEL SERIALIZABLE
• ensures full isolation by locking the rows, preventing other transactions from
accessing them until the transaction is complete
• MySQL:

www.utm.my
SET TRANSACTION [ISOLATION LEVEL READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ |
SERIALIZABLE];
START TRANSACTION;
[SQL Statements];
ROLLBACK or ROLLBACK TO …;
COMMIT; 29
SET TRANSACTION [ISOLATION LEVEL READ
UNCOMMITTED | READ COMMITTED | REPEATABLE READ |
SERIALIZABLE];
Example 6
• balance of account_id = 70001001 is temporarily updated to deduct 200
in the database. However, this transaction has not been committed yet,
this change is still uncommitted and might not be finalized.
SET TRANSACTION ISOLATION LEVEL READ
UNCOMMITTED;

START TRANSACTION;

UPDATE bank_accounts Before transaction


SET balance = balance - 200.00
WHERE account_id = 70001001;

www.utm.my
ROLLBACK;

SELECT * FROM bank_accounts;

30
SET TRANSACTION [ISOLATION LEVEL READ
UNCOMMITTED | READ COMMITTED | REPEATABLE READ |
SERIALIZABLE];
Example 7
• Transaction 1: balance of account_id = 70001001
is updated to deduct 200 in the database and commit changes.
• Transaction 2: can see the committed changes from Transaction 1.
SET TRANSACTION ISOLATION LEVEL READ
COMMITTED; Before transaction
START TRANSACTION;
UPDATE bank_accounts
SET balance = balance - 200.00
WHERE account_id = 70001001;
COMMIT;

SET TRANSACTION ISOLATION LEVEL READ

www.utm.my
COMMITTED;
START TRANSACTION;
SELECT * FROM bank_accounts;
COMMIT;
31
SET TRANSACTION [ISOLATION LEVEL READ
UNCOMMITTED | READ COMMITTED | REPEATABLE READ |
SERIALIZABLE];
Example 8
• Transaction 1: view balance of account_id = 70001001 and commit under
REPEATABLE READ.
• Transaction 2: update balance of account_id = 70001001 to
deduct 200 in the database.
SET TRANSACTION ISOLATION LEVEL REPEATABLE
READ;
START TRANSACTION;
SELECT * FROM bank_accounts WHERE account_id =
70001001;
COMMIT;
Before transaction
SET TRANSACTION ISOLATION LEVEL READ

www.utm.my
COMMITTED;
START TRANSACTION;
UPDATE bank_accounts
SET balance = balance - 200.00
WHERE account_id = 70001001; 32
COMMIT;
SET TRANSACTION [ISOLATION LEVEL READ
UNCOMMITTED | READ COMMITTED | REPEATABLE READ |
SERIALIZABLE];
Example 9
• Transaction 1: view balance of account_id = 70001001 and commit under
SERIALIZABLE.
• Transaction 2: update balance of account_id = 70001001 to
deduct 500 in the database.
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SELECT * FROM bank_accounts WHERE account_id =
70001001;
COMMIT;

SET TRANSACTION ISOLATION LEVELBeforeREAD


transaction
COMMITTED;

www.utm.my
START TRANSACTION;
UPDATE bank_accounts
SET balance = balance - 500.00
WHERE account_id = 70001001;
COMMIT; 33
Summary
Mode What It Does
READ ONLY Ensures no data can be modified during the transaction.
READ WRITE Allows both reading and modifying data during the transaction.
READ UNCOMMITTED Allows reading uncommitted changes from other transactions (dirty reads).
Prevents dirty reads, but data can still change between reads (non-repeatable reads
READ COMMITTED possible).
Ensures consistent data for the duration of the transaction (no dirty or non-
REPEATABLE READ repeatable reads).

www.utm.my
Ensures complete isolation by locking rows, preventing phantom reads or any
SERIALIZABLE concurrent access.

34
35

You might also like