Practical-9 To 12
Practical-9 To 12
AIM: To apply the concept of security and privileges. (only Theory with
example.. No implementation).
A Data Control Language (DCL) has syntax similar to a computer.
programming language used to control access to data stored in a database
(Authorization). In particular, it is a component of Structured Query Language
(SQL).
Examples of DCL commands include:
GRANT to allow specified users to perform specified tasks.
REVOKE to remove the user accessibility to database object Some of Data
Control Language commands are:
1. GRANT COMMAND
The GRANT statement assigns one or more privileges to a specific user. The
following illustrates the basic syntax of the GRANT statement is:
GRANT (system privileges | object_privileges) TO user [WITH ADMIN
OPTION]
Example:
create user shivam identified by nopassword; grant create session to shivam;
grant create table to shivam; grant select, insert, update, delete on employee to
shivam;
2. REVOKE COMMAND
The Oracle REVOKE statement revokes system and object privileges from a
user.
The basic syntax of the Oracle REVOKE
statement: REVOKE (system_privilege |
object privilege) FROM user;
Example: revoke select, insert, update, delete on
employee from shivam; revoke create table from
shivam; revoke create session from shivam.
Practical-10
AIM: To study Transaction control commands.
Transaction control commands manage changes made by DML commands.
Collection of operation that forms a single logical unit of work is called
Transaction. Transaction can either be one DML statement or a group of
statements. Transaction must be atomic. All transactions have beginning and an
end. A transaction can be saved or undone. If transaction failed in middle no
part of the transaction can be saved. Transaction Command Controls:
COMMIT COMMAND:
COMMIT statement commits all changes for the current transaction. It is used
to permanently save any transaction into the database. Syntax: commit;
ROLLBACK COMMAND:
This command restores the database to last committed state. It is also used with
SAVEPOINT command to jump to a save point in an ongoing transaction.
Syntax: ROLLBACK
savepoint_name;
SAVEPOINT COMMAND:
SAVEPOINT command is used to temporarily save a transaction so that you
can roll back to that point whenever required.
Syntax: SAVEPOINT savepoint_name; Syntax: ROLLBACK savepoint_name;
1. TC CONTINUE TRANSACTION
The Integration Service does not perform any transaction change for the row.
This is the
2. TC COMMIT_BEFORE
default value of the expression.
The Integration Service commits the transaction, begins a new transaction, and
writes the current row to the target. The current row is in the new transaction.
In tc_commit_before, when this flag is found set, then a commit is performed
before the processing of the current row.
3. TC_COMMIT_AFTER
The Integration Service writes the current row to the target, commits the
transaction, and begins a new transaction. The current row is in the committed
transaction. In
tc_commit_after, the current row is processed then a commit is performed.
4. TC_ROLLBACK_BEFORE
The Integration Service rolls back the current transaction, begins a new
transaction, and writes the current row to the target. The
current row is in the new transaction.
In tc_rollback_before, rollback is performed first, and then data is processed to
write.
5. TC ROLLBACK_AFTER
The Integration Service writes the current row to the target, rollback the
transaction, and begins a new transaction. The current row is in the rolled-back
transaction. In tc_rollback after data is processed, then the rollback is
performed.
Prectiacal-11
1. Process Data:
→ Process the retrieved row as needed. You can use variables to store column
values.
2. Close Cursor:
→ Use the CLOSE statement to release the resources associated with the cursor
when done.
→ Syntax:
CLOSE cursor_name;
3. Deallocate Cursor:
→ Optionally, use the DEALLOCATE statement to remove the cursor
definition when it's no longer needed.
Syntax:
DEALLOCATE PREPARE cursor_name;
Example:
DELIMITER //
CREATE PROCEDURE ProcessData()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE yarl INT;
DECLARE var2 VARCHAR(255);
DECLARE your cursor CURSOR FOR SELECT id, name FROM
your_table;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done =
TRUE;
OPEN your cursor;
read_loop: LOOP
FETCH your cursor INTO varl, var2;
IF done THEN
LEAVE read_loop:
END IF;
--Your processing logic here using varl and var2
END LOOD
CLOSE your_cursor;
END //
DELIMITER;
Remember that using cursors can have performance implications, and in many
cases, set-based operations are more efficient. Cursors are typically used when
row-based processing is necessary or when dealing with procedural logic within
stored procedures.
Prectical-12
Aim: Write Trigger
→In MySQL, a trigger is a set of instructions that are automatically executed
("triggered") in response to a specified event on a particular table or view.
→The basic syntax for creating a trigger in MySQL is as follows:
CREATE TRIGGER trigger_name
(BEFORE AFTER) (INSERT | UPDATE | DELETE) ON table_name
FOR EACH ROW
BEGIN
-- Trigger body: SQL statements to be executed END;
Here's a breakdown of the key components:
1. trigger name:
→ This is the name you give to the trigger.
2. BEFORE AFTER:
→ Specifies whether the trigger should be executed before or after the
triggering event.
3.INSERT UPDATE | DELETE:
→ Defines the event that triggers the execution of the trigger.
4. ON table name:
→Specifies the table associated with the trigger.
5. FOR EACH ROW:
→ Indicates that the trigger should be invoked once for each row affected by the
triggering event.
6. BEGIN...END:
→ Encloses the SQL statements that make up the trigger body.
→ For example, a simple trigger that updates a timestamp column whenever a
row is updated in a table might look like this:
CREATE TRIGGER update_timestamp
BEFORE UPDATE
ON your_table
FOR EACH ROW
BEGIN
SET NEW.updated_at = NOW();
END;
→ This trigger sets the updated_at column to the current timestamp before any
update operation on the specified table.
→ Remember to exercise caution when using triggers, as they can impact
performance and introduce complexity to database logic.