PL/SQL Exercises
PL/SQL Exercises
• أول حاجه محتاج اخصم المبلغ من حساب الشخص اللى بيبعت فلوس
• تاني حاجه هزود المبلغ اللى خصمته لحساب الشخص اللى ببعتله الفلوس
Begin
UPDATE transfer SET balance = balance – :value
WHERE acc_id = :from_acc AND balance >= :value;
Student Subject
• المطلوب :أول حاجه هحسب مجموع درجات كل مادة عن طريق الحسبة دي :
#ID total = lab + oral + written
#Code
*Name
*Title
oScore
oDesc • تاني حاجه اشوف حالة كل مادة بمعنى الطالب جاب فيها درجة النجاح وال أل
oYear_State
اذن حسب مجموع الدرجات هحدد if total >= 50, then subject state = success
Register • تالت حاجه هحسب مجموع درجات الطالب (عباره عن مجموع درجات جميع المواد اللى هو
مسجل فيها)
#Std_ID
#Sub_Code
oLab • رابع حاجه احسب حالة الطالب (راسب أو ناجح) "بالنسبة لجميع المواد" بحسبها عن طريق:
oWritten لو عدد المواد اللى الطالب رسب فيها أكتر من مادتين اذن الطالب بيكون راسب
oOral
oTotal
oSub_State
• PL/SQL code:
Begin
-- Register Table
UPDATE register
SET total = nvl(lab,0) + nvl(oral,0) + nvl(written,0) ;
UPDATE register
SET sub_state = ‘S’ WHERE total >= 50 ;
UPDATE register
SET sub_state = ‘F’ WHERE total < 50 ;
-- Student Table
UPDATE student SET score = ( SELECT sum(total) FROM register WHERE register.std_id = student.id );
End;
Third Example: Bank Certificate Purchase
Purchase_Cer
#Purchase_serial
#ACC_No
#Cer_type
*Purchase_Date
:pur_seq.nextval
Client Certificate
sysdate
#ACC_No #Cer_Type
*Name *Title
*NID *Period :acc_id
*Balance *Value
:type
Client_Cer
#ACC_No
#Cer_type
*Cer_serial
*Start_Date
*Period
*Value
• PL/SQL Code:
Create sequence pur_sequence
increment by 1
start with 1;
Begin
-- هخصم فلوس الشهادة من حساب المستخدم
UPDATE client SET balance = balance – :value
WHERE acc_id = :acc_id AND balance >= :value;
commit;
• ON COMMIT: Automatically refreshed when a transaction involving the underlying tables is committed.
• The figure shows three tables: Student, subject and result. The result table
holds the score of subjects for each student. The maximum score for each
subject is 100 degrees. For the student to pass a subject he/she must get 50
degrees or more.
• Design a PL/SQL function called get_state which accepts student ID as a
parameter and returns his state according to the following table:
• State Condition:
‘Failed’ Failed in more than two subjects
‘Pass 2’ If he failed in only 2 subjects
‘Pass 1’ Failed in only 1 subject
‘Success’ If he/she succeeded in all subject
• PL/SQL Code:
-- Conditional statement
IF V_NoFailed = 0 THEN V_STATE := 'Success' ;
ELSIF V_NoFailed = 1 THEN V_STATE := 'Pass 1' ;
ELSIF V_NoFailed = 2 THEN V_STATE := 'Pass 2' ;
ELSE V_STATE := 'Fail' ;
END IF;
return v_state;
END;
Example on Procedure: ‘Login Procedure’
• PL/SQL Code:
In a login form, user is prompted to enter his, her :UserName and :Password.
The Login button calls a stored Boolean function Validate, which returns TRUE if
both username and password exist in User Table (ID, TrueName, UNAME, PASS),
else it returns FALSE,
a) Define and write the code of Validate function
b) Show how to call it from login button
c) Modify the code to prevent users from login at ‘Friday’
(a)
CREATE OR REPLACE FUNCTION Validate (P_UserName IN User.UNAME %TYPE , P_Pass IN User.PASS%TYPE) RETURN BOOLEAN
IS
V_UserID User.ID%TYPE;
BEGIN
SELECT ID into V_UserID FROM User WHERE UNAME = P_UserName AND PASS= P_Pass
AND To_CHAR(SYSDATE, ‘Day’) <> ‘Friday’; --prevent users from login at ‘Friday’
IF V_UserID is not null THEN RETURN TRUE;
ELSE RETURN FALSE
END IF;
EXCEPTION
RETURN FALSE;
END;
(b) How to call it
DECLARE
v_login BOOLEAN;
BEGIN
v_login := Validate ( :UserName , :Password); -- call the function
IF v_login = FALSE THEN Message(‘ Not a Valid Login’);
END IF;
END;