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

Advanced Database Systems - Lab Session

Uploaded by

Gemeda Abugare
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Advanced Database Systems - Lab Session

Uploaded by

Gemeda Abugare
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Advanced Database Systems

(ITec2072)

Lab Session

Compiled by: Bekele A.


Outline
 Transaction Control Language (TCL)
 Data Control Language (DCL)
 Database Objects
 Views
 Stored Procedures
 Triggers
 Indexes

2
Transaction Control Language (TCL)
 TCL statements are used to manage the changes made by
DML statements.
 It allows statements to be grouped together into logical
transactions.
 TCL commands
 COMMIT
 ROLLBACK
 SAVE TRANSACTION
 SET TRANSACTION

 COMMIT: used to save changes made by DML to database


 Syntax
 Begin Transaction
 insert1
 Insert2
 Update1
 COMMIT

3
Transaction Control Language (TCL)…
 ROLLBACK:
 Used to cancel transactions which are not saved.
 Undo the changes till last commit
 Note: before commit only you have to do rollback, once you commit
you cannot rollback.
 Syntax is in same way as commit
 SAVE TRANSACTION
 it will create system change number SCN for your transaction and for that
transaction you can rollback
 Example:
 Begin Transaction
 insert1
 Insert2
 Save Transaction ST_name;
 insert3
 update1
 rollback Transaction ST_name; //rollback insert3 and update1

4
Transaction Control Language (TCL)…
 SET TRANSACTION:
 Always it should be at beginning of transaction
 SET TRASACTION READ WRITE; //DEFAULT
 transaction in R/W mode
 Insert, update, delete, merge and select are possible
 SET TRASACTION READ ONLY;
 Only select is possible
 SET TRASACTION ISOLATION LEVEL READ COMMITTED;
//default
 SET TRASACTION ISOLATION LEVEL SERIALIZABLE;

5
Lab Task#1
 Create database called Account
 Create table User_Account(Id:Pk, name, balance)
 Insert records of three users A=1200, B =1500 and C=2000
 Transfer 500 birr from Account A to B with commit
 Transfer 500 birr from Account A to C with rollback
 Try this combination
 Begin Transaction
 Insert new record, D with balance 2500
 Update B, deposit 500
 Save Transaction sp1;
 Update C, withdraw 1000
 Rollback Transaction sp1;
 Then see the effect on your database

6
Data Control Language (DCL)
 DCL Commands
 GRANT
 REVOKE
 How to Create User

7
SQL GRANT Statement
 The GRANT Statement grants access privileges for database
objects to other users.
 It has the following general format:
 GRANT privilege-list ON [TABLE] object-list TO user-list
 Privilege-list is either ALL PRIVILEGES or a comma-separated list of
properties: SELECT, INSERT, UPDATE, DELETE.
 Object-list is a comma-separated list of table and view names.
 User-list is either PUBLIC or a comma-separated list of user names.
 Privileges granted are revoked with the REVOKE Statement.
 The optional specifier WITH GRANT OPTION may follow
user-list in the GRANT statement.
 WITH GRANT OPTION specifies that, in addition to access
privileges, the privilege to grant those privileges to other users
is granted.

8
GRANT STATEMENT
 GRANT Statement Examples
 GRANT SELECT ON Student TO PUBLIC;
 GRANT SELECT, INSERT, UPDATE ON Employee TO Abdisa,
Abdi;
 GRANT SELECT ON Employee TO Biiftu WITH GRANT
OPTION;

9
REVOKE Statement
 The REVOKE Statement revokes access privileges for database
objects previously granted to other users.
 It has the following general format:
 REVOKE privilege-list ON [TABLE] object-list FROM user-list
 The REVOKE Statement revokes each privilege in privilege-list
for each object (table) in object list from each user in user-list.
 All privileges must have been previously granted.
 The user-list may specify PUBLIC. This must apply to a
previous GRANT TO PUBLIC.
 REVOKE Statement Examples
 REVOKE SELECT ON Student FROM PUBLIC
 REVOKE SELECT, INSERT, UPDATE ON Employee FROM Abdisa
 REVOKE SELECT ON Employee FROM Biftu

