0% found this document useful (0 votes)
8 views42 pages

223 Final Lecture - 2024

The document discusses project demonstrations for a class including details about deliverables, tests, and topics to be covered. The topics include views, stored procedures, triggers, and case statements with more details on case statements. Examples are provided for each topic.

Uploaded by

keyk1098
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views42 pages

223 Final Lecture - 2024

The document discusses project demonstrations for a class including details about deliverables, tests, and topics to be covered. The topics include views, stored procedures, triggers, and case statements with more details on case statements. Examples are provided for each topic.

Uploaded by

keyk1098
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Project Demonstrations

Prac Test 2
More Case Statements
Project Deliverable 2
• Part 1 (3%) due 31 May
• Part 2 (12%) due 7th June
• Demonstration of D2 part 2 in labs
Prac Test 2
• Mock test is on Moodle
• 5th June
• 20% of final mark
• 105 min test
• Open book test
• SQL tables and test data provided
Test Topics
• Views
• Stored Procedures
• Triggers
• Case Statements
• More on these today
Views
• Creating View by joining multiple tables
Views
• Creating View by joining multiple tables

1. Which tables to join?


2. Where clauses (‘hamilton’, ‘cat’)
Views
• Creating View by joining multiple tables

1. Which tables to joine?


2. Where clauses (like ‘%Porcine%’ )

Note: app is appointment ID


Views
• Creating View by joining multiple tables and view

1. Which tables to join?


Stored Procedures
• Creating a stored procedure
• Executing a stored procedure
• Local variables
• Input and Output Parameters
• Error handling
• converting data types using str() or convert()
Stored Procedures

1. How many input parameters? (1)


2. How many checks? (2)
3. output messages (1)
CREATE PROC general @param varchar(24) = NULL
AS
Stored Procedures
-- No param supplied
find a good template and use it
IF @param IS NULL BEGIN
PRINT 'param is null'
RETURN -1
END
-- param is supplied but does not exist in database
IF NOT EXISTS (SELECT * FROM table_name WHERE column_name = @param) BEGIN
PRINT ‘Nothing with param ‘ + @param -- str() for int type
RETURN -2
-- param supplied and exists in database
END ELSE BEGIN
Declare @count int
SELECT @count = count(column_name1) FROM table_name …, join …. on … WHERE column_name = @param
PRINT str(@count) + ’ is found’
END
RETURN
CREATE PROC general @param varchar(30) = NULL
AS
Stored Procedures
-- No param supplied
IF @param IS NULL BEGIN
PRINT ‘Please enter the name of a registered animal'
RETURN -1
END
-- param is supplied but does not exist in database
IF NOT EXISTS (SELECT * FROM animal WHERE name = @param) BEGIN
PRINT @param + ‘ is not a registered name’ -- str() for int type
RETURN -2
-- param supplied and exists in database
END ELSE BEGIN
Declare @count int
SELECT @count = count(column_name1) FROM table_name …, join …. on … WHERE column_name = @param
PRINT str(@count) + ’ is found’
END
RETURN
CREATE PROC general @param varchar(30) = NULL
AS
Stored Procedures
-- No param supplied
IF @param IS NULL BEGIN
PRINT ‘Please enter the name of a registered animal'
RETURN -1
END
-- param is supplied but does not exist in database
IF NOT EXISTS (SELECT * FROM animal WHERE name = @param) BEGIN
PRINT @param + ‘ is not a registered name’ -- str() for int type
RETURN -2
-- param supplied and exists in database
END ELSE BEGIN
Declare @count int
SELECT @count = count(column_name1) FROM table_name …, join …. on … WHERE column_name = @param
PRINT @param + ‘ has ‘ + str(@count) + ’ appointment(s).’
END
RETURN
Stored Procedures 1. How many input parameters? (1)
2. How many checks? (2)
3. output messages: a table of query results
CREATE PROC general @param date = NULL
AS
-- No param supplied
IF @param IS NULL BEGIN
PRINT ‘please enter a date in the range of the schedule'
RETURN -1
END
-- param is supplied but does not exist in database
IF NOT EXISTS (SELECT * FROM scheduleSlot WHERE dayDate = @param) BEGIN
PRINT @param + ‘ is outside of this schedule‘ -- str() for int type
RETURN -2
-- param supplied and exists in database
END ELSE BEGIN
Declare @count int
SELECT @count = count(column_name1) FROM table_name …, join …. on … WHERE column_name = @param
PRINT str(@count) + ’ is found’
END
RETURN
CREATE PROC general @param date = NULL
AS
-- No param supplied
IF @param IS NULL BEGIN
PRINT ‘please enter a date in the range of the schedule'
RETURN -1
END
-- param is supplied but does not exist in database
IF NOT EXISTS (SELECT * FROM scheduleSlot WHERE dayDate = @param) BEGIN
PRINT @param + ‘ is outside of this schedule‘ -- str() for int type
RETURN -2
-- param supplied and exists in database
END ELSE BEGIN
SELECT column …. FROM table_name …, join …. on … WHERE column_name = @param
order by column …
END
RETURN
Triggers
CREATE TRIGGER trigger_name
• Creating a trigger ON table_name
ON|AFTER { INSERT | UPDATE | DELETE }
• Using the trigger
AS SQL statements
• Safe tests

