Update DBMS/SQL/Week7/writeup.md
This commit is contained in:
		
							parent
							
								
									4a7538f18d
								
							
						
					
					
						commit
						1514e4d972
					
				
					 1 changed files with 175 additions and 1 deletions
				
			
		|  | @ -1 +1,175 @@ | ||||||
| ## Week 7 - DBMS Lab - PL SQL & Triggers | ## Week 7 - DBMS Lab - PL SQL & Triggers | ||||||
|  | 
 | ||||||
|  | ### DB | ||||||
|  | 
 | ||||||
|  | ```sql | ||||||
|  | SQL> select table_name from user_tables; | ||||||
|  | 
 | ||||||
|  | TABLE_NAME | ||||||
|  | -------------------------------------------------------------------------------- | ||||||
|  | ACCIDENT | ||||||
|  | CAR | ||||||
|  | OWNS | ||||||
|  | PARTICIPATED | ||||||
|  | PERSON | ||||||
|  | 
 | ||||||
|  | SQL> select * from accident; | ||||||
|  | 
 | ||||||
|  | REPORT_NUMBER ACCD_DATE LOCATION | ||||||
|  | ------------- --------- -------------------------------------------------- | ||||||
|  |             1 01-JAN-24 Delhi India | ||||||
|  |             3 29-FEB-24  India | ||||||
|  |             5 31-MAR-00  Gujrat India | ||||||
|  |            12 17-JUL-00  karnataka India | ||||||
|  | 
 | ||||||
|  | SQL> select * from car; | ||||||
|  | 
 | ||||||
|  | REGNO                MODEL                                YEAR | ||||||
|  | -------------------- ------------------------------ ---------- | ||||||
|  | ABCD0001             Celtos                                  3 | ||||||
|  | EFGH2001             Ferrari                                32 | ||||||
|  | DSDS0001             Urus                                    5 | ||||||
|  | ABCD4001             Honda City                              1 | ||||||
|  | HFSP5601             mini cooper                             7 | ||||||
|  | 
 | ||||||
|  | SQL> select * from owns; | ||||||
|  | 
 | ||||||
|  | DRIVER_ID#                     REGNO | ||||||
|  | ------------------------------ -------------------- | ||||||
|  | 1234                           ABCD0001 | ||||||
|  | 1235                           EFGH2001 | ||||||
|  | 1236                           DSDS0001 | ||||||
|  | 1237                           ABCD4001 | ||||||
|  | 1238                           HFSP5601 | ||||||
|  | 
 | ||||||
|  | SQL> select * from participated; | ||||||
|  | 
 | ||||||
|  | DRIVER_ID#                     REGNO                REPORT_NUMBER DAMAGE_AMOUNT | ||||||
|  | ------------------------------ -------------------- ------------- ------------- | ||||||
|  | 1234                           ABCD0001                         1         10000 | ||||||
|  | 1236                           DSDS0001                         3           150 | ||||||
|  | 1238                           HFSP5601                         5          1500 | ||||||
|  | 1238                           HFSP5601                        12         25000 | ||||||
|  | 
 | ||||||
|  | SQL> select * from person; | ||||||
|  | 
 | ||||||
|  | DRIV NAME       ADDRESS | ||||||
|  | ---- ---------- ------------------------------ | ||||||
|  | 1234 Rohan      Delhi India | ||||||
|  | 1237 Saarthak   Manipal India | ||||||
|  | 1236 Ramesh     Maharashtra India | ||||||
|  | 1235 Rohit      Banglalore India | ||||||
|  | 1238 Amogh      Muzaffarnagar | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ### Q1. Generate a trigger displaying driver information, on participating in an accident. | ||||||
|  | 
 | ||||||
|  | Trigger: | ||||||
|  | ```sql | ||||||
|  | CREATE OR REPLACE TRIGGER trg_display_driver_info | ||||||
|  | AFTER INSERT ON participated | ||||||
|  | FOR EACH ROW | ||||||
|  | DECLARE | ||||||
|  |     v_driver_name VARCHAR2(100); | ||||||
|  |     v_driver_address VARCHAR2(255); | ||||||
|  | BEGIN | ||||||
|  |     -- Fetch driver information based on DRIVER_ID# | ||||||
|  |     SELECT NAME, ADDRESS | ||||||
|  |     INTO v_driver_name, v_driver_address | ||||||
|  |     FROM person | ||||||
|  |     WHERE DRIVER_ID# = :NEW.DRIVER_ID#; | ||||||
|  | 
 | ||||||
