0% found this document useful (0 votes)
10 views5 pages

Views, Triggers and Stored Procedures-1

Uploaded by

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

Views, Triggers and Stored Procedures-1

Uploaded by

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

--Muiz Ul Islam Khan 22L-6944

--Mustafa Sajid 22L-6621


--Saim Naseer 22L-6593
--DELIVERABLE 2

---VIEWS

use Moviehub

--1.
CREATE VIEW Movies_actor_names as
Select M.MovieID, M.Title, A.Names
From Artists as A, Movie as M
Where M.LeadingID=A.ArtistID

--Drop Movies_actor_names

--2.
CREATE VIEW rate_review as
Select U.UserName, URW.ReviewText, UR.Rating
From Users as U, UserReview as URW, UserRating as UR
Where U.UserID=URW.UserID AND U.UserID=UR.UserID AND URW.MovieID=UR.MovieID

--3
CREATE VIEW RMBO as ---RecommendedMovieIDboxoffice
Select R.*, BO.*
From BoxOffice as BO, Recommendation as R
WHERE BO.MovieID=RecommendedMovieID

--4
CREATE VIEW ActorBO as ---ACTORS AND THEIR BOX OFFICES TOTAL REVENUE
Select A.Names, A.ArtistID, M.MovieID, BO.TotalRevenue
From Artists as A, BoxOffice as BO, Movie as M
Where M.LeadingID = A.ArtistID AND M.MovieID = BO.MovieID

--5
CREATE VIEW User_interaction as --this will have email and profile info of the
user who is leaving a comment. i.e the "content" column must have "comment" as a
value.
Select U.UserID, U.Email, U.ProfileInfo, SI.Content
From Users AS U, SocialInteraction as SI
Where SI.InteractionType = 'comment'

-----------------------------------------------------------------------------

--TRIGGERS:

--1
CREATE TRIGGER CheckFiveStarReviews
ON UserRating
AFTER INSERT, UPDATE
AS
BEGIN
DECLARE @MovieID INT;
DECLARE @FiveStarCount INT;
DECLARE @MovieTitle VARCHAR(50);

-- Get the MovieID of the affected movie


SELECT @MovieID = MovieID
FROM inserted;

-- Count the number of 5-star reviews for the affected movie


SELECT @FiveStarCount = COUNT(*)
FROM UserRating
WHERE MovieID = @MovieID AND Rating = 5;

-- Get the title of the affected movie


SELECT @MovieTitle = Title
FROM Movie
WHERE MovieID = @MovieID;

-- Check if the number of 5-star reviews is greater than or equal to 100,000


IF @FiveStarCount >= 100000
BEGIN
-- Perform actions here, such as sending a notification or updating a flag in
the Movie table
PRINT 'The movie "' + @MovieTitle + '" with ID ' + CONVERT(VARCHAR(10),
@MovieID) + ' has received more than 100,000 5-star reviews!';
END
END;

-------------------------------------
--2
CREATE TRIGGER MostActiveUserTrigger
ON UserReview
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
DECLARE @MostActiveUserID INT;
DECLARE @MostActiveUserName VARCHAR(20);

-- Using a CTE to calculate the aggregated review counts


;WITH ReviewCounts AS (
SELECT UR.UserID, U.UserName, COUNT(*) AS ReviewCount
FROM UserReview UR
JOIN Users U ON UR.UserID = U.UserID
GROUP BY UR.UserID, U.UserName
HAVING COUNT(*) >= 50 -- Only users with at least 50 reviews
)
-- Get the UserID of the user with the most reviews
SELECT TOP 1 @MostActiveUserID = UserID, @MostActiveUserName = UserName
FROM ReviewCounts
ORDER BY ReviewCount DESC;

-- Check if a user with at least 50 reviews was found


IF @MostActiveUserID IS NOT NULL
BEGIN
-- Insert into a log table
INSERT INTO MostActiveUserLog (UserID, UserName, ReviewCount, LogDateTime)
VALUES (@MostActiveUserID, @MostActiveUserName, (SELECT COUNT(*) FROM
UserReview WHERE UserID = @MostActiveUserID), GETDATE());
END
END;
-------------------------------------------------------

--3
CREATE TRIGGER DeleteMovieOnBadReviews
ON UserRating
AFTER INSERT
AS
BEGIN
DECLARE @MovieID INT;
DECLARE @OneStarCount INT;

-- Get the MovieID of the affected movie


SELECT @MovieID = MovieID
FROM inserted;

-- Count the number of 1-star reviews for the affected movie


SELECT @OneStarCount = COUNT(*)
FROM UserRating
WHERE MovieID = @MovieID AND Rating = 1;

-- Check if the number of 1-star reviews is greater than or equal to 100,000