10
How to create user
 Syntax
 Create login userName with password = „pass‟
 Create user userName for login userName
 Example
 Create login Beshatu with password = „root‟
 Create user Beshatu for login Beshatu

11
VIEWS
 A view is a single virtual table that is derived from other table
 Views are saved sql query
 The other tables could be base tables or previously defined
view.
 Allows for limited update operations since the table may not
physically be stored
 Allows full query operations
 A view does not necessarily exist in physical form, which
limits the possible update operations that can be applied to
views.
 It doesn't store any data.

12
Views…
 Advantages of using views
 Views can be used to reduce the complexity of the database schema
 Used as mechanism to implement row and column level security
 Used to represent aggregated data and hide detailed data
 How to create view
 Syntax
Create view view_name
as
select field(s)
from tablename
where
condition

13
Views…
 How to access views
 select * from viewName
 How to see the definition of views
 sp_helptext viewname
 To modify a view
 Alter a view statement
 To drop a view
 Drop view viewname
 Views are updatable( insert, update and delete)
 Update to view make update to base table

14
Lab Task#2
 Create database called Account
 Create table User_Account(Id:Pk, name, balance, gender)
 Create two users(Talile and Guyo)
 Create two views:
 TalileView: user id, name and gender
 GuyoView: user id and balance
 Grant insertion, update and selection on TalileView to Talile
 Grant insertion, update and selection on GuyoView to Guyo
 Grant selection on TalileView to Guyo
 Grant selection on GuyoView to Talile
 Revoke selection on TalileView form Guyo
 Revoke selection on GuyoView from Talile

15
Stored-Procedures
 What are stored-procedures? And what are the advantages of using
them.
 Stored procedures are database objects that perform a user defined
operation.
 A stored procedure can have a set of compound SQL statements.
 A stored procedure executes the SQL commands and returns the
result to the client.
 SQL code can be reused over and over again.
 So if you have an SQL query that you write over and over again, save
it as a stored procedure, and then just call it to execute it.
 You can also pass parameters to a stored procedure, so that the
stored procedure can act based on the parameter value(s) that is
passed.

16
Advantages of using Stored Procedures
 Execution plan retention and reusability
 Reduce network traffic
 Code reusability and better maintainability
 Better security
 Avoid SQL injection attack

17
Stored-Procedures…
 Creating Stored Procedure
 Syntax
CREATE PROCEDURE procedure_name
AS
Begin
sql_statement
End
GO;
 Execute a Stored Procedure
EXEC procedure_name;
 Example
CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;
 Execute the stored procedure above as follows:
EXEC SelectAllCustomers;

18
Stored-Procedures…
 Stored Procedure With One Parameter
 Creates a stored procedure that selects Customers from a particular City from the
"Customers" table:
CREATE PROCEDURE SelectAllCustomers @City varchar(30)
AS
SELECT * FROM Customers WHERE City = @City
GO;
 Execute the stored procedure above as follows:
EXEC SelectAllCustomers City = “Adama";
 Setting up multiple parameters is very easy. list of each parameter and data type separated by
a comma
 Example
CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;
 Example
EXEC SelectAllCustomers City = “Adama", PostalCode = "W0012";
19
Stored Procedure…
 How to view text definition of Stored procedure
 sp_helptext SPName
 Altering procedure
 use alter to change definition of stored procedure
 Syntax
 ALTER PROCEDURE procedure_name
AS
sql_statement
GO;
 Drop Stored Procedure, remove Stored procedure
 Syntax
 Drop procedure SPName
 To encrypt the text stored procedure
 Syntax
ALTER PROCEDURE procedure_name
With Encryption
AS
sql_statement
GO;

20
Lab Task #3
 Create database called Account
 Create table User_Account(Id:Pk, name, balance, gender)
 Create two stored procedure on User_Account:
 StoredPro1: select user id, name and gender
 StoredPro2: select user id, name and balance, then execute it.
 Alter definition of StoredPro1, to accept one parameter
