Professional Documents
Culture Documents
SQL
❖ Structured Query Language (SQL) is a language that all commercial RDBMS
implementations understand.
❖ SQL is a non-procedural language.
1
Naming Rules
❖ Begin with a letter and followed by zero or more of characters A-Z, 0-9, _, $, #
2
Data types
❖ Integers Decimal numbers--- NUMBER, INTEGER .
Number is an oracle data type. Integer is an ANSI data type. The syntax for
NUMBER is NUMBER(P,S) p is the precision and s is the scale. P can range
from 1 to 38 and s from -84 to +127
❖ Floating point numbers FLOAT
❖ Raw binary data, size bytes long. Maximum size is 32767 bytes RAW(size)
Constants/Literals
❖ ANSI standard defines format for literals
Operators
❖ Arithmetic operators like +,-,*,/
3
- The above two examples also illustrate use of relational operators
NULL
❖ Missing/unknown/inapplicable data represented as a null value
❖ NULL is not a data value. It is just an indicator that the value is unknown
Statements
- SQL has three flavours of statements.
4
DDL is Data Definition Language statements. Some examples:
▪ CREATE - to create objects in the database
▪ DELETE - deletes all records from a table, the space for the records remain
▪ SAVEPOINT - identify a point in a transaction to which you can later roll back
5
Database Objects
❖ Table -- Basic unit of storage; composed of rows and columns
C/old/new 🡪 Change old to new in the current line of the current buffer.
6
A text 🡪 Add text to the end of current line in current buffer.
SAVE name CREATE/ REPLACE/ APPEND 🡪 Save the content of current buffer
in to the file name. If filetype is missing, then CREATE is the default.
@filename 🡪 Run the file filename which contains SQL statements. The file may
contain any commands that may be interactively.
START file 🡪 Run the file. The file may contain any commands that may not be
interactively.
***
7
Ex.No. 1 CREATING DATABASE TABLE Date :
CREATE TABLE
▪ Used to create a table by defining its structure, the data type and name of the various
columns, the relationships with columns of other tables etc.
myapp_category
8
myapp_event
Myapp_event_rsvps
myapp_profile
myapp_profile_rsvp_events
9
myapp_registration
myapp_venue
SQL>
SQL>
10
Q2) Q2) Confirm table creation and describe table structure:
SQL>DESC myapp_attendee;
Q3) List name of the tables created for your event management app:
SQL>SHOW TABLES FROM ems;
This query will list all tables created by the user.
Q4) Describe tables owned by the user for your event management app:
SQL>SELECT * FROM information_schema.tables WHERE table_schema = 'ems';
This query provides a description of all tables owned by the user.
Q5) View distinct object types owned by the user for your event management app:
SQL>SELECT DISTINCT table_type FROM information_schema.tables
WHERE table_schema = 'ems';
This query lists distinct object types owned by the user.
Q6) View tables, views, synonyms, and sequences owned by the user for your event management app:
SQL>SELECT * FROM information_schema.tables WHERE table_schema = 'ems';
This query displays all tables, views, synonyms, and sequences owned by the user.
ALTER TABLE
▪ Add Column
ALTER TABLE table ADD(column data type [DEFAULT expr]
[, column data type]...);
▪ Drop Column
▪ Modify Column
ALTER TABLE table MODIFY(column data type [DEFAULT expr]
[, column data type]...);
11
Q7) Add new columns COMNT and MISCEL in the myapp_attendee table of character type:
SQL> ALTER TABLE myapp_attendee
ADD COLUMN COMNT VARCHAR(255),
ADD COLUMN MISCEL VARCHAR(255);
Q13) Remove all the rows in the myapp_attendee12 table (Presently no records in myapp_attendee12):
SQL>DELETE FROM myapp_attendee12;
12
Q14) Add some comment to the myapp_attendee12 table and also confirm the
inclusion of comment:
SQL > ALTER TABLE myapp_attendee12 COMMENT = 'This table stores
information about event attendees.';
Verified by
Staff In-charge Sign : Date :
13
Ex.No. 2 Working with Data Manipulation commands Date :
DML
⮚ A DML statement is executed when you:
• Add new rows to a table
• Modify existing rows in a table
• Remove existing rows from a table
⮚ A transaction consists of a collection of DML statements that form a logical unit of
work.
INSERT STATEMENT
⮚ Add new rows to a table by using the INSERT statement.
(i) INSERT INTO table VALUES(value1, value2,..);
14
▪ Only one row is inserted at a time with this syntax.
Q2) Insert the first & second rows of myapp_event table using syntax (ii):
SQL> INSERT INTO myapp_event (title, description, location, organizer, capacity, category_id,
date, time)
VALUES ('Conference', 'Annual tech conference', 'Convention Center', 'Tech Events Inc.', 200, 1,
'2024-06-15', '09:00:00'), ('Workshop', 'Hands-on workshop on web development', 'Community
Center', 'Coding Academy', 50, 2, '2024-07-20', '14:00:00');
Q3) Insert the remaining rows of myapp_event table using syntax (iii):
SQL> INSERT INTO myapp_event (title, description, location, organizer, capacity, category_id,
date, time)
VALUES (&title, &description, &location, &organizer, &capacity, &category_id, &date, &time);
Q4) Create a table ems_organizers with the following columns: organize-id, name, phone and email
SQL>CREATE TABLE ems_organizers (
organizer_id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(255),
phone_number VARCHAR(20));
15
Q5) Insert values into the ems_organizers table by copying the values from the
auth_user table where the role of the user is 'organizer':
16
UPDATE STATEMENT
▪ Modify existing rows with the UPDATE statement.
▪ All rows in the table are modified if you omit the WHERE clause
Q6) Change the location of all rows of myapp_event table to 'New York':
SQL> UPDATE myapp_event SET location = 'New York';
Q7) Change the location to 'Dallas' for events with category_id = 2 in myapp_event table:
SQL> UPDATE myapp_event SET location = 'Dallas' WHERE category_id = 2;
DELETE STATEMENT
▪ You can remove existing rows from a table by using the DELETE statement.
▪ All rows in the table are deleted if you omit the WHERE clause.
Q8) Delete the rows from myapp_attendee table whose first name is 'Paul':
SQL> DELETE FROM myapp_attendee WHERE first_name = 'Paul';
SELECT STATEMENT
▪ To perform a query we use select statement
SELECT [DISTINCT] {*, column [alias],...}
FROM table;
17
Q9) List all the columns and rows of the myapp_category table:
SQL> SELECT * FROM myapp_category;
Q10) List the name of the event and organizer of myapp_event table:
SQL> SELECT title AS event_name, organizer FROM myapp_event;
Q11) Without duplication, list all names of the venues in myapp_venue table:
SQL> SELECT DISTINCT name FROM myapp_venue;
Q14) List the attendee names from myapp_attendee table with the column headings NAME
SQL> SELECT CONCAT(first_name, ' ', last_name) AS NAME
FROM myapp_attendee;
DATABASE TRANSACTIONS
▪ Begin when the first executable SQL statement is executed
18
o The affected rows are locked; other users cannot change the data within the
affected rows.
▪ State of Data After COMMIT
o Data changes are made permanent in the database.
o The previous state of the data is permanently lost.
o All users can view the results.
o Locks on the affected rows are released; those rows are available for other
users to manipulate.
o All savepoints are erased.
Q16) Delete all the rows from myapp_event table and rollback the transaction:
SQL> DELETE FROM myapp_event;
ROLLBACK;
▪ Rolling Back to a Marker
19
c)Insert a new row in myapp_venue table with your own values:
SQL> INSERT INTO myapp_venue (name, address, capacity, facilities)
VALUES ('Grand Ballroom', '123 Main Street', 500, 'Sound system, Projector');
Verified by
Staff In-charge Sign :
20
Ex.No. 3 INTEGRITY CONSTRAINTS Date :
CONSTRAINTS
▪ Constraints enforce rules at the table level.Constraints prevent the deletion of a table
if there are dependencies.
▪ The following constraint types are valid in Oracle:
o NOT NULL
o UNIQUE Key
o PRIMARY KEY
o FOREIGN KEY
o CHECK
▪ Name a constraint or the Oracle Server will generate a name by using the
SYS_Cn format.
▪ Create a constraint:
o At the same time as the table is created
o After the table has been created
▪ Define a constraint at the column or table level.
DEFINING CONSTRAINTS
▪ Column constraint level
column [CONSTRAINT constraint_name] constraint_type
▪ Table constraint level
[CONSTRAINT constraint_name] constraint_type(column)
21
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(255),
phone_number VARCHAR(20));
NOT NULL Constraint;
⮚ Ensures that null values are not permitted for the column
CHECK Constraint
⮚ Defines a condition that each row must satisfy
UNIQUE Constraint
⮚ Prevent the duplication of values within the rows of a specified column
FOREIGN KEY
o Defines the column in the child table at the table constraint level
REFERENCES
o Identifies the table and column in the parent table
ON DELETE CASCADE
o Allows deletion in the parent table and deletion of the dependent rows in the
child table
ADDING A CONSTRAINT
⮚ Add or drop, but not modify, a constraint
22
⮚ Add a NOT NULL constraint by using the MODIFY clause
ALTER TABLE table ADD CONSTRAINT const-name cons-type (column);
Q2) Add NOT NULL constraint to the columns first_name and last_name of
myapp_attendee table:
SQL> ALTER TABLE myapp_attendee
MODIFY first_name VARCHAR(50) NOT NULL,
MODIFY last_name VARCHAR(50) NOT NULL;
23
Q3) Create table for Categories:
SQL>CREATE TABLE myapp_category (
id INT PRIMARY KEY,
name VARCHAR(255),
description TEXT );
DROPING CONSTRAINTS
▪ Removing constraints from the table
▪ Activate an integrity constraint currently disabled in the table definition by using the
ENABLE clause.
A UNIQUE or PRIMARY KEY index is automatically created if you enable a
UNIQUE key or PRIMARY KEY constraint.
Q13) Query the USER_CONSTRAINTS table to view all constraint definitions and
names:
SQL> SELECT constraint_name, constraint_type, search_condition
FROM USER_CONSTRAINTS
WHERE table_name = 'myapp_attendee';
Q14) View the columns associated with the constraint names in the
USER_CONS_COLUMNS view:
SQL> SELECT constraint_name, column_name
FROM USER_CONS_COLUMNS
WHERE table_name = 'myapp_attendee';
Verified by
Staff In-charge Sign : Date :
26
Ex.No. 4 BASIC SELECT STATEMENTS Date :
Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
Comparison Operators
= Equal to
<> Not Equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
IN (List) Match any of list of values
LIKE Match a character pattern (% 🡪 any no. of characters, - 🡪 One Character)
IS NULL Is a null value
BETWEEN…AND… Between two values
Logical Operators
AND Returns TRUE if both component conditions are TRUE
OR Returns TRUE if either component condition is TRUE
NOT Returns TRUE if the following condition is FALSE
Concatenation Operator ( || )
▪ Concatenates the Columns of any data type.
Operator Precedence
Order Evaluated Operators
1 Parenthesis
2 All Arithmetic Operators (Multiplication and Division
followed by Addition and subtraction)
3 All Comparison Operators
4 NOT
27
5 AND
6 OR
28
Where Clause
▪ Specify the Selection of rows retrieved by the WHERE Clause
SELECT column1, column2, …
FROM table
WHERE condition;
▪ The WHERE clause follows the FROM clause
Order by Clause
▪ Sort rows specified by the order ASC / DESC
SELECT column1, column2, … …
FROM table
ORDER BY sort-column DESC;
▪ Omitting the keyword DESC will sort the table in ascending order
Note :
▪ AS Keyword between the column name and the actual alias name
▪ Date and character literal values must be enclosed within single quotation marks
Q1) Update all the records of myapp_attendee table by increasing 10% of their
phone numbers as an additional contact.
SQL> UPDATE myapp_attendee SET phone_number = CONCAT
(phone_number, '_Additional');
Q2) Delete the records from myapp_attendee table where the length of the phone number is less than
10.
SQL> DELETE FROM myapp_attendee WHERE LENGTH(phone_number) < 10;
Q3) Display each attendee's first name as "First Name" and last name as "Last
Name", and their annual contact number as "Annual Contact Number".
29
SQL> SELECT first_name AS "First Name", last_name AS "Last Name",
CONCAT(phone_number, '_Annual') AS "Annual Contact Number"
FROM myapp_attendee;
Q4) List the concatenated value of first name and last name of each attendee.
SQL> SELECT CONCAT(first_name, ' ', last_name) AS "Full Name" FROM myapp_attendee;
30
Q5) List the first names of attendees from the myapp_attendee table.
SQL> SELECT first_name FROM myapp_attendee;
Q6) List the details of attendees who have registered before 30th September 1981.
SQL> SELECT * FROM myapp_attendee WHERE registration_date < '1981-09-30';
Q7) List names of attendees whose IDs are 7369, 7839, 7934, and 7788.
SQL> SELECT first_name, last_name FROM myapp_attendee WHERE id IN (7369, 7839, 7934,
7788);
Q9) SELECT first_name, last_name FROM myapp_attendee WHERE department_id NOT IN (30,
40, 10);
SQL> SELECT first_name, last_name FROM myapp_attendee WHERE department_id NOT IN
(30, 40, 10);
Q10) List names of attendees who joined between 30th June 1981 and 31st December 1981.
SQL> SELECT first_name, last_name FROM myapp_attendee WHERE registration_date
BETWEEN '1981-06-30' AND '1981-12-31';
Q13) List names and roles of attendees who do not report to anybody.
SQL> SELECT first_name, last_name, role FROM myapp_attendee WHERE reporting_to IS
NULL;
31
Q16) List attendees whose first names either start or end with 's'.
SQL> SELECT * FROM myapp_attendee WHERE first_name LIKE 's%' OR first_name LIKE '%s';
Q17) List names of attendees whose names have 'i' as the second character.
SQL> SELECT first_name, last_name FROM myapp_attendee WHERE first_name LIKE '_i%';
Q18) Sort the myapp_attendee table in ascending order by registration date and list first name, last
name, email, and registration date.
SQL> SELECT first_name, last_name, email, registration_date
FROM myapp_attendee
ORDER BY registration_date ASC;
Q19) Sort the myapp_attendee table in descending order by annual contact number and list first name,
last name, email, and annual contact number.
SQL> SELECT first_name, last_name, email, CONCAT(phone_number, '_Annual') AS "Annual Contact
Number"
FROM myapp_attendee
ORDER BY phone_number DESC;
Q20) List first name, department number, and salary after sorting the myapp_attendee table in
ascending order by department number and then descending order by salary.
SQL> SELECT first_name, department_id, salary
FROM myapp_attendee
ORDER BY department_id ASC, salary DESC;
Verified by
Staff In-charge Sign : Date :
32
Ex. No. 5 SQL FUNCTIONS Date :
Note : The exercises that follow mostly uses system table ‘dual’. It is a table which is
automatically created by Oracle along with the data dictionary. Dual table has one
column defined to be of varchar2 type and contains only one row with value ‘x’.
SCALAR FUNCTIONS
Q1) List the hiredate of employees who work in deptno 20 in a format like
‘WEDNESDAY JANUARY 12, 1983’
(Hint: DAY : Day of the week, MONTH : Name of the month, DD: Day of the
month, and YYYY : Year)
SQL> SELECT TO_CHAR(hire_date, 'DAY MONTH DD, YYYY') AS
"Hire Date"
FROM myapp_profile
WHERE department_id = 20;
Q2) Display the hiredate with time of employess who work in deptno 20.
SQL> SELECT TO_CHAR(hire_date, 'DD-MON-YYYY HH24:MI:SS') AS "Hire Date with
Time"
FROM myapp_profile
WHERE department_id = 20;
33
Q3) Each employee receives a salary review after every 150 days of service. Now list
employee name, hiredate and first salary review date of each employee who work
in dept no 20.
SQL> SELECT first_name || ' ' || last_name AS "Employee Name", hire_date,
hire_date + INTERVAL '150' DAY AS "First Salary Review Date"
FROM myapp_profile
WHERE department_id = 20;
34
07- 12’,’dd-mm-yy’)) from dual;
Date Arithmetic Add /Subtract no. of Select sysdate+25 from dual;
days to a date Select sysdate-25 from dual;
Subtract one date Select sysdate - to_date(‘02-10-06’,’dd-
from mm-yy’) from dual;
another, producing
a no. of days
35
Q5. Character Functions
36
Q7. Numeric Functions
37
GROUP FUNCTIONS
Common Group Functions
• AVG : Average value of a set
• COUNT : Numbers of non null values
• MAX : Maximum of a set
• MIN : Minimum of a set
• STDDEV : Standard Deviation of a set
• SUM : Sum of a set
• VARIANCE : Variance of a set
Syntax :
SELECT column, group_function(column)
FROM table
[WHERE condition]
[GROUP BY group_column_or_expression]
[HAVING group_condition]
[ORDER BY column];
⮚ Columns that are not a part of the Group Functions should be included in the Group
by clause
⮚ Any column or expression in the SELECT list that is not an aggregate function must
be in the GROUP BY clause
⮚ Group Functions cannot be placed in the where clause
⮚ HAVING clause is to restrict groups Groups satisfying the HAVING condition are
displayed
38
Q9) Find the number of rows in the myapp_profile table.
SQL> SELECT COUNT(*) AS "Number of Rows" FROM myapp_profile;Q10) Find the number of
different designations available in the myapp_profile table.
SQL> SELECT COUNT(DISTINCT job_title) AS "Number of Designations" FROM myapp_profile;
Q11) Find the number of employees who earn commission in the myapp_profile table.
SQL> SELECT COUNT(*) AS "Number of Employees Earning Commission"
FROM myapp_profile
WHERE commission > 0;
Q14) Find the maximum, minimum, and average capacity in the myapp_venue table.
SQL> SELECT
MAX(capacity) AS max_capacity,
MIN(capacity) AS min_capacity,
AVG(capacity) AS avg_capacity
FROM
myapp_venue; GROUP BY job_title;
Q15) Find the number of employees who work in department number 30.
SQL> SELECT COUNT(*) AS "Number of Employees in Department 30"
FROM myapp_profile
WHERE department_id = 30;
Q17) List the category numbers and number of attendee in each category
SQL> SELECT category_id, COUNT(*) AS "Number of Attendee"
FROM myapp_profile
GROUP BY category_id;
Q18) list the venue name and total capacity of venues, excluding those
where the capacity is less than or equal to 5000
SQL> SELECT name AS venue_name, SUM(capacity) AS "Total
Capacity"
39
FROM myapp_venue
GROUP BY name
HAVING SUM(capacity) > 5000
Verified by
Staff In-charge Sign : Date :
40
Ex. No. 6 JOINING TABLES Date :
Set operators
▪ Set operators combine the results of two queries into a single.
Operator Function
Union Returns all distinct rows selected by either query
Union all Returns all rows selected by either query including duplicates
Intersect Returns only rows that are common to both the queries
Minus Returns all distinct rows selected only by the first query and not
by the second.
42
Q3) List User (with Duplicates) with Either a Reservation or Attendee Record:
SQL> SELECT CONCAT(first_name, ' ', last_name) AS User _name
FROM myapp_profile
UNION ALL
SELECT CONCAT(first_name, ' ', last_name) FROM myapp_attendee;
Joins
▪ Used to combine the data spread across tables
Syntax
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
▪ If join condition is omitted, then a Cartesian product is formed. That is all rows in
the first table are joined to all rows in the second table
Types of Joins
▪ Inner Join (Simple Join) : It retrieves rows from 2 tables having a common column.
o Equi Join : A join condition with relationship = .
o Non Equi Join : A join condition with relationship other than = .
43
▪ Self Join : Joining of a table to itself
▪ Outer Join : Returns all the rows returned by simple join as well as
those rows from one table that do not match any row from
the other table. The symbol (+) represents outer joins.
Verified by
.
Staff In-charge Sign : Date :
44
Ex. No. 7 SUB QUERIES Date :
▪ The subquery (inner query) executes once before the main query.
▪ The result of the subquery is used by the main query (outer query).
Single-Row Subqueries
▪ Return only one row
Multiple-Row Subqueries
▪ Return more than one row
45
ANY Compare value to each value returned by the subquery
ALL Compare value to every value returned by the subquery
46
Q1) List the names of attendees whose email is not in the list of organizers' emails:
SQL> SELECT first_name, last_name
FROM myapp_attendee
WHERE email NOT IN (SELECT organizer_email FROM myapp_event);
Q2) List the titles of events organized by users who are members of a specific user
group:
SQL> SELECT title
FROM myapp_event
WHERE organizer_id IN (SELECT user_id
FROM auth_user_groups
WHERE group_id = (SELECT id
FROM auth_group
WHERE group_name = 'Organizers'));
Q3) List the names of attendees who have RSVP'd for events organized by users
who are also attendees of at least one event:
SQL> SELECT first_name, last_name
FROM myapp_attendee
WHERE id IN (SELECT user_id
FROM myapp_profile_rsvp_events
WHERE event_id IN (SELECT id
FROM myapp_event
WHERE organizer_id IN (SELECT user_id
FROM myapp_profile_rsvp_events)));
Q4) List the names of attendees who have RSVP'd for events with a capacity greater than the average
capacity of all events:
SQL> SELECT first_name, last_name
FROM myapp_attendee
WHERE id IN (SELECT user_id
FROM myapp_event_rsvps
WHERE event_id IN (SELECT id
FROM myapp_event
WHERE capacity > (SELECT AVG(capacity)
FROM myapp_event)));
Q5) List the titles of events that have not been RSVP'd by any attendee:
SQL> SELECT title
FROM myapp_event
WHERE id NOT IN (SELECT event_id FROM myapp_event_rsvps);
47
Q6) List the names of attendees who have registered for events with a capacity
greater than 100:
SQL>. SELECT first_name, last_name
FROM myapp_attendee
WHERE id IN (SELECT attendee_id
FROM myapp_registration
WHERE event_id IN (SELECT id
FROM myapp_event
WHERE capacity > 100));
Q7) List the names of attendees who have RSVP'd for events organized by users with
specific permissions:
SQL> SELECT first_name, last_name
FROM myapp_attendee
WHERE id IN (SELECT user_id
FROM myapp_event_rsvps
WHERE event_id IN (SELECT id
FROM myapp_event
WHERE organizer_id IN (SELECT user_id
FROM auth_user_user_permissions
WHERE permission_id IN (SELECT id
FROM auth_permission
WHERE permission_name =
'organizer_permission'))));
Q8) List the names of attendees who have registered for events with an attendee limit
that has been reached:
SQL> SELECT first_name, last_name
FROM myapp_attendee
WHERE id IN (SELECT attendee_id
FROM myapp_registration
WHERE event_id IN (SELECT id
FROM myapp_event
WHERE (SELECT COUNT(*)
FROM myapp_registration
WHERE event_id = myapp_event.id) >= capacity));
Q9) List the names of attendees who have RSVP'd for events organized by users who
have previously attended events themselves:
SELECT first_name, last_name
FROM myapp_attendee
WHERE id IN (SELECT user_id
48
FROM myapp_event_rsvps
WHERE event_id IN (SELECT id
FROM myapp_event
WHERE organizer_id IN (SELECT user_id
FROM myapp_profile_rsvp_events
WHERE event_id IN (SELECT id
FROM myapp_event
WHERE organizer_id =
myapp_profile_rsvp_events.profile_id))));
Verified By:
49
Ex. No. 8 VIEWS Date :
VIEWS
▪ An Imaginary table contains no data and the tables upon which a view is based are
called base tables.
▪ Logically represents subsets of data from one or more tables
Advantages of view
▪ To restrict database access
50
▪ You embed a subquery within the CREATE VIEW statement.
Q1) View to list all attendees along with their event registrations:
SQL> CREATE VIEW attendee_event_registrations AS
SELECT a.id AS attendee_id, a.first_name, a.last_name, a.email,
a.phone_number,
e.id AS event_id, e.title AS event_title, e.date AS event_date, e.time AS
event_time
FROM myapp_attendee a
JOIN myapp_registration r ON a.id = r.attendee_id
JOIN myapp_event e ON r.event_id = e.id;
▪ You cannot remove a row/ modify data/ add data if the view contains the
following
o Group functions
o A GROUP BY clause
o The DISTINCT keyword
▪ You cannot modify data in a view if it contains columns defined by expressions or
it contains ROWNUM pseudo column
▪ You cannot add data if any NOT NULL columns in the base tables that are not
selected by the view
51
a.id AS attendee_id, a.first_name, a.last_name, a.email, a.phone_number
FROM myapp_event e
LEFT JOIN myapp_registration r ON e.id = r.event_id
LEFT JOIN myapp_attendee a ON r.attendee_id = a.id;-- Confirm modifications
SELECT * FROM myapp_profile WHERE department_id = 10;
Deleting Views
Syntax
DROP VIEW event_with_attendees;
52
Ex. No. 9 ADVANCED SELECT STATEMENTS Date :
Q1) Get full details of all events in a specific location, say "New York".
SQL>SELECT *
FROM myapp_event
WHERE location = 'New York';
Q2) Get user IDs who have RSVP'd for a specific event, say with ID 1.
SQL>SELECT user_id
FROM myapp_event_rsvp
WHERE event_id = 1;
Q4) Get all event IDs, user IDs, and category IDs for events where users have RSVP'd.
SQL>SELECT event_id, user_id, category_id
FROM myapp_event_rsvp;
Q5) Get all event IDs, user IDs, and category IDs for events where users have not RSVP'd.
SQL>SELECT event_id, user_id, category_id
FROM myapp_event
WHERE id NOT IN (SELECT event_id FROM myapp_event_rsvp);
Q6) Get event titles for events organized by an organizer named "John Doe".
SQL>SELECT title
FROM myapp_event
WHERE organizer = 'John Doe';
Q7) Get all pairs of event locations and venue addresses such that the event is happening at the
venue.
SQL>SELECT e.location, v.address
FROM myapp_event e
INNER JOIN myapp_venue v ON e.location = v.name;
Q8) Get event IDs for events organized by organizers not residing in the same city as the event
location.
SQL>SELECT id
FROM myapp_event e
WHERE NOT EXISTS (
SELECT 1
FROM myapp_profile p
WHERE p.user_id = e.organizer_id
AND p.city = e.location
53
);
Q9) Get all pairs of event IDs and venue IDs where the venue capacity is greater than the event
capacity.
SQL>SELECT e.id AS event_id, v.id AS venue_id
FROM myapp_event e
INNER JOIN myapp_venue v ON e.capacity < v.capacity;
Verified by
Staff In-charge Sign : Date :
54
Ex. No. 10 ADDITIONAL QUERIES Date :
Solve the queries Q1 to Q32 by considering the
following tables Table 1 : STUDIES (PNAME, SPLACE,
COURSE, CCOST)
Hint : First 3 columns are of type varchar and the 4th one is number
LEGEND :
PNAME – Programmer Name, SPLACE – Study Place, CCOST – Course Cost, DEVIN
– Developed in, SCOST – Software Cost, DCOST – Development Cost, PROF1 –
Proficiency 1
Q4) Get the event titles along with the number of attendees:
SQL> SELECT e.title, COUNT(r.id) AS attendees_count
FROM myapp_event e
LEFT JOIN myapp_event_rsvp r ON e.id = r.event_id
GROUP BY e.title;
Q5) Get the event titles and their corresponding organizer names
SQL> SELECT e.title, p.first_name, p.last_name
FROM myapp_event e
JOIN myapp_profile p ON e.organizer_id = p.user_id;
55
Q6) Get the event titles with their RSVP status (Active/Inactive).
SQL> SELECT e.title, r.status
FROM myapp_event e
JOIN myapp_registration r ON e.id = r.event_id;
Q8) Get the event titles and their corresponding venue names:
SQL> SELECT e.title, v.name AS venue_name
FROM myapp_event e
JOIN myapp_venue v ON e.venue_id = v.id;
Q10) Get the event titles with their corresponding start times in AM/PM format
SQL> SELECT title, to_char(time, 'HH12:MI AM') AS start_time FROM myapp_event;
Q11) Get the event titles with their corresponding end times in 24-hour format:
SQL> SELECT title, to_char(time, 'HH24:MI') AS end_time FROM myapp_event;
Q12) Get the event titles with their corresponding day of the week:
SQL> SELECT title, to_char(date, 'Day') AS day_of_week FROM
myapp_event;
Q13) Get the event titles along with their full descriptions:
SQL> SELECT title, description FROM myapp_event;
Q14) Get the event titles and the total number of RSVPs for each event.
SQL> SELECT e.title, COUNT(r.id) AS total_rsvps
FROM myapp_event e
LEFT JOIN myapp_event_rsvp r ON e.id = r.event_id
56
GROUP BY e.title;
Q16) Get the event titles with their corresponding cities and countries:
SQL> SELECT e.title, v.city, v.country
FROM myapp_event e
JOIN myapp_venue v ON e.venue_id = v.id;
Q17) Get the event titles and their corresponding organizer emails:
SQL> SELECT e.title, p.email
FROM myapp_event e
JOIN myapp_profile p ON e.organizer_id = p.user_id;
Q18) Get the event titles with the total registration fees collected for each event
SQL> SELECT e.title, SUM(r.registration_fee) AS total_fees_collected
FROM myapp_event e
JOIN myapp_registration r ON e.id = r.event_id
GROUP BY e.title;
Q19) Get the event titles and their corresponding registration statuses (Active/Inactive):
SQL> SELECT e.title, r.status
FROM myapp_event e
JOIN myapp_registration r ON e.id = r.event_id;
Q20) Get the event titles along with the count of attendees who checked-in:
SQL> SELECT e.title, COUNT(r.id) AS checked_in_count
FROM myapp_event e
JOIN myapp_registration r ON e.id = r.event_id
WHERE r.check_in_status = 'Checked-In'
GROUP BY e.title;
Q21) Get the event titles and the average rating given by attendees for each event:
SQL> SELECT e.title, AVG(r.rating) AS average_rating
FROM myapp_event e
JOIN myapp_event_rsvp r ON e.id = r.event_id
GROUP BY e.title;
57
Q22) Get the event titles along with the total revenue generated from ticket sales:
SQL> SELECT e.title, SUM(r.ticket_price * r.ticket_quantity) AS total_revenue
FROM myapp_event e
JOIN myapp_registration r ON e.id = r.event_id
GROUP BY e.title;
Q23) Get the event titles and the total number of unique attendees for each event:
SQL> SELECT e.title, COUNT(DISTINCT r.user_id) AS unique_attendees_count
FROM myapp_event e
JOIN myapp_event_rsvp r ON e.id = r.event_id
GROUP BY e.title;
Q24) Get the event titles with the average age of attendees for each event:
SQL> SELECT e.title, AVG(EXTRACT(YEAR FROM age(current_date,
p.date_of_birth))) AS average_age
FROM myapp_event e
JOIN myapp_profile p ON e.organizer_id = p.user_id;
Q25) Get the event titles and the total number of male attendees for each event:
SQL> SELECT e.title, COUNT(CASE WHEN p.gender = 'Male' THEN 1
END) AS male_attendees_count
FROM myapp_event e
JOIN myapp_event_rsvp r ON e.id = r.event_id
JOIN myapp_profile p ON r.user_id = p.user_id
GROUP BY e.title;
Q26) Get the event titles and the total number of female attendees for each event:
SQL> SELECT e.title, COUNT(CASE WHEN p.gender = 'Female' THEN 1 END) AS
female_attendees_count
FROM myapp_event e
JOIN myapp_event_rsvp r ON e.id = r.event_id
JOIN myapp_profile p ON r.user_id = p.user_id
GROUP BY e.title;
Q27)Get the event titles and the total revenue generated from ticket sales, rounded to two decimal
places:
SQL> SELECT e.title, ROUND(SUM(r.ticket_price * r.ticket_quantity), 2) AS total_revenue
FROM myapp_event e
JOIN myapp_registration r ON e.id = r.event_id
GROUP BY e.title;
Q28) Get the event titles along with the count of attendees who checked-in, grouped by month:
SQL> SELECT e.title, EXTRACT(MONTH FROM e.date) AS month,
COUNT(r.id) AS checked_in_count
FROM myapp_event e
58
JOIN myapp_registration r ON e.id = r.event_id
WHERE r.check_in_status = 'Checked-In'
GROUP BY e.title, EXTRACT(MONTH FROM e.date);
Q29) Get the event titles along with the average rating given by attendees for each event, rounded to one decimal
place:
SQL> SELECT e.title, ROUND(AVG(r.rating), 1) AS average_rating
FROM myapp_event e
JOIN myapp_event_rsvp r ON e.id = r.event_id
GROUP BY e.title;
Q30) Get the event titles and the total number of unique attendees for each event, excluding organizers:
SQL> SELECT e.title, COUNT(DISTINCT r.user_id) AS unique_attendees_count
FROM myapp_event e
JOIN myapp_event_rsvp r ON e.id = r.event_id
LEFT JOIN myapp_profile p ON r.user_id = p.user_id
WHERE e.organizer_id != r.user_id
GROUP BY e.title;
Q31) Get the event titles with the average age of attendees for each event, rounded to one decimal place:
SQL> SELECT e.title, ROUND(AVG(EXTRACT(YEAR FROM age(current_date, p.date_of_birth))), 1)
AS average_age
FROM myapp_event e
JOIN myapp_event_rsvp r ON e.id = r.event_id
JOIN myapp_profile p ON r.user_id = p.user_id
GROUP BY e.title;
Q32) Get the event titles and the total number of male attendees for each event, categorized by age group:
SQL> SELECT e.title,
SUM(CASE WHEN p.gender = 'Male' THEN 1 ELSE 0 END) AS male_attendees_count,
SUM(CASE WHEN EXTRACT(YEAR FROM age(current_date, p.date_of_birth)) BETWEEN 18
AND 25 THEN 1 ELSE 0 END) AS age_group_18_25,
SUM(CASE WHEN EXTRACT(YEAR FROM age(current_date, p.date_of_birth)) BETWEEN 26
AND 35 THEN 1 ELSE 0 END) AS age_group_26_35
-- Add more age groups as needed
FROM myapp_event e
JOIN myapp_event_rsvp r ON e.id = r.event_id
JOIN myapp_profile p ON r.user_id = p.user_id
GROUP BY e.title;
Q33) Get the event titles and the total number of female attendees for each event, categorized by age group:
SQL> SELECT e.title,
SUM(CASE WHEN p.gender = 'Female' THEN 1 ELSE 0 END) AS female_attendees_count,
SUM(CASE WHEN EXTRACT(YEAR FROM age(current_date, p.date_of_birth)) BETWEEN 18
AND 25 THEN 1 ELSE 0 END) AS age_group_18_25,
SUM(CASE WHEN EXTRACT(YEAR FROM age(current_date, p.date_of_birth)) BETWEEN 26
AND 35 THEN 1 ELSE 0 END) AS age_group_26_35
-- Add more age groups as needed
FROM myapp_event e
JOIN myapp_event_rsvp r ON e.id = r.event_id
59
JOIN myapp_profile p ON r.user_id = p.user_id
GROUP BY e.title;
Verified by
Staff In-charge Sign : Date :
60
PL/SQL INTRODUCTION
PL/SQL
▪ PL/SQL bridges the gap between database technology and procedural programming
languages.
▪ PL/SQL uses the facilities of the sophisticated RDBMS and extends the standard SQL
database language
▪ Not only PL/SQL allow you to insert, delete, update and retrieve data, it lets you use
procedural techniques such as looping and branching to process the data.
▪ Thus PL/SQL provides the data manipulating power of SQL with the data processing
power of procedural languages
Advantage of PL/SQL
PL/SQL is a completely portable, high performance transaction processing language. It
provides the following advantages :
▪ Procedural Capabilities
o It supports many of constructs like constants, variable, selection and
iterative statements
▪ Improved Performance
o Block of SQL statements can be executed at a time
▪ Enhanced Productivity
o PL/SQL brings added functionality to non procedural tools such as
SQL Forms.
▪ Portability
o PL/SQL can run anywhere the RDBMS can run
▪ Integration with RDBMS
o Most PL/SQL variables have data types native to the RDBMS data
dictionary. Combined with the direct access to SQL, these native data
type declarations allow easy integration of PL/SQL with RDBMS.
Character Set
It is either ASCII or EBCDIC format
61
Identifiers
It begins with a letter and can be followed by letters, numbers, $ or #. Maximum
size is 30 characters in length.
62
Variable Declaration
The data types (number, varchar2, real, date, …) discussed in SQL are all
applicable in PL/SQL.
Ex. Salary Number(7,2);
Sex Boolean;
Count smallint :=0;
Tax number default 750;
Name varchar2(20) not null;
Constant declaration
Ex. Phi Constant Number(7,2) := 3.1417;
Comment
Line can be commented with double hyphen at the beginning of the line.
Ex. - - This is a comment line
Assignments
Variable assignment sets the current value of a variable. You can assign values to
a variable as follows
(i) Assignment operator (:=)
Ex. d := b*b – 4*a*c;
(ii) Select … into statement
Ex. Select sal into salary from emp where empno=7655;
Operators
Operators used in SQL are all applicable to PL/SQL also.
Block Structure
PL/SQL code is grouped into structures called blocks. If you create a stored procedure or
package, you give the block of PL/SQL code a name. If the block of PL/SQL code is not
given a name, then it is called an anonymous block.
The PL/SQL block divided into three section: declaration section, the executable
section and the exception section
The structure of a typical PL/SQL block is shown in the listing:
declare
< declaration section >
begin
< executable commands>
exception
<exception handling>
end;
53
Declaration Section :
Defines and initializes the variables and cursor used in the block
Executable commands :
Uses flow-control commands (such as IF command and loops) to execute the
commands and assign values to the declared variables
Exception handling :
Provides handling of error conditions
The %TYPE attribute is particularly useful when declaring variables that refer to
database columns. You can reference a table and column, or you can reference an
owner, table, and column.
my_dname dept.dname%TYPE;
DECLARE
emp_rec emp%ROWTYPE;
...
BEGIN
SELECT * INTO emp_rec FROM emp WHERE ...
...
END;
Columns in a row and corresponding fields in a record have the same names and
data types.
54
The column values returned by the SELECT statement are stored in fields. To
reference a field, you use the dot notation.
DECLARE
dept_rec1 dept%ROWTYPE;
dept_rec2 dept%ROWTYPE;
…..
BEGIN
.. …..
dept_rec1 := dept_rec2;
…..
END;
Second, you can assign a list of column values to a record by using the SELECT
and FETCH statement, as the example below shows. The column names must
appear in the order in which they were defined by the CREATE TABLE or
CREATE VIEW statement.
DECLARE
dept_rec dept%ROWTYPE;
….
BEGIN
SELECT deptno, dname, loc INTO dept_rec FROM dept
WHERE deptno = 30;
….
END;
55
Creating and Executing PL/SQL Programs
Edit your PL/SQL program in your favourite editor as text file.
Execute the following command once for a session to get displayed the output.
SQL> set serveroutput on;
Now execute the program using the following command.
SQL> start filename; (or) SQL> @filename;
Note : Give absolute path of the filename if you saved the file in some directory.
Ex. SQL> start z:\plsql\ex11; (or) SQL> @ z:\plsql\ex11;
Control Structures
(i) IF Statements
There are three forms of IF statements: IF-THEN, IF-THEN-ELSE, and IF-
THEN-ELSIF. The third form of IF statement uses the keyword ELSIF (NOT
ELSEIF) to introduce additional conditions, as follows:
IF condition1 THEN
sequence_of_statements1;
ELSIF condition2 THEN
sequence_of_statements2;
ELSE
sequence_of_statements3;
END IF;
LOOP
The simplest form of LOOP statement is the basic (or infinite) loop, which
encloses a sequence of statements between the keywords LOOP and END LOOP,
as follows:
LOOP
sequence_of_statements3;
...
END LOOP;
With each iteration of the loop, the sequence of statements is executed, then
control resumes at the top of the loop. If further processing is undesirable or
impossible, you can use the EXIT statement to complete the loop. You can place
one or more EXIT statements anywhere inside a loop, but nowhere outside a loop.
There are two forms of EXIT statements: EXIT and EXIT-WHEN.
56
The EXIT statement forces a loop to complete unconditionally. When an EXIT
statement is encountered, the loop completes immediately and control passes to
the next statement.
LOOP
...
IF ... THEN
...
EXIT; -- exit loop immediately
END IF;
END LOOP;
-- control resumes here
LOOP
….
EXIT WHEN i>n; -- exit loop if condition is true
….
END LOOP;
….
Until the condition evaluates to TRUE, the loop cannot complete. So, statements
within the loop must change the value of the condition.
Like PL/SQL blocks, loops can be labeled. The label, an undeclared identifier
enclosed by double angle brackets, must appear at the beginning of the LOOP
statement, as follows:
<<label_name>>
LOOP
sequence_of_statements;
...
END LOOP [label_name];
Optionally, the label name can also appear at the end of the LOOP statement.
With either form of EXIT statement, you can complete not only the current loop,
but any enclosing loop. Simply label the enclosing loop that you want to
complete, then use the label in an EXIT statement.
57
<<outer>>
LOOP
... LOOP
...
EXIT outer WHEN ... -- exit both loops
END LOOP;
...
END LOOP outer;
(iii) WHILE-LOOP
The WHILE-LOOP statement associates a condition with a sequence of
statements enclosed by the keywords LOOP and END LOOP, as follows:
Before each iteration of the loop, the condition is evaluated. If the condition
evaluates to TRUE, the sequence of statements is executed, then control resumes
at the top of the loop. If the condition evaluates to FALSE or NULL, the loop is
bypassed and control passes to the next statement. Since the condition is tested at
the top of the loop, the sequence might execute zero times.
(iv) FOR-LOOP
Whereas the number of iteration through a WHILE loop is unknown until the loop
completes, the number of iterations through a FOR loop is known before the loop
is entered. FOR loops iterate over a specified range of integers. The range is part
of an iteration scheme, which is enclosed by the keywords FOR and LOOP.
The lower bound need not be 1. However, the loop counter increment (or
decrement) must be 1. PL/SQL lets you determine the loop range dynamically at
run time, as the following example shows:
58
The loop counter is defined only within the loop. You cannot reference it outside
the loop. You need not explicitly declare the loop counter because it is implicitly
declared as a local variable of type INTEGER.
The EXIT statement allows a FOR loop to complete prematurely. You can
complete not only the current loop, but any enclosing loop.
BEGIN
...
GOTO insert_row;
...
<<insert_row>>
INSERT INTO emp VALUES ...
END;
The NULL statement explicitly specifies inaction; it does nothing other than pass
control to the next statement. It can, however, improve readability. Also, the
NULL statement is a handy way to create stubs when designing applications
from the top down.
***
59
Ex. No. 11 BASIC PL/SQL Date :
DECLARE
v_organizer_name VARCHAR2(255) := 'Organizer Name'; --
Specify the organizer name here
BEGIN
FOR event_rec IN (SELECT * FROM myapp_event WHERE
organizer = v_organizer_name) LOOP
DBMS_OUTPUT.PUT_LINE('Event Title: ' ||
event_rec.title);
DBMS_OUTPUT.PUT_LINE('Description: ' ||
event_rec.description);
-- Add more fields as needed
END LOOP;
END;
/
60
Q2) Query to get events with RSVP status for a particular user:
DECLARE
v_user_id INT := 123; -- Specify the user ID here
BEGIN
FOR event_rec IN (SELECT e.*, er.status
FROM myapp_event e
JOIN myapp_event_rsvp er ON e.id = er.event_id
WHERE er.user_id = v_user_id) LOOP
DBMS_OUTPUT.PUT_LINE('Event Title: ' || event_rec.title);
DBMS_OUTPUT.PUT_LINE('RSVP Status: ' || event_rec.status);
-- Add more fields as needed
END LOOP;
END;
Q3) Query to check if a venue is suitable for a given event based on capacity:
DECLARE
v_event_id INT := 456; -- Specify the event ID here
v_venue_id INT;
v_event_capacity INT;
v_venue_capacity INT;
BEGIN
SELECT capacity INTO v_event_capacity FROM myapp_event WHERE id =
v_event_id;
61
FROM myapp_profile u
WHERE NOT EXISTS (SELECT 1
FROM myapp_profile_rsvp_events pr
WHERE pr.profile_id = u.id)) LOOP
v_inactive_user_count := v_inactive_user_count + 1;
DBMS_OUTPUT.PUT_LINE('User ID: ' || user_rec.user_id);
-- Add more fields as needed
END LOOP;
IF v_inactive_user_count = 0 THEN
DBMS_OUTPUT.PUT_LINE('All users have RSVP''d to at least one event.');
END IF;
END;
/
CURSOR
A cursor is a temporary work area created in the system memory when a SQL
statement is executed. A cursor contains information on a select statement and the
rows of data accessed by it. This temporary work area is used to store the data
retrieved from the database, and manipulate this data. A cursor can hold more
than one row, but can process only one row at a time.
62
There are two types of cursors in PL/SQL. They are Implicit cursors and Explicit
cursors.
Implicit cursors
These are created by default when DML statements like, INSERT, UPDATE, and
DELETE statements are executed.
Oracle provides few attributes called as implicit cursor attributes to check the
status of DML operations. The cursor attributes available are %FOUND,
%NOTFOUND, %ROWCOUNT, and %ISOPEN.
For example, When you execute INSERT, UPDATE, or DELETE statements the
cursor attributes tell us whether any rows are affected and how many have been
affected.
%FOUND
The return value is TRUE, if the DML statements like INSERT, DELETE
and UPDATE affect at least one row or if SELECT ….INTO statement
return at least one row. Ex. SQL%FOUND
%NOTFOUND
The return value is FALSE, if DML statements affect at least one row or if
SELECT. …INTO statement return at least one row.
Ex. SQL%NOTFOUND
%ROWCOUNT
Return the number of rows affected by the DML operations
Ex. SQL%ROWCOUNT
Q5) Write a PL/SQL Block, to update salaries of all the employees who work in deptno
20 by 15%. If none of the employee’s salary are updated display a message 'None
of the salaries were updated'. Otherwise display the total number of employee
who got salary updated.
Declare
num number(5);
Begin
update emp set sal = sal + sal*0.15 where deptno=20;
if SQL%NOTFOUND then
dbms_output.put_line('none of the salaries were updated');
63
elsif SQL%FOUND then
num := SQL%ROWCOUNT;
dbms_output.put_line('salaries for ' || num || 'employees are updated');
end if;
End;
Explicit cursors
They must be created when you are executing a SELECT statement that returns
more than one row. Even though the cursor stores multiple records, only one
record can be processed at a time, which is called as current row. When you fetch
a row the current row position moves to next row.
▪ FETCH the data from cursor into PL/SQL variables or records in the
Execution Section.
▪ CLOSE the cursor in the Execution Section before you end the PL/SQL
Block.
Fetching Cursor :
FETCH cursor_name INTO variable-list/record-type;
%FOUND
TRUE, if fetch statement returns at least one row.
Ex. Cursor_name%FOUND
%NOTFOUND
TRUE, , if fetch statement doesn’t return a row.
Ex. Cursor_name%NOTFOUND
64
%ROWCOUNT
65
The number of rows fetched by the fetch statement.
Ex. Cursor_name%ROWCOUNT
%ISOPEN
TRUE, if the cursor is already open in the program.
Ex. Cursor_name%ISOPEN
Q6) Implicit Cursor Example: This program uses an implicit cursor to update records
and then uses the cursor attributes to display the number of affected rows.
DECLARE
v_email VARCHAR2(255);
BEGIN
UPDATE myapp_user
SET email = 'updated@example.com'
WHERE first_name = 'John';
Q7)Explicit Cursor Example: This program declares an explicit cursor to select events
from a specific category and fetches each event one by one.
DECLARE
CURSOR event_cursor IS
SELECT title, date
FROM myapp_event
WHERE category_id = 3;
v_title myapp_event.title%TYPE;
v_date myapp_event.date%TYPE;
BEGIN
OPEN event_cursor;
LOOP
FETCH event_cursor INTO v_title, v_date;
EXIT WHEN event_cursor%NOTFOUND;
CLOSE event_cursor;
66
END;
Q8) Explicit Cursor FOR LOOP Example: This program uses a cursor FOR
LOOP to simplify the fetch process. It automatically opens, fetches each
row, and closes the cursor.
DECLARE
CURSOR venue_cursor IS
SELECT name, address
FROM myapp_venue
WHERE capacity > 500;
BEGIN
FOR v_record IN venue_cursor LOOP
DBMS_OUTPUT.PUT_LINE('Venue: ' || v_record.name || ', Address: ' ||
v_record.address);
END LOOP;
END;
67
Cursor for loop
Cursor for loop automatically opens a cursor, fetches each row and closes the cursor
when all rows have been processed.
Ex.
Declare
Cursor s1 is select .. .. ..
Begin
For var in s1
Loop
68
-- statements ---
End loop;
.. .. .. .. ..
II. TRIGGER
A trigger is a PL/SQL block structure which is fired when DML statements like Insert,
Delete and Update is executed on a database table. A trigger is triggered automatically
when an associated DML statement is executed.
Syntax of Trigger
69
FOR EACH ROW
This clause is used to determine whether a trigger must fire when each row gets
affected ( i.e. a Row Level Trigger) or just once when the entire SQL statement is
executed (i.e.statement level Trigger).
WHEN (condition)
This clause is valid only for row level triggers. The trigger is fired only for rows
that satisfy the condition specified.
Types of Triggers
There are two types of triggers based on which level it is triggered.
▪ Row level trigger : An event is triggered for each row updated, inserted or deleted.
▪ Statement level trigger : An event is triggered for each SQL statement executed.
Before and After Triggers : Since triggers occur because of events, they may be set
to occur immediately before or after those events. Within the trigger, we are able to
refer old and new values involved in transactions. Old refers to the data as it existed
prior to the transaction. New refer to the data that the transaction creates.
Q11) Trigger to Audit User Registrations: This trigger creates an audit record every time a new user
registers.
CREATE TRIGGER Audit_New_Registration
AFTER INSERT ON myapp_registration
FOR EACH ROW
BEGIN
INSERT INTO registration_audit (registration_id, audit_date, action)
VALUES (NEW.id, NOW(), 'New Registration');
END;
Q12) Trigger to Validate Event Capacity: This trigger prevents an RSVP if the event is already full.
CREATE TRIGGER Check_Event_Capacity
BEFORE INSERT ON myapp_event_rsvp
FOR EACH ROW
DECLARE
v_capacity INT;
v_current_rsvps INT;
BEGIN
SELECT capacity INTO v_capacity FROM myapp_event WHERE id = NEW.event_id;
SELECT COUNT(*) INTO v_current_rsvps FROM myapp_event_rsvp WHERE event_id =
NEW.event_id;
70
END;
Q13) Trigger to Update Venue Status: This trigger updates the status of a venue to ‘Inactive’ if the
capacity is reduced below a certain threshold.
CREATE TRIGGER Update_Venue_Status
AFTER UPDATE OF capacity ON myapp_venue
FOR EACH ROW
WHEN (NEW.capacity < 100)
BEGIN
UPDATE myapp_venue
SET status = 'Inactive'
WHERE id = NEW.id;
END;
Verified by
Staff In-charge Sign : Date :
71
APPENDIX -A
CODD’S RULES
Codd’s rule provides a method for therotical evalution of a product, that claims to be a
Relational Data Base Management System.
▪ Integrity Constraints
▪ Transaction boundaries
72
Rule 7 : The Insert, Update and Delete Rule
The capability of handling a base relation or in fact a derived relation as a single operand
must hold good for all retrieve, insert, update and delete activity.
73
User access to the database via terminal monitors or application programs must remain
logically consistent whenever changes to the storage representation or access methods to
the data are changed.
74
APPENDIX -B
Q1) Find the average cost of events in a specific category:
SQL SELECT AVG(cost) FROM myapp_event WHERE category_id = (SELECT id FROM
myapp_category WHERE name = 'Conferences');
75
Q11) Find all software that generated more than 5000 in profit:
SQL>SELECT * FROM myapp_software WHERE price * sold - development_cost > 5000;
Q12) Get the ceiling value of the development cost divided by the price:
SQL>SELECT CEIL(development_cost/price) FROM myapp_software;
Q13) Select all software where sales revenue is greater than or equal to the development cost:
SQL>SELECT * FROM myapp_software WHERE price * sold >= development_cost;
76
BIBLIOGRAPHY
The following books and manuals were referred during the preparation of this work book
and suggested for further reading
▪ Date C.J, “An Introduction to Database”, 8th Edition , 2003, Addison-Wesley Pub
Co, ISBN: 978-0321197849
77