0% found this document useful (0 votes)
45 views7 pages

практис5 лаб3

1. The document contains PL/SQL code blocks that declare variables, tables, exceptions, and perform data manipulation and exception handling. 2. It declares variables, tables for movies, genres, persons, and their relationships, and exceptions for business rules validation. 3. The code blocks perform inserts into tables, selects with exception handling, and deletes with validation of constraints and exceptions.

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)
45 views7 pages

практис5 лаб3

1. The document contains PL/SQL code blocks that declare variables, tables, exceptions, and perform data manipulation and exception handling. 2. It declares variables, tables for movies, genres, persons, and their relationships, and exceptions for business rules validation. 3. The code blocks perform inserts into tables, selects with exception handling, and deletes with validation of constraints and exceptions.

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

DECLARE

-- declare variables
v_movie_id movie.movie_id%TYPE := 1001;
v_title movie.title%TYPE;
v_budget movie.budget%TYPE;
-- declare user-defined exception
no_data_found EXCEPTION;

BEGIN
-- attempt to retrieve genre_id and director for a given movie_id
SELECT movie_id, title INTO v_movie_id, v_title FROM movie WHERE movie_id =
v_movie_id;

-- check if genre_id is null


IF v_movie_id IS NULL THEN
-- raise pre-defined exception if genre_id is null
RAISE VALUE_ERROR;
END IF;

-- attempt to retrieve actor_name for a given movie_id


--SELECT movie.title, genre.genre_name FROM movie JOIN movie_genres ON
movie.movie_id = movie_genres.movie_id JOIN genre ON genre.genre_id =
movie_genres.genre_id;

-- attempt to retrieve language_name for a given movie_id


--SELECT person_name FROM person WHERE person_id = 4;

EXCEPTION
-- handle pre-defined exception
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('Error: Genre ID is null for Movie ID ' || v_movie_id);

-- handle non-predefined exception


WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Error: No data found for Movie ID ' || v_movie_id);

-- handle user-defined exception


WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('Error: No data found for Movie ID ' || v_movie_id);
END;

CREATE TABLE messages (


results VARCHAR(50)
);

DECLARE
ename hr.employees.last_name%TYPE;
emp_sal hr.employees.salary%TYPE := 6000;
BEGIN
DELETE FROM messages;
SELECT last_name INTO ename FROM hr.employees WHERE salary = emp_sal;
IF SQL%ROWCOUNT = 1 THEN
INSERT INTO messages (results) VALUES (ename || ' has a salary of ' ||
emp_sal);
ELSIF SQL%ROWCOUNT = 0 THEN
raise no_data_found;
ELSE
raise too_many_rows;
END IF;
EXCEPTION
WHEN no_data_found THEN
INSERT INTO messages (results) VALUES ('No employee with a salary of ' ||
emp_sal);
WHEN too_many_rows THEN
INSERT INTO messages (results) VALUES ('More than one employee with a salary of
' || emp_sal);
WHEN OTHERS THEN
INSERT INTO messages (results) VALUES ('Some other error occurred');
END;

select * from messages;

----
CREATE TABLE employees (
employee_id NUMBER,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
email VARCHAR2(100),
hire_date DATE,
job_id VARCHAR2(50),
salary NUMBER,
commission_pct NUMBER,
manager_id NUMBER,
department_id NUMBER,
CONSTRAINT pk_employee_id PRIMARY KEY (employee_id),
CONSTRAINT fk_department_id FOREIGN KEY (department_id) REFERENCES departments
(department_id)
);
------------------------------------------------
DECLARE
childrecord_exists EXCEPTION;
PRAGMA EXCEPTION_INIT(childrecord_exists, -2292);
BEGIN
DBMS_OUTPUT.PUT_LINE('Deleting department 40....');
DELETE FROM departments WHERE department_id = 40;
EXCEPTION
WHEN childrecord_exists THEN
DBMS_OUTPUT.PUT_LINE('Cannot delete department 40. Child record exists in
employees table.');
END;

CREATE TABLE departments (


department_id NUMBER
);

BEGIN
INSERT INTO departments (department_id) VALUES (10);
INSERT INTO departments (department_id) VALUES (20);
INSERT INTO departments (department_id) VALUES (30);
INSERT INTO departments (department_id) VALUES (40);
INSERT INTO departments (department_id) VALUES (50);
END;
------------------------
DECLARE
childrecord_exists EXCEPTION;
PRAGMA EXCEPTION_INIT(childrecord_exists, -2292);

BEGIN
DBMS_OUTPUT.PUT_LINE('Deleting department 40.....');
DELETE FROM departments WHERE department_id = 40;
EXCEPTION

WHEN childrecord_exists THEN


DBMS_OUTPUT.PUT_LINE('Cannot delete department 40 because it still has child
records.');
END;
------------

DECLARE
not_enough EXCEPTION;
balance NUMBER(10,2) := 1000;
withdrawal NUMBER(10,2) := 1500;
BEGIN
IF withdrawal > balance THEN
RAISE not_enough;
END IF;

balance := balance - withdrawal;


DBMS_OUTPUT.PUT_LINE('Withdrawal successful. New balance: ' || balance);

EXCEPTION
WHEN not_enough THEN
DBMS_OUTPUT.PUT_LINE('Not enough for withdrawal: ' || withdrawal);
END;

---------------------------------lab 3---------------------------------------
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)
);

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);
CREATE TABLE genre (
genre_id NUMBER(10) NOT NULL,
genre_name varchar2(100) DEFAULT NULL,
PRIMARY KEY (genre_id)
);
select * from genre
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');

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)
);

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);

