Compare commits
No commits in common. "3a8015626828e78d0b5cc9d72bbf55c800e749b0" and "06c90bf3762ca53e55598311d3c7563472054a3e" have entirely different histories.
3a80156268
...
06c90bf376
@ -1,87 +0,0 @@
|
|||||||
# P10 - Trigger
|
|
||||||
|
|
||||||
**Problem Statement:** Trigger: Write a after trigger for Insert, update and delete event considering following requirement:
|
|
||||||
Emp(Emp_no, Emp_name, Emp_salary)
|
|
||||||
a) Trigger should be initiated when salary tried to be inserted is less than Rs.50,000/-
|
|
||||||
b) Trigger should be initiated when salary tried to be updated for value less than Rs. 50,000/-
|
|
||||||
Also the new values expected to be inserted will be stored in new table Tracking(Emp_no,Emp_salary).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Creating tables:
|
|
||||||
```sql
|
|
||||||
CREATE TABLE Emp(
|
|
||||||
Emp_no NUMBER(14),
|
|
||||||
Emp_name VARCHAR(255),
|
|
||||||
Emp_salary NUMBER(14)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE Tracking(
|
|
||||||
Emp_no NUMBER(14),
|
|
||||||
Emp_salary NUMBER(14)
|
|
||||||
);
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
## Trigger
|
|
||||||
```sql
|
|
||||||
CREATE OR REPLACE TRIGGER P10
|
|
||||||
AFTER INSERT OR UPDATE ON Emp
|
|
||||||
FOR EACH ROW
|
|
||||||
BEGIN
|
|
||||||
IF inserting THEN
|
|
||||||
IF (:New.Emp_salary < 50000) THEN
|
|
||||||
insert into Tracking (Emp_no, Emp_salary) VALUES (:New.Emp_no, :New.Emp_salary);
|
|
||||||
DBMS_OUTPUT.PUT_LINE('Inserting record with salary < 50000');
|
|
||||||
END IF;
|
|
||||||
ELSIF updating THEN
|
|
||||||
IF (:New.Emp_salary < 50000) THEN
|
|
||||||
UPDATE Tracking SET Emp_salary = :New.Emp_salary WHERE Emp_no = :Old.Emp_no;
|
|
||||||
DBMS_OUTPUT.PUT_LINE('Updated value of salary < 50000 for a record');
|
|
||||||
END IF;
|
|
||||||
END IF;
|
|
||||||
END;
|
|
||||||
/
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
1. After performing insertion operation:
|
|
||||||
```sql
|
|
||||||
INSERT INTO Emp VALUES (1, 'Tanmay', 45000);
|
|
||||||
INSERT INTO Emp VALUES (2, 'Rajesh', 35000);
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
<details>
|
|
||||||
<summary>Output</summary>
|
|
||||||
1 row(s) inserted.<br>
|
|
||||||
Inserting record with salary < 50000<br>
|
|
||||||
<br>
|
|
||||||
1 row(s) inserted.<br>
|
|
||||||
Inserting record with salary < 50000<br>
|
|
||||||
<br>
|
|
||||||
Tracking:<br>
|
|
||||||
EMP_NO EMP_SALARY<br>
|
|
||||||
1 45000<br>
|
|
||||||
2 35000<br>
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|
||||||
2. After performing update operation:
|
|
||||||
```sql
|
|
||||||
UPDATE Emp SET Emp_salary = 43000 WHERE Emp_no = 1;
|
|
||||||
|
|
||||||
```
|
|
||||||
<details>
|
|
||||||
<summary>Output</summary>
|
|
||||||
1 row(s) updated.<br>
|
|
||||||
Updated value of salary < 50000 for a record<br>
|
|
||||||
<br>
|
|
||||||
Tracking:<br>
|
|
||||||
EMP_NO EMP_SALARY<br>
|
|
||||||
1 43000<br>
|
|
||||||
2 35000<br>
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|
||||||
---
|
|
@ -3,37 +3,34 @@
|
|||||||
## Creating function
|
## Creating function
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
CREATE OR REPLACE FUNCTION P7(
|
CREATE OR REPLACE FUNCTION Age_calc (p_dob IN DATE)
|
||||||
dob IN DATE,
|
RETURN VARCHAR IS
|
||||||
f_month OUT NUMBER,
|
-- declare
|
||||||
f_day OUT NUMBER
|
ageOP VARCHAR2(255);
|
||||||
)
|
years NUMBER;
|
||||||
RETURN NUMBER
|
months NUMBER;
|
||||||
IS
|
days NUMBER;
|
||||||
current_date DATE := SYSDATE;
|
|
||||||
year_diff NUMBER;
|
|
||||||
month_diff NUMBER;
|
|
||||||
day_diff NUMBER;
|
|
||||||
BEGIN
|
BEGIN
|
||||||
year_diff := EXTRACT(YEAR FROM current_date) - EXTRACT(YEAR FROM dob);
|
|
||||||
month_diff := EXTRACT(MONTH FROM current_date) - EXTRACT(MONTH FROM dob);
|
|
||||||
day_diff := EXTRACT(DAY FROM current_date) - EXTRACT(DAY FROM dob);
|
|
||||||
|
|
||||||
IF (day_diff < 0) THEN
|
years := EXTRACT(YEAR FROM SYSDATE) - EXTRACT(YEAR FROM p_dob);
|
||||||
month_diff := month_diff - 1;
|
months := EXTRACT(MONTH FROM SYSDATE) - EXTRACT(MONTH FROM p_dob);
|
||||||
day_diff := day_diff + EXTRACT(DAY FROM last_day(add_months(SYSDATE, -1)));
|
days := EXTRACT(DAY FROM SYSDATE) - EXTRACT(DAY FROM p_dob);
|
||||||
|
|
||||||
|
IF (days < 1) THEN
|
||||||
|
months := months - 1;
|
||||||
|
days := days + EXTRACT(DAY FROM LAST_DAY(ADD_MONTHS(SYSDATE, -1)));
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
IF (month_diff < 0) THEN
|
IF (months < 1) THEN
|
||||||
year_diff := year_diff - 1;
|
years := years - 1;
|
||||||
month_diff := month_diff + 12;
|
months := months + 12;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
f_month := month_diff;
|
ageOP := 'Given person is ' || years || ' years ' || months || ' months ' || days || ' days old.';
|
||||||
f_day := day_diff;
|
|
||||||
|
|
||||||
RETURN year_diff;
|
RETURN ageOP;
|
||||||
END;
|
END Age_calc;
|
||||||
/
|
/
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -41,13 +38,8 @@ END;
|
|||||||
## Calling function
|
## Calling function
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
DECLARE
|
|
||||||
years NUMBER;
|
|
||||||
months NUMBER;
|
|
||||||
days NUMBER;
|
|
||||||
BEGIN
|
BEGIN
|
||||||
years := P7(TO_DATE('2003-12-02', 'YYYY-MM-DD'), months, days);
|
DBMS_OUTPUT.PUT_LINE(Age_calc(TO_DATE('2004-04-28', 'YYYY-MM-DD')));
|
||||||
DBMS_OUTPUT.PUT_LINE('Age: ' || years || ' Years ' || months || ' Months ' || days || ' Days.');
|
|
||||||
END;
|
END;
|
||||||
/
|
/
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user