Begin transaction
test statements {INSERT | UPDATE | DELETE }
Rollback transaction
CREATE TRIGGER general ON table FOR|AFTER Operation
AS
DECLARE @RC int = @@ROWCOUNT
IF @RC = 0
RETURN -- nothing happened (efficient)
IF @RC = 1
BEGIN
-- Single row operation
END
ELSE
BEGIN
-- Multi-row operation
END
CREATE TRIGGER general ON table FOR|AFTER Operation
AS
DECLARE @RC int = @@ROWCOUNT
IF @RC = 0
RETURN -- nothing happened (efficient)
IF EXISTS|NOT EXISTS (SELECT * FROM tablename WHERE … ) BEGIN
RAISERROR('You cannot do this ….',16,1)
ROLLBACK TRAN
END
IF EXISTS|NOT EXISTS (SELECT * FROM tablename WHERE … ) BEGIN
RAISERROR('You cannot do this too ….',16,1)
ROLLBACK TRAN
END
….
RETURN
19
CREATE TRIGGER ThirtyMin ON scheduleSlot After update, insert
AS
DECLARE @RC int = @@ROWCOUNT
IF @RC = 0
RETURN -- nothing happened (efficient)
IF EXISTS|NOT EXISTS (SELECT * FROM tablename WHERE … ) BEGIN
RAISERROR('You cannot do this ….',16,1)
ROLLBACK TRAN
END
IF EXISTS|NOT EXISTS (SELECT * FROM tablename WHERE … ) BEGIN
RAISERROR('You cannot do this too ….',16,1)
ROLLBACK TRAN
END
….
RETURN
20
two conditions
(1) must be half an hour long
CREATE TRIGGER ThirtyMin ON scheduleSlot After update, insert (2) start on hour or at half past
AS
DECLARE @RC int = @@ROWCOUNT
IF @RC = 0
RETURN -- nothing happened (efficient)
IF EXISTS|NOT EXISTS (SELECT * FROM tablename WHERE … ) BEGIN
RAISERROR('You cannot do this ….',16,1)
ROLLBACK TRAN
END
IF EXISTS|NOT EXISTS (SELECT * FROM tablename WHERE … ) BEGIN
RAISERROR('You cannot do this too ….',16,1)
ROLLBACK TRAN
END
….
RETURN
21
CREATE TRIGGER ThirtyMin ON scheduleSlot After update, insert
AS
DECLARE @RC int = @@ROWCOUNT
IF @RC = 0
RETURN -- nothing happened (efficient)
IF EXISTS (SELECT appointment_id FROM INSERTED
WHERE DATEDIFF(MINUTE, startTime, endTime) != 30 ) BEGIN
RAISERROR('This time slot is not thirty minutes long',16,1)
ROLLBACK TRAN
END

IF EXISTS|NOT EXISTS (SELECT * FROM tablename WHERE … ) BEGIN


RAISERROR('You cannot do this too ….',16,1)
ROLLBACK TRAN
END
…. 22
RETURN
%:00:00: start on the hour
%:30:00: start at half past

CREATE TRIGGER ThirtyMin ON scheduleSlot After update, insert


