Update DBMS/SQL/Week7/writeup.md
This commit is contained in:
		
							parent
							
								
									4c49bd8d7d
								
							
						
					
					
						commit
						2bd895d3bf
					
				
					 1 changed files with 62 additions and 1 deletions
				
			
		|  | @ -41,8 +41,69 @@ Driver_id: 1235 Name: Rohit Address: Banglalore India | |||
| 1 row created. | ||||
| ``` | ||||
| 
 | ||||
| ### Q2.  | ||||
| ### Q2. Create a trigger that updates a `total_damage` column in the `accident` table whenever a new entry is added to or removed from the participated field.` | ||||
| 
 | ||||
| ```SQL | ||||
| SQL> CREATE OR REPLACE TRIGGER update_total_damage | ||||
|   2  AFTER | ||||
|   3    INSERT OR DELETE ON PARTICIPATED | ||||
|   4  BEGIN | ||||
|   5    UPDATE ACCIDENT | ||||
|   6    SET | ||||
|   7      total_damage = ( | ||||
|   8        SELECT | ||||
|   9          SUM(damage_amount) | ||||
|  10        FROM PARTICIPATED p | ||||
|  11        WHERE | ||||
|  12          p.report_number = ACCIDENT.report_number | ||||
|  13      ); | ||||
|  14  END; | ||||
|  15  / | ||||
| 
 | ||||
| Trigger created. | ||||
| ``` | ||||
| ```sql | ||||
| SQL> ALTER TABLE ACCIDENT ADD total_damage NUMBER; | ||||
| ``` | ||||
| ```sql | ||||
| SQL> INSERT INTO | ||||
|         PARTICIPATED (driver_id#, regno, report_number, damage_amount) | ||||
|       VALUES | ||||
|         ('1235', 'EFGH2001', 1, 5000); | ||||
| Driver_id: 1235 Name: Rohit Address: Banglalore India | ||||
| 
 | ||||
| 1 row created. | ||||
| 
 | ||||
| SQL> SELECT | ||||
|         report_number, | ||||
|         total_damage | ||||
|       FROM ACCIDENT | ||||
|       WHERE | ||||
|         report_number = 1; | ||||
| 
 | ||||
| REPORT_NUMBER TOTAL_DAMAGE | ||||
| ------------- ------------ | ||||
|             1        15000 | ||||
| 
 | ||||
| SQL> DELETE FROM PARTICIPATED | ||||
|       WHERE | ||||
|         driver_id# = '1235' | ||||
|         AND regno = 'EFGH2001' | ||||
|         AND report_number = 1; | ||||
| 
 | ||||
| 1 row deleted. | ||||
| 
 | ||||
| SQL> SELECT | ||||
|         report_number, | ||||
|         total_damage | ||||
|       FROM ACCIDENT | ||||
|       WHERE | ||||
|         report_number = 1; | ||||
| 
 | ||||
| REPORT_NUMBER TOTAL_DAMAGE | ||||
| ------------- ------------ | ||||
|             1        10000 | ||||
| ``` | ||||
| 
 | ||||
| 
 | ||||
| ### Q3. List cars involved in accidents with cumulative damage exceeding a specific amount. | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Aadit Agrawal
						Aadit Agrawal