IF @OneStarCount >= 100000
BEGIN
-- Log the deletion event into a separate table
INSERT INTO DeletedMoviesLog (MovieID, DeletionReason, DeletionDateTime)
VALUES (@MovieID, 'Excessive 1-star reviews', GETDATE());

-- Delete the movie from the Movie table


DELETE FROM Movie
WHERE MovieID = @MovieID;

-- Print a message indicating that the movie has been deleted (optional)
PRINT 'The movie with ID ' + CONVERT(VARCHAR(10), @MovieID) + ' has received
100,000 1-star reviews and has been deleted!';
END
END;

--------------------------
--4
CREATE TABLE MoviesLastMonth (
MovieID INT,
Title VARCHAR(100),
ReleaseDate DATE
);

CREATE TRIGGER DisplayMoviesLastMonth


ON Movie
AFTER INSERT, UPDATE
AS
BEGIN
-- Check for movies with release date in the last month and insert into
MoviesLastMonth table
INSERT INTO MoviesLastMonth (MovieID, Title, ReleaseDate)
SELECT MovieID, Title, ReleaseDate
FROM inserted
WHERE DATEDIFF(DAY, ReleaseDate, GETDATE()) <= 30; -- Movies released within the
last 30 days
END;

CREATE TRIGGER DeleteOldMovies


ON Movie
AFTER INSERT, UPDATE
AS
BEGIN
-- Delete movies with release date more than 30 days from today
DELETE FROM Movie
WHERE DATEDIFF(DAY, ReleaseDate, GETDATE()) > 30;
END;

-------------------------
--5

CREATE TRIGGER NotifyUsersOnAwardMovie


ON Movie
AFTER INSERT, UPDATE
AS
BEGIN
DECLARE @MovieTitle VARCHAR(50);
DECLARE @Recipients VARCHAR(MAX);
DECLARE @MailSubject NVARCHAR(255);
DECLARE @MailBody NVARCHAR(MAX);

-- Check if any movie with an award has been inserted


IF EXISTS (SELECT * FROM inserted WHERE Awards IS NOT NULL)
BEGIN
-- Get the title of the inserted movie
SELECT @MovieTitle = Title
FROM inserted;

-- Get the email addresses of all users


SELECT @Recipients = COALESCE(@Recipients + ',', '') + Email
FROM Users;

-- Construct email subject


SET @MailSubject = 'New Award-Winning Movie Added';

-- Construct email body


SET @MailBody = 'A new award-winning movie (' + @MovieTitle + ') has been
added. Check it out!';

-- Send email to all users


EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'YourMailProfile', -- Replace 'YourMailProfile' with the
name of your Database Mail profile
@recipients = @Recipients,
@subject = @MailSubject,
@body = @MailBody;
END
END;

--STORED PROCEDURES:

--procedures

--1 selects all actors

Create Procedure SelectAllActor as


Select* From Artists

--2 selects all movies with a specified actor NAME


CREATE PROCEDURE SelectAllMovieswithActorX
@ActorNames VARCHAR(30)
AS
BEGIN
SELECT M.Title, M.MovieID, A.Names
FROM Movie AS M
INNER JOIN Artists AS A ON M.LeadingID = A.ArtistID
WHERE A.Names LIKE '%' + @ActorNames + '%';
END;

--------------------------------------------
--3 selects all movies with a opening week revenue over specified value(moni)
CREATE PROCEDURE SelectAllHitMovies @moni integer AS
BEGIN
Select M.Title, BO.OpeningWeekGross
From Movie as M, BoxOffice as BO
Where BO.OpeningWeekGross > @moni
END;
---------------------------------
--4 finds a specified users rating of a specified movieID
CREATE PROCEDURE SpecifiedUserRatingofMovie
@movieID INT,
@userID INT
AS
BEGIN
SELECT Rating
FROM UserRating
WHERE MovieID = @movieID AND UserID = @userID;
END;
---------------------------------
--5
CREATE PROCEDURE SelectallREVIEWSofamovie
@movieid INT
AS
BEGIN
SELECT M.Title, M.MovieID, URW.UserID, URW.ReviewID, URW.ReviewText
FROM Movie AS M
INNER JOIN UserReview AS URW ON M.MovieID = URW.MovieID
WHERE M.MovieID = @movieid;
END;

---end
--example executions
--1.
Exec SelectAllActor
--2
Exec SelectAllMovieswithActorX @ActorNames='mahr'
--3
Exec SelectAllHitMovies @moni=5000000
--4
Exec SpecifiedUserRatingofMovie @movieID=1234, @userID = 123
--5
EXEC SelectallREVIEWSofamovie @movieid=12345

You might also like