@name.
 Alter definition of StoredPro2 , to accept two parameter
@name and @balance
 Alter StoredPro1 and encrypt definition
 Drop StoredPro2

21
TRIGGERS
 SQL triggers are special stored procedures that are executed
automatically in response to the database object, database, and
server events.
 Triggers involve actions and fired/executed
automatically whenever an event occurs such as an
insert, delete, or update operation or pressing a button
or when mouse button is clicked
 Used to enforce business logic that can‟t be coded through
referential integrity or constraints

22
Triggers…
 SQL Server provides three type of triggers:
 Data manipulation language (DML) triggers which are
invoked automatically in response to INSERT, UPDATE,
and DELETE events against tables.

 Data definition language (DDL) triggers which fire in


response to CREATE, ALTER, and DROP statements.
 DDL triggers also fire in response to some system stored procedures
that perform DDL-like operations.

 Logon triggers which fire in response to LOGON events

23
Benefits of Triggers
 Generating some derived column values automatically
 Enforcing referential integrity
 Event logging and storing information on table access
 Auditing
 Synchronous replication of tables
 Imposing security authorizations
 Preventing invalid transactions

24
DML Triggers
 DML triggers are can be classified into 2 types
 AFTER trigger (FOR trigger) and
 INSTEAD OF triggers
 After Trigger:
 Fires after the triggering action.
 The INSERT, UPDATE and DELETE statements causes after trigger to fire
after the respective statements complete execution.
 INSTEAD OF Triggers:
 Fires instead of the triggering action.
 The INSERT, UPDATE and DELETE statements cause an INSTEAD OF to
fire INSTEAD OF the respective statement execution.

25
Creating Trigger
 The CREATE TRIGGER statement allows you to create a
new trigger that is fired automatically whenever an event
such as INSERT, DELETE, or UPDATE occurs against a
table.
 Syntax of the CREATE TRIGGER statement:
CREATE TRIGGER Trigger_Name
ON table_name
{AFTER | INSTEAD OF} {INSERT | UPDATE | DELETE}
AS
BEGIN
-- sql_statements
END

26
AFTER(FOR) Trigger
 Example(AFTER INSERT)  Example(AFTER Update)
CREATE TRIGGER ForInsert
ON Employee CREATE TRIGGER ForUpdate
FOR insert ON Employee
AS FOR update
BEGIN
print „Successfully Inserted!!‟
AS
END BEGIN
 Example(AFTER DELETE) print „Successfully Updated!!‟
CREATE TRIGGER ForDelete END
ON Employee
FOR delete
AS
BEGIN
print„Successfully Deleted!!‟
END

27
INSTEAD OF TRIGGER
 Example(instead of Update)
Create trigger InsteadOfUpdate
ON Department
Instead of update
AS
BEGIN
print 'Cannot be changed!!'
END

28
Modify the definition of Trigger
 Altering Trigger
Alter trigger trigger_name //existing trigger
ON table_name
For Event
AS
BEGIN
-- body of trigger
END
 Dropping trigger
 Syntax
 Drop trigger trigger_name ON table_name
 Example: drop trigger ForDelete ON Department

29
Disable and Enable Trigger
 Syntax to disable/enable
 Disable trigger trigger_name ON table_name
 Enable trigger trigger_name ON table_ Name

 Example
 Disabel trigger ForDelete ON Department
 Enable trigger ForDelete ON Department

30
Lab Task #4
 Create database called Account
 Create table User_Account(Id:Pk, name, balance, gender)
 Create three After trigger on User_Account:
 ForInsert: which display “Successfully Inserted!!”
 ForUpdate: which display “Successfully Updated!!”
 ForDelete: which display “Successfully Deleted!!”
 Create instead of trigger on User_Account:
 InsteadOfUpdate: to display “You Cannot update!!”
 Alter definition of ForUpdate, to display message “Successfully
Changed!!”
 Disable trigger ForDelete
 Drop trigger ForInsert

31

You might also like