|  |     -- Display the driver information | ||||||
|  |     DBMS_OUTPUT.PUT_LINE('Driver ID: ' || :NEW.DRIVER_ID#); | ||||||
|  |     DBMS_OUTPUT.PUT_LINE('Driver Name: ' || v_driver_name); | ||||||
|  |     DBMS_OUTPUT.PUT_LINE('Driver Address: ' || v_driver_address); | ||||||
|  |     DBMS_OUTPUT.PUT_LINE('Car Registration Number: ' || :NEW.REGNO); | ||||||
|  |     DBMS_OUTPUT.PUT_LINE('Report Number: ' || :NEW.REPORT_NUMBER); | ||||||
|  |     DBMS_OUTPUT.PUT_LINE('Damage Amount: ' || :NEW.DAMAGE_AMOUNT); | ||||||
|  | END; | ||||||
|  | / | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | Demo: | ||||||
|  | ```sql | ||||||
|  | SQL> INSERT INTO participated (DRIVER_ID#, REGNO, REPORT_NUMBER, DAMAGE_AMOUNT) | ||||||
|  |   2  VALUES (1235, 'EFGH2001', 3, 5000); | ||||||
|  | Driver ID: 1235 | ||||||
|  | Driver Name: Rohit | ||||||
|  | Driver Address: Banglalore India | ||||||
|  | Car Registration Number: EFGH2001 | ||||||
|  | Report Number: 3 | ||||||
|  | Damage Amount: 5000 | ||||||
|  | Driver_id: 1235 Name: Rohit Address: Banglalore India | ||||||
|  | 
 | ||||||
|  | 1 row created. | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ### Q2.  | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | ### Q3. List cars involved in accidents with cumulative damage exceeding a specific amount. | ||||||
|  | 
 | ||||||
|  | Checking for damage amount > 10000, we have | ||||||
|  | ```SQL | ||||||
|  | SQL> SELECT | ||||||
|  |           p.driver_id#, | ||||||
|  |           o.regno, | ||||||
|  |           SUM(pa.damage_amount) AS total_damage | ||||||
|  |       FROM | ||||||
|  |           PARTICIPATED pa | ||||||
|  |       JOIN | ||||||
|  |           OWNS o ON pa.driver_id# = o.driver_id# | ||||||
|  |       JOIN | ||||||
|  |           PERSON p ON o.driver_id# = p.driver_id# | ||||||
|  |       GROUP BY | ||||||
|  |           p.driver_id#, o.regno | ||||||
|  |       HAVING | ||||||
|  |           SUM(pa.damage_amount) > 10000; | ||||||
|  | 
 | ||||||
|  | DRIVER_ID#                     REGNO                TOTAL_DAMAGE | ||||||
|  | ------------------------------ -------------------- ------------ | ||||||
|  | 1238                           HFSP5601                    26500 | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ### Q4. Identify cars that have been involved in more than one accident and calculate the total damage for each car. | ||||||
|  | 
 | ||||||
|  | ```SQL | ||||||
|  | SQL> SELECT  | ||||||
|  |     o.regno,  | ||||||
|  |     COUNT(DISTINCT pa.report_number) AS accident_count,  | ||||||
|  |     SUM(pa.damage_amount) AS total_damage | ||||||
|  | FROM  | ||||||
|  |     PARTICIPATED pa | ||||||
|  | JOIN  | ||||||
|  |     OWNS o ON pa.driver_id# = o.driver_id# | ||||||
|  | GROUP BY  | ||||||
|  |     o.regno | ||||||
|  | HAVING  | ||||||
|  |     COUNT(DISTINCT pa.report_number) > 1; | ||||||
|  | 
 | ||||||
|  | REGNO                ACCIDENT_COUNT TOTAL_DAMAGE | ||||||
|  | -------------------- -------------- ------------ | ||||||
|  | HFSP5601                          2        26500 | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ### Q5. Calculate the average damage amount for accidents at each location. | ||||||
|  | ```sql | ||||||
|  | SQL> SELECT | ||||||
|  |           a.location, | ||||||
|  |           AVG(pa.damage_amount) AS average_damage | ||||||
|  |       FROM | ||||||
|  |           ACCIDENT a | ||||||
|  |       JOIN | ||||||
|  |           PARTICIPATED pa ON a.report_number = pa.report_number | ||||||
|  |       GROUP BY | ||||||
|  |           a.location; | ||||||
|  | 
 | ||||||
|  | LOCATION                                           AVERAGE_DAMAGE | ||||||
|  | -------------------------------------------------- -------------- | ||||||
|  |  karnataka India                                            25000 | ||||||
|  | Delhi India                                                 10000 | ||||||
|  |  India                                                       2575 | ||||||
|  |  Gujrat India                                                1500 | ||||||
|  |  ``` | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Aadit Agrawal
						Aadit Agrawal