Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

VIEWS

Write a query to create view “Communi es” consis ng of Name, Loctaion and
Comm_Category

CREATE VIEW COMMUNITIES AS SELECT NAME, LOCATION, COMM_CATEGORY FROM COMMUNITY;

SELECT * FROM COMMUNITIES;

Now, Inser ng a row in Community Table


INSERT INTO COMMUNITY VALUES ('CTF', 11, 'Nerul', 'Cybersecurity', 300);

SELECT * FROM COMMUNITIES;


The inser on in the Community Table reflects in the Created View Communi es
Write a query to create view “Event_and_Par cipant” consis ng of Event.ID, Event.Name,
Event.E_Date, Par cipant_Name, ar cipant_Loca on and entries for each par cipants
who par cipated in the events

CREATE VIEW Event_and_Par cipant AS


SELECT EVENT.ID, EVENT.Name, EVENT.E_Date, PARTICIPANT.Name AS Par cipant_Name,
PARTICIPANT.Loca on AS Par cipant_Loca on
FROM EVENT
INNER JOIN PARTICIPATES ON EVENT.ID = PARTICIPATES.Event_ID
INNER JOIN PARTICIPANT ON PARTICIPATES.PARTICIPANT_ID = PARTICIPANT.EMAIL;

SELECT * FROM Event_and_Par cipant;


TRIGGERS
Create a Trigger which store informa on about inser on, dele on or upda on of Events in a table
consis ng of Event_ID, Ac on & Timestamp

Crea ng Table “Event_Audit”

CREATE TABLE EVENT_AUDIT (


Event_ID NUMBER(2),
Ac on VARCHAR2(50),
Audit_Timestamp TIMESTAMP DEFAULT SYSTIMESTAMP,
CONSTRAINT event_audit_pk PRIMARY KEY (Event_ID, Audit_Timestamp)
);

Crea ng Trigger “Event_Audit_Trigger”

CREATE OR REPLACE TRIGGER event_audit_trigger


AFTER INSERT OR UPDATE OR DELETE ON EVENT
FOR EACH ROW
BEGIN
IF INSERTING THEN
INSERT INTO EVENT_AUDIT (Event_ID, Ac on)
VALUES (:NEW.ID, 'Event created');
ELSIF UPDATING THEN
INSERT INTO EVENT_AUDIT (Event_ID, Ac on)
VALUES (:OLD.ID, 'Event updated');
ELSIF DELETING THEN
INSERT INTO EVENT_AUDIT (Event_ID, Ac on)
VALUES (:OLD.ID, 'Event deleted');
END IF;
END;

Ini ally, the table “Event_Audit” is empty, a er upda ng a row in the Event table, the data in
Table is displayed and following is the result

Upda ng row in Event Table


UPDATE EVENT SET Loca on = 'Bandra', Fees = 500 WHERE ID = 1;

Displaying contents of table “Event_Audit”


SELECT * FROM EVENT_AUDIT;
Write a query to create a trigger which increments the number of par cipants in the
Community Table whenever a new par cipant joins the Community

CREATE OR REPLACE TRIGGER update_no_par cipants


AFTER INSERT ON PARTICIPATES
FOR EACH ROW
BEGIN
UPDATE COMMUNITY
SET No_par cipants = No_par cipants + 1
WHERE ID IN (
SELECT Community_ID
FROM JOINS
WHERE Event_ID = :NEW.Event_ID
);
END;

Before inser on, the Community Table has the following contents
A er Inser ng a new row in the Par cipates table
INSERT INTO PARTICIPATES VALUES ('sid@email.com', 4);

Displaying the contents of the Community Table


SELECT * FROM COMMUNITY;

The value inserted in the Par cipates Table (Par cipant_ID, Event_ID) is (‘sid@email.com’, 4),
the par cipant par cipated in the event with ID 4 which is posted in the Community ID 2,
a er inser on the value of No_Par cipants in the Community table got incremented by 1, as
a new par cipant enrolled in an event posted in the Community with ID 2

You might also like