0% found this document useful (0 votes)
44 views

Lab 5

This document contains code for creating multiple packages in Oracle to manage movie data. The first package defines functions to get a movie by ID and add a new movie. The second package defines functions to get genres for a movie and add a new genre. The third package defines functions to get expensive movies over a budget and filter movies by runtime. Tables are created to store movie, genre, and other related data, and data is inserted. The packages are used to retrieve, add, and filter movie data.

Uploaded by

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

Lab 5

This document contains code for creating multiple packages in Oracle to manage movie data. The first package defines functions to get a movie by ID and add a new movie. The second package defines functions to get genres for a movie and add a new genre. The third package defines functions to get expensive movies over a budget and filter movies by runtime. Tables are created to store movie, genre, and other related data, and data is inserted. The packages are used to retrieve, add, and filter movie data.

Uploaded by

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

CREATE SEQUENCE movie_seq

START WITH 500


INCREMENT BY 1
NOCACHE
NOCYCLE;

---- 1 package ----


CREATE OR REPLACE PACKAGE movie_package AS
FUNCTION get_movie(p_movie_id IN movie.movie_id%TYPE)
RETURN movie%ROWTYPE;

PROCEDURE add_movie(p_title IN movie.title%TYPE, p_budget IN movie.budget%TYPE,


p_overview IN movie.overview%TYPE, p_release_date IN
movie.release_date%TYPE,
p_runtime IN movie.runtime%TYPE);

END movie_package;

CREATE OR REPLACE PACKAGE BODY movie_package AS


FUNCTION get_movie(p_movie_id IN movie.movie_id%TYPE)
RETURN movie%ROWTYPE AS
v_movie movie%ROWTYPE;
BEGIN
SELECT *
INTO v_movie
FROM movie
WHERE movie_id = p_movie_id;
RETURN v_movie;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Movie not found.');
RETURN NULL;
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error retrieving movie: ' || SQLERRM);
RETURN NULL;
END get_movie;

PROCEDURE add_movie(p_title IN movie.title%TYPE, p_budget IN movie.budget%TYPE,


p_overview IN movie.overview%TYPE, p_release_date IN
movie.release_date%TYPE,
p_runtime IN movie.runtime%TYPE) AS
BEGIN
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
VALUES (movie_seq.nextval, p_title, p_budget, p_overview, p_release_date,
p_runtime);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error adding movie: ' || SQLERRM);
END add_movie;
END movie_package;

----Using-----
DECLARE
v_movie_id movie.movie_id%TYPE := 100;
v_movie movie%ROWTYPE;
BEGIN
v_movie := movie_package.get_movie(v_movie_id);

--DBMS_OUTPUT.PUT_LINE('Movie ID: ' || v_movie.movie_id);


DBMS_OUTPUT.PUT_LINE('Title: ' || v_movie.title);
DBMS_OUTPUT.PUT_LINE('Budget: ' || v_movie.budget);
DBMS_OUTPUT.PUT_LINE('Overview: ' || v_movie.overview);
DBMS_OUTPUT.PUT_LINE('Release Date: ' || v_movie.release_date);
DBMS_OUTPUT.PUT_LINE('Runtime: ' || v_movie.runtime);

movie_package.add_movie(
'New Movie', 1000000, 'This is a new movie', '20-NOV-21', 120
);
END;

----2 package ------------------------------------


CREATE OR REPLACE PACKAGE movie_pkg AS
TYPE gen_table IS TABLE OF genre.genre_name%TYPE INDEX BY BINARY_INTEGER;

FUNCTION get_movie_genre(mov_id IN NUMBER) RETURN gen_table;


PROCEDURE add_genre(gen_id IN NUMBER, gen_name IN VARCHAR2);
END movie_pkg;

CREATE OR REPLACE PACKAGE BODY movie_pkg AS


FUNCTION get_movie_genre(mov_id IN NUMBER) RETURN gen_table AS
gen_names gen_table;
gen_id genre.genre_id%TYPE := 1002;
BEGIN
FOR i IN 1..10 LOOP
SELECT genre_name
INTO gen_names(i)
FROM genre
WHERE genre_id = gen_id;
gen_id := gen_id + 1;
END LOOP;

RETURN gen_names;
END get_movie_genre;

PROCEDURE add_genre(gen_id IN NUMBER, gen_name IN VARCHAR2) AS