CREATE TABLE person (


person_id NUMBER(10) NOT NULL,
person_name varchar2 (500) DEFAULT NULL,
PRIMARY KEY (person_id)
);

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');

CREATE TABLE gender (


gender_id NUMBER(10) NOT NULL,
gender varchar2(20) DEFAULT NULL,
PRIMARY KEY (gender_id)
);

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;

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)
);

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);

------------user defined-----------------
DECLARE
v_movie_name VARCHAR2(100) := 'Star Wars: Episode VII - The Force Awakens';
some_variable movie%Rowtype;--VARCHAR2(100);
BEGIN

SELECT * INTO some_variable FROM movie WHERE title = v_movie_name;

IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20002, 'Movie not found: ' || v_movie_name);
END IF;

DBMS_OUTPUT.PUT_LINE('Found the movie: ' || v_movie_name);

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
-------------------------------------------------------------------------
select * from movie
-----------pre defined-----------------------
DECLARE
v_movie_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_movie_count FROM movie WHERE release_date = '02-09';

DBMS_OUTPUT.PUT_LINE('There are ' || v_movie_count || ' movies released in


2019.');

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
------------non predefined-------------
DECLARE
v_movie_id VARCHAR2(20) := 'string';
v_found_movie movie%ROWTYPE;
BEGIN

BEGIN
v_movie_id := TO_CHAR(TO_NUMBER(v_movie_id));
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid movie id: ' || v_movie_id);
END;

SELECT * INTO v_found_movie FROM movie WHERE movie_id = v_movie_id;

DBMS_OUTPUT.PUT_LINE('Found movie with ID ' || v_found_movie.movie_id || ': ' ||


v_found_movie.title);

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
-----2 incoporate exceptions--------
-----1-------------------
DECLARE
runt movie.runtime%TYPE := 149;
bud movie.budget%TYPE := 50000;
BEGIN
FOR movie_table IN (SELECT * FROM movie WHERE runt = runtime AND budget > bud)
LOOP
BEGIN
DBMS_OUTPUT.PUT_LINE(movie_table.title || ' spent ' || movie_table.budget);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END LOOP;
END;
-----2-------------
DECLARE
mov_id NUMBER := 5;
name movie.title%TYPE;
bud movie.budget%TYPE;

movie_id movie.movie_id%TYPE;

CURSOR mov_cursor IS
SELECT title, budget, release_date
FROM movie
WHERE movie_id = mov_id;
BEGIN
FOR mov_table IN mov_cursor LOOP
BEGIN
name := mov_table.title;
bud := mov_table.budget;

IF bud > 50000 THEN


DBMS_OUTPUT.PUT_LINE(name || ' is an expensive movie');
ELSE
DBMS_OUTPUT.PUT_LINE(name || ' is not an expensive movie');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END LOOP;
END;
-----3--------------------
DECLARE
TYPE gen_table IS TABLE OF genre.genre_name%TYPE INDEX BY BINARY_INTEGER;
gen_names gen_table;
gen_id genre.genre_id%TYPE := 102;
BEGIN
FOR i IN 1..10 LOOP
BEGIN
SELECT genre_name
INTO gen_names(i)
FROM genre
WHERE genre_id = gen_id;

gen_id := gen_id + 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for genre_id ' || gen_id);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END LOOP;

FOR i IN 1..gen_names.COUNT LOOP


DBMS_OUTPUT.PUT_LINE(gen_names(i));
END LOOP;
END;

-----4---------------------

You might also like