Unit IV Dbms
Unit IV Dbms
Systems
10/9/2023 DBMS 2
Extensions to SQL
(PL/SQL)
10/9/2023 DBMS 3
What is PL/SQL
10/9/2023 DBMS 4
PL/SQL Functions and Procedures
10/9/2023 DBMS 5
Stored Function
10/9/2023 DBMS 6
PL/SQL Functions
10/9/2023 DBMS 7
Deterministic and Non- deterministic Functions
• A deterministic function always returns the same result for the same input parameters
whereas a non-deterministic function returns different results for the same input
parameters.
• If you don’t use DETERMINISTIC or NOT DETERMINISTIC, MySQL uses the
NOT DETERMINISTIC option by default.
• rand() is nondeterministic function. That means we do not know what it will return
ahead of time.
• DELIMITER $$
• CREATE FUNCTION myrand() RETURNS INT
• DETERMINISTIC
• BEGIN
• RETURN round(rand()*10000, 0);
• END$$
10/9/2023 DBMS 8
PL/SQL Functions – Example 1
▪ Define a function that, given the name of a department, returns the count of the
number of instructors in that department.
10/9/2023 DBMS 9
Example 1 (Cont)..
▪ The function dept_count can be used to find the department names and budget of
all departments with more than 12 instructors.
10/9/2023 DBMS 10
Example 2
▪ A function that returns the level of a customer based on credit limit. We use the IF
statement to determine the credit limit.
10/9/2023 DBMS 11
Example 2 (Cont..)
• Calling function:
• we can call the CustomerLevel() in a SELECT statement as follows:
O Output:
10/9/2023 DBMS 12
Example 3
10/9/2023 DBMS 13
Example 3 (cont..)
10/9/2023 DBMS 14
Stored Procedures
10/9/2023 DBMS 15
Stored Procedures in MySQL
10/9/2023 DBMS 16
Example 1 – No parameters
▪ The GetAllProducts() stored procedure selects all products from the products
table.
10/9/2023 DBMS 17
Example 1 (Cont..)
▪ Calling Procedure:
CALL GetAllProducts();
▪ Output:
10/9/2023 DBMS 18
Example 2 ( with IN parameter)
• Suppose we want to keep track of the total salaries of employees working for each
department
10/9/2023 DBMS 20
Example 2 (Cont..)
Step 3: Call the procedure to update the totalsalary for each department
10/9/2023 DBMS 21
Example 2 (Cont..)
10/9/2023 DBMS 22
Example 3 (with OUT Parameter)
▪ The following example shows a simple stored procedure that uses an OUT
parameter.
▪ Within the procedure MySQL MAX() function retrieves maximum salary from
MAX_SALARY of jobs table.
10/9/2023 DBMS 23
(Cont..)
• Procedure Call:
• mysql> CALL my_proc_OUT(@M)$$
• Query OK, 1 row affected (0.03 sec)
• mysql< SELECT @M$$
• Output:
+-------+
| @M |
+-------+
| 40000 |
+-------+
1 row in set (0.00 sec)
10/9/2023 DBMS 24
Example 4 (with INOUT Parameter)
▪ The following example shows a simple stored procedure that uses an INOUT
parameter.
▪ ‘count’ is the INOUT parameter, which can store and return values and
‘increment’ is the IN parameter, which accepts the values from user.
10/9/2023 DBMS 25
Example 4 (Cont..)
Function
Call:
10/9/2023 DBMS 26
Stored Procedures (Cont..)
▪ Use show procedure status to display the list of stored procedures you have created
10/9/2023 DBMS 27
Language Constructs for Procedures &
Functions
▪ SQL supports constructs that gives it almost all the power of a general-purpose
programming language.
o Warning: most database systems implement their own variant of the standard
syntax below.
▪ Compound statement: begin … end,
o May contain multiple SQL statements between begin and end.
o Local variables can be declared within a compound statements
10/9/2023 DBMS 28
Language Constructs
▪ CASE Statement
CASE case_expression
WHEN when_expression_1 THEN commands
WHEN when_expression_2 THEN commands
...
ELSE commands
END CASE;
10/9/2023 DBMS 30
Language Constructs (Cont.)
10/9/2023 DBMS 31
Language Constructs (Cont.)
▪ The following is another handler which means that in case an error occurs, rollback the
previous operation, issue an error message, and exit the current code block.
▪ If we declare it inside the BEGIN END block of a stored procedure, it will terminate stored
procedure immediately.
10/9/2023 DBMS 33
Error Handling in MySQL
▪ The following handler means that if there are no more rows to fetch, in case of a
cursor or SELECT INTO statement, set the value of the no_row_found variable
to 1 and continue execution.
▪ The following handler means that if a duplicate key error occurs, MySQL error
1062 is issued. It issues an error message and continues execution.
10/9/2023 DBMS 34
Example
▪ The article_tags table stores the relationships between articles and tags. Each
article may have many tags and vice versa.
▪ Next, create a stored procedure that inserts article id and tag id into the
article_tags table.
10/9/2023 DBMS 35
Example (Cont..)
10/9/2023 DBMS 36
Example (Cont..)
▪ After that, try to insert a duplicate key to check if the handler is really invoked.
10/9/2023 DBMS 38
Cursors
11/9/2023 DBMS 39
Cursors
11/9/2023 DBMS 40
Cursors
3. Then, use the FETCH statement to retrieve the next row pointed by the cursor and
move the cursor to the next row in the result set.
4. Finally, call the CLOSE statement to deactivate the cursor and release the memory
associated with it as follows:
11/9/2023 DBMS 41
Cursors
11/9/2023 DBMS 42
Example using Cursors
11/9/2023 DBMS 43
Example using Cursors
11/9/2023 DBMS 44
Example using Cursors
• Call procedure :
11/9/2023 DBMS 45
Another Example
11/9/2023 DBMS 46
Another Example (Cont..)
11/9/2023 DBMS 47
Another Example (Cont..)
11/9/2023 DBMS 48
Triggers
11/9/2023 DBMS 49
Triggers
11/9/2023 DBMS 50
Triggering Events and Actions in SQL
A trigger can be defined to be invoked either before or after the data is changed by INSERT, UPDATE or DELETE .
MySQL allows you to define maximum six triggers for each table.
11/9/2023 DBMS 51
MySQL Trigger Syntax
11/9/2023 DBMS 52
MySQL Trigger Example 1
Create a BEFORE
UPDATE trigger that is
invoked before a
change is made to the
employees table.
In a trigger defined for INSERT, you can use NEW keyword only.
You cannot use the OLD keyword.
However, in the trigger defined for DELETE, there is no new row so
you can use the OLD keyword only.
In the UPDATE trigger, OLD refers to the row before it is updated
and NEW refers to the row after it is updated.
11/9/2023 DBMS 54
Example (Cont..)
11/9/2023 DBMS 55
Example (Cont..)
11/9/2023 DBMS 56
Example 2
Create a trigger to update the total salary of a department when a new employee is hired:
11/9/2023 DBMS 59
MySQL Trigger
To drop a trigger
mysql> drop trigger <trigger name>
11/9/2023 DBMS 60
Types of Data
• Data can be broadly classified into four types:
– Structured Data:
▪ Have a predefined model, which organizes data into a form that is relatively easy to store,
process, retrieve
and manage
▪ E.g., relational data
– Unstructured Data:
▪ Opposite of structured data
▪ E.g., Flat binary files containing text, video or audio
▪ Note: data is not completely devoid of a structure (e.g., an audio file may still have an
encoding structure and some metadata associated with it)
– Dynamic Data:
▪ Data that changes relatively frequently
▪ E.g., office documents and transactional entries in a financial database
– Static Data:
▪ Opposite of dynamic data, E.g. Medical imaging data from MRI or CT scans
StructuredUnstructured
Media Production, Media Archive,
eCAD, mCAD, Office Broadcast, Medical
Docs Imaging
Transaction Systems,
BI, Data Warehousing
ERP, CRM
63
XML
• XML is case sensitive
• All start tags must have end tags
• Elements must be properly nested
• XML declaration is the first statement
• Every document must contain a root element
• Attribute values must have quotation marks
• Certain characters are reserved for parsing
DBMS 79
11/9/2023