BEGIN
INSERT INTO genre (genre_id, genre_name)
VALUES (gen_id, gen_name);
END add_genre;
END movie_pkg;

----Using-----
DECLARE
genres movie_pkg.gen_table;
BEGIN
genres := movie_pkg.get_movie_genre(1002);
FOR i IN 1..genres.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('Genre ' || i || ': ' || genres(i));
END LOOP;
END;

BEGIN
movie_pkg.add_genre(2000, 'Romance');
DBMS_OUTPUT.PUT_LINE('Genre added successfully');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;

--- 3 package ----


CREATE OR REPLACE PACKAGE movie_filter_pkg AS
FUNCTION get_expensive_movies(budget_in IN NUMBER) RETURN SYS_REFCURSOR;
PROCEDURE filter_movies_by_runtime(runtime_in IN NUMBER);
END movie_filter_pkg;

CREATE OR REPLACE PACKAGE BODY movie_filter_pkg AS


FUNCTION get_expensive_movies(budget_in IN NUMBER) RETURN SYS_REFCURSOR AS
movie_cur SYS_REFCURSOR;
BEGIN
OPEN movie_cur FOR
SELECT * FROM movie WHERE budget > budget_in;
RETURN movie_cur;
END get_expensive_movies;

PROCEDURE filter_movies_by_runtime(runtime_in IN NUMBER) AS


BEGIN
FOR movie_table IN (SELECT * FROM movie WHERE runtime > runtime_in)
LOOP
DBMS_OUTPUT.PUT_LINE(movie_table.title || ' has a runtime of ' ||
movie_table.runtime || ' minutes.');
END LOOP;
END filter_movies_by_runtime;
END movie_filter_pkg;
------
--Using--

DECLARE
movie_cur SYS_REFCURSOR;
movie_rec movie%ROWTYPE;
BEGIN
movie_cur := movie_filter_pkg.get_expensive_movies(50000);
LOOP
FETCH movie_cur INTO movie_rec;
EXIT WHEN movie_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(movie_rec.title || ' has a budget of ' ||
movie_rec.budget);
END LOOP;
CLOSE movie_cur;
END;

BEGIN
movie_filter_pkg.filter_movies_by_runtime(120);
END;

-----Creating tables and inserting data----------------


CREATE TABLE movie (
movie_id NUMBER(10) NOT NULL,
title varchar(1000) DEFAULT NULL,
budget NUMBER(10) DEFAULT NULL,
overview varchar2(1000) DEFAULT NULL,
release_date date DEFAULT NULL,
runtime NUMBER(5) DEFAULT NULL,
PRIMARY KEY (movie_id)
);
CREATE TABLE genre (
genre_id NUMBER(10) NOT NULL,
genre_name varchar2(100) DEFAULT NULL,
PRIMARY KEY (genre_id)
);
CREATE TABLE movie_genres (
movie_id NUMBER(10) DEFAULT NULL,
genre_id NUMBER(10) DEFAULT NULL,
CONSTRAINT fk_mg_genre FOREIGN KEY (genre_id) REFERENCES genre (genre_id),
CONSTRAINT fk_mg_movie FOREIGN KEY (movie_id) REFERENCES movie (movie_id)
);
CREATE TABLE gender (
gender_id NUMBER(10) NOT NULL,
gender varchar2(20) DEFAULT NULL,
PRIMARY KEY (gender_id)
);
CREATE TABLE language (
language_id NUMBER(10) NOT NULL,
language_name varchar2(500) DEFAULT NULL,
PRIMARY KEY (language_id)
);
CREATE TABLE country (
country_id NUMBER(10) NOT NULL,
country_name varchar2(200) DEFAULT NULL,
PRIMARY KEY (country_id)
);
CREATE TABLE person (
person_id NUMBER(10) NOT NULL,
person_name varchar2 (500) DEFAULT NULL,
PRIMARY KEY (person_id)
);
CREATE TABLE movie_cast (
movie_id NUMBER(10) DEFAULT NULL,
person_id NUMBER(10) DEFAULT NULL,
character_name varchar2(400) DEFAULT NULL,
gender_id NUMBER(10) DEFAULT NULL,
CONSTRAINT fk_mca_gender FOREIGN KEY (gender_id) REFERENCES gender (gender_id),
CONSTRAINT fk_mca_movie FOREIGN KEY (movie_id) REFERENCES movie (movie_id),
CONSTRAINT fk_mca_per FOREIGN KEY (person_id) REFERENCES person (person_id)
);
CREATE TABLE production_country (
movie_id NUMBER(10) DEFAULT NULL,
country_id NUMBER(10) DEFAULT NULL,
CONSTRAINT fk_pc_country FOREIGN KEY (country_id) REFERENCES country
(country_id),
CONSTRAINT fk_pc_movie FOREIGN KEY (movie_id) REFERENCES movie (movie_id)
);

