Untitled Document-3
Untitled Document-3
->
Sql date and time functions are used to perform various operations on date
and time provided by the user following are sql date and time function
1) CURDATE();
Provides current date
Syntax: SELECT CURDATE();
2) CURTIME();
a) Provides current time
b) Syntax: SELECT CURTIME();
3) CURRENT_TIMESTAMP();
a) Provides current time as a timestamp.
b) Syntax: SELECT CURRENT_TIMESTAMP();
4) NOW();
a) Retrieves current date and time as timestamp
b) Syntax: NOW();
5) SYSDATE();
a) Provides current date and time used in sql or oracle
b) Syntax: SELECT SYSDATE();
->
1) performance : PL/SQL reduces network traffic by combining multiple sql
queries in one unit
2) Modularity : Using PL/SQL code can be combined into different
procedures & functions which increase modularity
3) Exception handling/error handling : PL/SQL enables you to handle
exceptions more effectively thus reducing over all errors.
4) Security : PL/SQL provides strong security measures by granting
specific privileges to stored procedures and functions helping to
restrict data.
->
1) Database triggers are a stored PL/SQL program unit ie. procedures which
are associated with a specific database table.
2) Database triggers automatically executes when a given SQL operation
like (insert, delete, update, delete) executes.
3) A database trigger has 3 parts
a) Triggering event
b) A optional trigger
c) Trigger action
4) Syntax:
CREATE TRIGGER (trigger name)
(BEFORE/AFTER)
{INSERT/UPDATE/DELETE}
ON (table name)
[FOR EACH ROW]
[TRIGGER]
5) Example:
CREATE TRIGGER meter_reading_insert
AFTER
INSERT ON meter_readings
FOR EACH ROW
BEGIN
CALL generate_invoice();
END;
Q.6 Write a PL/SQL program to print the sum of odd numbers using a for loop.
DECLARE
sum_of_odd:= 0;
n NUMBER := 10;
FOR i IN 1..20 LOOP
IF MOD(i*2)=0 THEN
sum_of_odd := sum_of_odd + i;
END IF:
END LOOP
Q.8 write PL/SQL program to print larger of 3 numbers imputed by the user
DECLARE
a NUMBER:= &NUM1;
b NUMBER:= &NUM2;
C NUMBER:= &NUM3;
largest NUMBER;
BEGIN
IF (a<b) AND (a>c) THEN
Largest := a;
ELSEIF (b>c) THEN
Largest :=b;
ELSE
Largest :=c;
END IF;
DBMS_OUTPUT.PUT_LINE(‘Largest number is: ‘|| largest);
END;
1) Advantages:
a) PL/SQL provides high performance
b) PL/SQL provides modularity
c) PL/SQL has better security
d) PL/SQL has better error handling
e) It supports object oriented programming
2) Disadvantages:
a) PL/SQL requires high amount of memory
b) It does not support functional debugging in stored procedures
Indexes are special lookup tables created by a database which are then by the
database search engines to speed-up the process of data retrieval.
Indexes are used to speed-up the SELECT and WHERE clause but provide input
delay.
Indexes can be created or dropped without having an effect on the data.