AS
DECLARE @RC int = @@ROWCOUNT
IF @RC = 0
RETURN -- nothing happened (efficient)
IF EXISTS (SELECT appointment_id FROM INSERTED
WHERE DATEDIFF(MINUTE, startTime, endTime) != 30 ) BEGIN
RAISERROR('This time slot is not thirty minutes long',16,1)
ROLLBACK TRAN
END
IF EXISTS (SELECT appointment_id FROM INSERTED
WHERE NOT (startTime LIKE ’%:00:00’ OR startTime LIKE ’%:30:00’) ) BEGIN
RAISERROR(‘This time slot does not start at a valid time’, 16,1)
ROLLBACK TRAN
END
….
RETURN 23
CREATE TRIGGER ThirtyMin ON scheduleSlot After update, insert
Write 3 safe tests for question 6. AS
DECLARE @RC int = @@ROWCOUNT
1. One safe test must insert a schedule slot at a correct IF @RC = 0
start time and length RETURN -- nothing happened (efficient)
IF EXISTS (SELECT appointment_id FROM INSERTED
2. Test 2 must not fire. The second safe test must attempt WHERE DATEDIFF(MINUTE, startTime, endTime) != 30 ) BEGIN
to update a timeSlot to an incorrect start time so the RAISERROR('This time slot is not thirty minutes long',16,1)
ROLLBACK TRAN
trigger must fire. END
3. The third safe test must attempt to insert a timeSlot IF EXISTS (SELECT appointment_id FROM INSERTED
WHERE NOT (startTime LIKE ’%:00:00’ OR startTime LIKE ’%:30:00’ )) BEGIN
with an incorrect length so the trigger must fire. RAISERROR(‘This time slot does not start at a valid time’, 16,1)
ROLLBACK TRAN
END
….
RETURN