INSERT INTO movie (movie_id, title, budget, overview, release_date, runtime) VALUES
(5,'Avengers: Infinity War',4000000,'American superhero film based on the Marvel
Comics','27-APR-18',149);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(1, 'Avatar 2', 50000, 'movie about blue people', '16-DEC-22', 192);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(285, 'Pirates of the Caribbean: The Curse of the Black Pearl', 700000,
'movie about pirates', '22-AUG-03', 143);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(100, 'Fast & Furious Presents: Hobbs & Shaw', 700000, ' American buddy
action comedy film directed by David Leitch', '02-AUG-19', 135);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(35, 'Sherlock Holmes', 200000, 'Detective Sherlock Holmes and his stalwart
partner Watson engage in a battle of wits and brawn with a nemesis whose plot is a
threat to all of England.', '15-JUN-19', 128);

INSERT INTO movie (movie_id, title, budget, overview, release_date, runtime) VALUES
(7,'The Nun',3500000,'American gothic supernatural horror film ','20-NOV-18',95);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(2, 'Annabelle: Creation', 704000, 'Years after the tragic death of their
little daughter, a doll- maker and his wife welcome a nun and a group of orphaned
girls to their home, but somehow they become the target of the doll-makers demonic
creation, Annabelle.', '10-AUG-17', 109);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(207, 'Annabelle', 620000, 'movie about the evil doll', '22-AUG-13', 143);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(26, 'The Conjuring', 390000, 'Paranormal investigators Ed and Lorraine
Warren work to help a family terrorized by a dark presence.', '05-DEC-13', 135);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(666, 'Annabelle Comes Home', 450000, 'Determined to keep Annabelle from
wreaking more havoc, demonologists Ed and Lorraine Warren bring the possessed doll
to the locked artifacts room in their home, placing her “safely” behind sacred
glass and enlisting a priest’s holy blessing. But an unholy night of horror awaits
as Annabelle awakens the evil spirits in the room, who all set their sights on a
new target—the Warrens’ ten year- old daughter, Judy, and her friends.', '26-JUN-
19', 128);

INSERT INTO movie (movie_id, title, budget, overview, release_date, runtime) VALUES
(8,'The Guilty',350000,'Netflix','20-NOV-21',90);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(3, 'The Deep House', 704000, 'English-language French supernatural horror
film written and directed by Julien Maury and Alexandre Bustillo.', '10-AUG-21',
90);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(4, 'The Pale Blue Eye', 620000, 'A world-weary detective is hired to
investigate the murder of a West Point cadet. Stymied by the cadets code of
silence, he enlists one of their own to help unravel the case - a young man the
world would come to know as Edgar Allan Poe.', '22-AUG-13', 103);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(6, 'Knives Out', 390000, 'A detective investigates the death of the
patriarch of an eccentric, combative family.', '05-DEC-13', 135);
INSERT INTO movie(movie_id, title, budget, overview, release_date, runtime)
values(9, 'Ant-Man and the Wasp: Quantumania', 450000, 'Scott Lang and Hope Van
Dyne, along with Hank Pym and Janet Van Dyne, explore the Quantum Realm, where they
interact with strange creatures and embark on an adventure that goes beyond the
limits of what they thought was possible.', '16-FEB-23', 125);
INSERT INTO genre (genre_id, genre_name) VALUES (1001,'Adventure');
INSERT INTO genre (genre_id, genre_name) VALUES (1002,'Fantasy');
INSERT INTO genre (genre_id, genre_name) VALUES (1003,'Comedy');
INSERT INTO genre (genre_id, genre_name) VALUES (1004,'Biography');
INSERT INTO genre (genre_id, genre_name) VALUES (1005,'Detective');
INSERT INTO genre (genre_id, genre_name) VALUES (1006,'Horror');
INSERT INTO genre (genre_id, genre_name) VALUES (1007,'Action');
INSERT INTO genre (genre_id, genre_name) VALUES (1008,'Historical ');
INSERT INTO genre (genre_id, genre_name) VALUES (1009,'Drama');
INSERT INTO genre (genre_id, genre_name) VALUES (1010,'Mystery');
INSERT INTO genre (genre_id, genre_name) VALUES (1011,'Romance');
INSERT INTO genre (genre_id, genre_name) VALUES (1012,'Thriller');

INSERT INTO movie_genres (movie_id, genre_id) VALUES (1,1002);


INSERT INTO movie_genres (movie_id, genre_id) VALUES (1,1001);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (5,1001);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (5,1002);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (285,1007);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (285,1003);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (285,1002);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (100,1007);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (100,1003);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (35,1005);

INSERT INTO movie_genres (movie_id, genre_id) VALUES (2,1006);


INSERT INTO movie_genres (movie_id, genre_id) VALUES (7,1006);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (207,1006);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (26,1006);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (666,1006);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (8,1010);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (3,1010);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (4,1012);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (6,1010);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (9,1012);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (8,1012);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (3,1012);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (4,1010);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (6,1012);
INSERT INTO movie_genres (movie_id, genre_id) VALUES (9,1010);

INSERT ALL
INTO gender (gender_id, gender) VALUES (0,'Unspecified')
INTO gender (gender_id, gender) VALUES (1,'Female')
INTO gender (gender_id, gender) VALUES (2,'Male')
SELECT * FROM dual;

INSERT ALL
INTO language (language_id, language_name) VALUES (2001,'English')
INTO language (language_id, language_name) VALUES (2002,'russian')
INTO language (language_id, language_name) VALUES (2003,'kazakh')
SELECT * FROM dual;

INSERT ALL
INTO country (country_id, country_name) VALUES (151,'USA')
INTO country (country_id, country_name) VALUES (152,'Great Britain')
INTO country (country_id, country_name) VALUES (153,'Kazakhstan')
INTO country (country_id, country_name) VALUES (154,'Turkey')
INTO country (country_id, country_name) VALUES (155,'India')
SELECT * FROM dual;

INSERT ALL
INTO country (country_id, country_name) VALUES (156,'Norway')
INTO country (country_id, country_name) VALUES (157,'Sweden')
INTO country (country_id, country_name) VALUES (158,'Spain')
INTO country (country_id, country_name) VALUES (159,'Australia')
INTO country (country_id, country_name) VALUES (160,'Germany')
INTO country (country_id, country_name) VALUES (161,'France')
INTO country (country_id, country_name) VALUES (162,'Egypt')
INTO country (country_id, country_name) VALUES (163,'Italy')
INTO country (country_id, country_name) VALUES (164,'South Korea')
INTO country (country_id, country_name) VALUES (165,'Brazil')
SELECT * FROM dual;

INSERT INTO person (person_id, person_name) VALUES (1,'George Lucas');


INSERT INTO person (person_id, person_name) VALUES (2,'Mark Hamill');
INSERT INTO person (person_id, person_name) VALUES (3,'Harrison Ford');
INSERT INTO person (person_id, person_name) VALUES (4,'Johny Depp');
INSERT INTO person (person_id, person_name) VALUES (5,'Orlando Bloom ');
INSERT INTO person (person_id, person_name) VALUES (6,'Keira Knightley');
INSERT INTO person (person_id, person_name) VALUES (7,'Miley Cyrus');
INSERT INTO person (person_id, person_name) VALUES (8,'Ryan Reynolds');
INSERT INTO person (person_id, person_name) VALUES (9,'Blake Lively');

INSERT INTO movie_cast (movie_id, person_id, character_name, gender_id) VALUES


(285,4,'Captain Jack Sparrow',2);
INSERT INTO movie_cast (movie_id, person_id, character_name, gender_id) VALUES
(285,5,'Will Turner',2);
INSERT INTO movie_cast (movie_id, person_id, character_name, gender_id) VALUES
(285,6,'Elizabeth Swann',1);

INSERT INTO production_country (movie_id, country_id) VALUES (285,151);


INSERT INTO production_country (movie_id, country_id) VALUES (1,151);

You might also like