Add DBMS/SQL/Week6/writeup.md
This commit is contained in:
parent
db93a685ab
commit
8f1a41fd6c
80
DBMS/SQL/Week6/writeup.md
Normal file
80
DBMS/SQL/Week6/writeup.md
Normal file
@ -0,0 +1,80 @@
|
||||
### 2. Generate a trigger displaying driver information, on participating in an accident
|
||||
|
||||
```SQL
|
||||
SQL> create or replace trigger driver_info
|
||||
after insert on participated
|
||||
for each row
|
||||
declare
|
||||
id varchar2(4);
|
||||
nam varchar2(10);
|
||||
addr varchar2(30);
|
||||
begin
|
||||
select "driver_id#", name, address
|
||||
into id, nam, addr
|
||||
from person
|
||||
where :new."driver_id#" = person."driver_id#";
|
||||
dbms_output.put_line('driver_id: ' || id || ' name: ' || nam || ' address: ' || addr);
|
||||
exception
|
||||
when no_data_found then
|
||||
dbms_output.put_line('no driver found for driver_id: ' || :new."driver_id#");
|
||||
when others then
|
||||
dbms_output.put_line('an error occurred: ' || sqlerrm);
|
||||
end;
|
||||
/
|
||||
|
||||
Trigger created.
|
||||
```
|
||||
|
||||
### 3. Create a function to return total number of accidents happened in a particular year.
|
||||
|
||||
```SQL
|
||||
SQL> create or replace function total_accd
|
||||
(year in int)
|
||||
return int
|
||||
as
|
||||
total int;
|
||||
begin
|
||||
select count(report_number)
|
||||
into total
|
||||
from accident
|
||||
where extract(year from accd_date)=year;
|
||||
return total;
|
||||
end;
|
||||
/
|
||||
|
||||
Function created.
|
||||
|
||||
SQL> select total_accd(2024) from dual;
|
||||
|
||||
TOTAL_ACCD(2024)
|
||||
----------------
|
||||
2
|
||||
```
|
||||
|
||||
### 4. Create a procedure to display total damage caused due to an accident for a particular driver on a specific year.
|
||||
```SQL
|
||||
create or replace procedure tot_damage
|
||||
(driver in varchar2, year in number) is
|
||||
damage number := 0; -- initialize damage to 0
|
||||
begin
|
||||
select sum(damage_amount) into damage
|
||||
from participated pa
|
||||
natural join accident ac
|
||||
where pa."driver_id#" = driver -- use the correct column name with quotes
|
||||
and extract(year from ac.accd_date) = year;
|
||||
|
||||
dbms_output.put_line('total damage: ' || nvl(damage, 0));
|
||||
exception
|
||||
when no_data_found then
|
||||
dbms_output.put_line('no data found for the given driver and year.');
|
||||
when others then
|
||||
dbms_output.put_line('an error occurred: ' || sqlerrm);
|
||||
end;
|
||||
/
|
||||
```
|
||||
```sql
|
||||
SQL> execute tot_damage('1236',2024);
|
||||
Total damage: 150
|
||||
|
||||
PL/SQL procedure successfully completed.
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user