Test 1
Begin Transaction
INSERT INTO scheduleSlot(room, dayDate, startTime, endTime) VALUES (1, ‘2024-5-27', '8:00', '8:30');
Rollback Transaction
24
CREATE TRIGGER ThirtyMin ON scheduleSlot After update, insert
Write 3 safe tests for question 6. AS
DECLARE @RC int = @@ROWCOUNT
1. One safe test must insert a schedule slot at a correct IF @RC = 0
start time and length RETURN -- nothing happened (efficient)
IF EXISTS (SELECT appointment_id FROM INSERTED
2. Test 2 must not fire. The second safe test must attempt WHERE DATEDIFF(MINUTE, startTime, endTime) != 30 ) BEGIN
to update a timeSlot to an incorrect start time so the RAISERROR('This time slot is not thirty minutes long',16,1)
ROLLBACK TRAN
trigger must fire. END
3. The third safe test must attempt to insert a timeSlot IF EXISTS (SELECT appointment_id FROM INSERTED
WHERE NOT (startTime LIKE ’%:00:00’ OR startTime LIKE ’%:30:00’ )) BEGIN
with an incorrect length so the trigger must fire. RAISERROR(‘This time slot does not start at a valid time’, 16,1)
ROLLBACK TRAN
END
….
RETURN

select * from scheduleSlot


Test 2
Begin Transaction
Update scheduleSlot
Set startTime = ’08:10’ where slot_id = 1
Rollback Transaction 25
CREATE TRIGGER ThirtyMin ON scheduleSlot After update, insert
Write 3 safe tests for question 6. AS
DECLARE @RC int = @@ROWCOUNT
1. One safe test must insert a schedule slot at a correct IF @RC = 0
start time and length RETURN -- nothing happened (efficient)
IF EXISTS (SELECT appointment_id FROM INSERTED
2. Test 2 must not fire. The second safe test must attempt WHERE DATEDIFF(MINUTE, startTime, endTime) != 30 ) BEGIN
to update a timeSlot to an incorrect start time so the RAISERROR('This time slot is not thirty minutes long',16,1)
ROLLBACK TRAN
trigger must fire. END
3. The third safe test must attempt to insert a timeSlot IF EXISTS (SELECT appointment_id FROM INSERTED
WHERE Not (startTime LIKE ’%:00:00’ OR startTime LIKE ’%:30:00’ ) )BEGIN
with an incorrect length so the trigger must fire. RAISERROR(‘This time slot does not start at a valid time’, 16,1)
ROLLBACK TRAN
END
….
RETURN

Test 3
Begin Transaction
INSERT INTO scheduleSlot(room, dayDate, startTime, endTime) VALUES (2, ‘2024-5-28', '8:00', ‘8:40');
Rollback Transaction
26
CREATE TRIGGER SnakeVet ON scheduleSlot After update
AS
DECLARE @RC int = @@ROWCOUNT
IF @RC = 0
RETURN -- nothing happened (efficient)
IF EXISTS|NOT EXISTS (SELECT * FROM tablename WHERE … ) BEGIN
RAISERROR('You cannot do this ….',16,1)
ROLLBACK TRAN
END
IF EXISTS|NOT EXISTS (SELECT * FROM tablename WHERE … ) BEGIN
RAISERROR('You cannot do this too ….',16,1)
ROLLBACK TRAN
END
….
RETURN 27
CREATE TRIGGER SnakeVet ON scheduleSlot After update
AS
DECLARE @RC int = @@ROWCOUNT
IF @RC = 0
RETURN -- nothing happened (efficient)
IF NOT EXISTS (SELECT appointment_id FROM
FROM inserted, appointment, animal
WHERE inserted.appointment_id = appointment. appointment_id
and appointment.animal_id = animal.animal_id
and category = 'snake’)
RETURN -- do nothing for non snake (efficient)

RETURN

28
CREATE TRIGGER SnakeVet ON scheduleSlot After update
AS
DECLARE @RC int = @@ROWCOUNT
IF @RC = 0
RETURN -- nothing happened (efficient)
IF (SELECT appointment_id FROM INSERTED) NOT IN (
select appointment_id FROM appointment, animal
WHERE appointment.animal_id = animal.animal_id
and category = 'snake’)
RETURN -- do nothing for non snake (efficient)

RETURN

29
CREATE TRIGGER SnakeVet ON scheduleSlot After update
AS
DECLARE @RC int = @@ROWCOUNT
IF @RC = 0
RETURN -- nothing happened (efficient)
IF NOT EXISTS (SELECT appointment_id FROM …)
RETURN -- do nothing for non snake (efficient)

IF (select staff_id from INSERTED) NOT IN (select staff_id from staff


where role like ‘%serpentine%’) BEIGN
RAISERROR(‘This is not a snake vet.’, 16,1)
ROLLBACK TRAN
END
RETURN
30
select appointment_id
from appointment
where animal_id NOT IN (select animal_id
from animal where category = 'snake')

Test 1
Begin Transaction
Update scheduleSlot
Set appointment_id = 10 where slot_id = 3
Rollback Transaction 31
select appointment_id
from appointment
where animal_id IN (select animal_id
from animal where category = 'snake')

select slot_id
from scheduleSlot
Test 2 where staff_id IN (select staff_id
Begin Transaction from staff where role like ‘%serpentine%')
Update scheduleSlot
Set appointment_id = 19 where slot_id = 41
Rollback Transaction 32
select appointment_id
from appointment
where animal_id IN (select animal_id
from animal where category = 'snake')

select slot_id
from scheduleSlot
Test 3 where staff_id IN (select staff_id
Begin Transaction from staff where role like ‘%serpentine%')
Update scheduleSlot
Set appointment_id = 19 where slot_id = 3
Rollback Transaction 33
Case Statements
CASE
WHEN Boolean_expression THEN result_expression
WHEN Boolean_expression THEN result_expression
ELSE result_expression
END

34
Case Statement Example
CASE
WHEN price = 0 THEN ‘Free’
WHEN price < 10 THEN ‘Cheap’
ELSE ‘Expensive’
END

35
Case Statement Select Example
SELECT productName, “Cost” =
CASE
WHEN price = 0 THEN ‘Free’
WHEN price < 10 THEN ‘Cheap’
ELSE ‘Expensive’
END
FROM products

36
Making choices using CASE
GO
SELECT Title, borrowed =
CASE
WHEN count(MoveID) = 0 THEN 'has never been borrowed'
WHEN count(MoveID) = 1 THEN 'has been borrowed once'
ELSE 'has been borrowed lots of times'
END
FROM Book left outer join Movement on Book.BookID = Movement.BookID
group by Book.BookID, Title
GO

37
38
SELECT st.fName, st.lName, an.category
FROM staff st join schedulSlot ss on st.staff_id = ss.staff_id
join appointment app on ss.appointment_id = app.appointment_id
join animal an on app.animal_id = an.animal_id
WHERE st.fName = 'Phil' and st.lName = 'Treweek'

39
SELECT st.fName, st.lName,
Cow = (case when an.category = 'cow' then 1 ELSE 0 END),
Sheep = (case when an.category = 'sheep' then 1 ELSE 0 END),
Pig = (case when an.category = 'Pig' then 1 ELSE 0 END),
Fish = (case when an.category = 'Fish' then 1 ELSE 0 END)
FROM staff st join scheduleSlot ss on st.staff_id = ss.staff_id
join appointment app on ss.appointment_id = app.appointment_id
join animal an on app.animal_id = an.animal_id
where st.fName = 'Phil' and st.lName = 'Treweek'
40
SELECT st.fName, st.lName,
Cow = sum(case when an.category = 'cow' then 1 ELSE 0 END),
Sheep = sum(case when an.category = 'sheep' then 1 ELSE 0 END),
Pig = sum(case when an.category = 'Pig' then 1 ELSE 0 END),
Fish = sum(case when an.category = 'Fish' then 1 ELSE 0 END)
FROM staff st join scheduleSlot ss on st.staff_id = ss.staff_id
join appointment app on ss.appointment_id = app.appointment_id
join animal an on app.animal_id = an.animal_id
where st.fName = 'Phil' and st.lName = 'Treweek'
group by st.staff_id, st.fName, st.lName 41
Any Questions?

42

You might also like