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

Ejemplo de Examen Base de Datos

This document contains solutions to exercises from a February 2017 exam on databases for an engineering degree program. The exercises cover topics like database design, SQL queries, stored procedures, and triggers. Sample table schemas, queries, a stored procedure implementation, and trigger are provided as answers to the questions.

Uploaded by

Carlos Mata Mata
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)
37 views7 pages

Ejemplo de Examen Base de Datos

This document contains solutions to exercises from a February 2017 exam on databases for an engineering degree program. The exercises cover topics like database design, SQL queries, stored procedures, and triggers. Sample table schemas, queries, a stored procedure implementation, and trigger are provided as answers to the questions.

Uploaded by

Carlos Mata Mata
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/ 7

Facultad de Informática. Ingenierı́a en Informática / del Software / de Computadores.

Databases. Course 2016-2017. February 2017 exam solutions. 26/01/2017.

Answers to Exercise 1.

StartDate
PolicyNr Name
Name EndDate Technique

Century
N N
Exhibition Assigned Painting

N N

Holds Paints

Name BirthDate
1 1
4 1
Museum Painter

Name City Master


Apprentice

Teaches

Answers to Exercise 2.

Student(SSN) Person(SSN, Name)

StudentMail(SSN, eMail)

Teacher(SSN, eMail, Position)

Course(Code, Name, Coordinator)

Enrolled(StudentSSN, Course, Year, Grade)

Tutors(Course, Tutor)

– Cardinality constraint 4 in relationship Tutor cannot be represented.


– Total participation constraint in relationship Enrolled cannot be represented.

1
Answers to Exercise 3.

alter session set nls_date_format = "DD/MM/YYYY HH24:MI";

-- -----------------------------------------------------------------------------
-- 3.a
-- -----------------------------------------------------------------------------

SELECT DISTINCT pe.TFilm


FROM Film pe
JOIN Show pa ON pe.TFilm = pa.TFilm
JOIN Cinema ci ON ci.cod = pa.codCinema
WHERE duration > 90 AND district = 24321;

-- -----------------------------------------------------------------------------
-- 3.b
-- -----------------------------------------------------------------------------

SELECT TFilm
FROM Film pe
JOIN Show pa USING (TFilm)
JOIN Room sa USING (codCinema,numRoom)
JOIN Cinema ci ON ci.cod = codCinema
WHERE pe.duration > 90
GROUP BY TFilm, ci.district
HAVING SUM(capacity) > 300;

-- -----------------------------------------------------------------------------
-- 3.c
-- -----------------------------------------------------------------------------

-- (Answer 1: using UNION)

SELECT DISTINCT c.cod, c.district, count(*)


FROM Cinema c
JOIN Show p ON c.cod = p.codCinema
WHERE p.ticketsSold = 0
GROUP BY c.cod, c.district
UNION ALL
SELECT cod, district, 0
FROM Cinema
WHERE cod NOT IN (SELECT codCinema FROM Show WHERE ticketsSold = 0);

-- (Answer 2: using LEFT JOIN)


-- Note that there is a nested subquery in the FROM clause.

SELECT DISTINCT c.cod, c.district, NVL(numShows,0)


FROM Cinema c
LEFT JOIN (
SELECT p.codCinema AS codCinema, COUNT(*) AS numShows
FROM Show p
WHERE p.ticketsSold = 0
GROUP BY p.codCinema)
ON c.cod = codCinema;

-- -----------------------------------------------------------------------------
-- 3.d
-- -----------------------------------------------------------------------------

2
SELECT codCinema
FROM Show
JOIN Film USING (TFilm)
WHERE EXTRACT(YEAR FROM releaseDate) = 2016
AND codCinema NOT IN (
SELECT codCinema
FROM Show
JOIN Film USING (TFilm)
WHERE EXTRACT(YEAR FROM releaseDate) != 2016);

-- -----------------------------------------------------------------------------
-- 3.e
-- -----------------------------------------------------------------------------

-- (Answer 1: using >= ALL)

SELECT c.district, COUNT(DISTINCT p.TFilm)


FROM Cinema c
JOIN Show p ON c.cod = p.codCinema
GROUP BY c.district
HAVING COUNT(DISTINCT p.TFilm) >= ALL (
SELECT COUNT(DISTINCT p.TFilm)
FROM Cinema c
JOIN Show p ON c.cod = p.codCinema
GROUP BY c.district);

-- (Answer 2: using MAX(COUNT(...)) )

SELECT c.district, COUNT(DISTINCT p.TFilm)


FROM Cinema c
JOIN Show p ON c.cod = p.codCinema
GROUP BY c.district
HAVING COUNT(DISTINCT p.TFilm) = (
SELECT MAX(COUNT(DISTINCT p.TFilm))
FROM Cinema c
JOIN Show p ON c.cod = p.codCinema
GROUP BY c.district);

-- -----------------------------------------------------------------------------
-- 3.f
-- -----------------------------------------------------------------------------

CREATE OR REPLACE PROCEDURE CinemaShow(p_Cinema Room.CodCinema%TYPE) AS


v_cinemaData VARCHAR(300);
v_count NUMBER;
CURSOR CShow IS
SELECT p.startTime, p.numRoom, p.TFilm, s.capacity - p.ticketsSold availableSeats
FROM Room s
JOIN Show p ON (s.codCinema = p.codCinema AND s.numRoom = p.numRoom)
WHERE S.codCinema = p_Cinema
ORDER BY p.startTime, p.numRoom;

BEGIN
DBMS_OUTPUT.PUT_LINE(’--------------------------------------------------’);
SELECT COUNT(*), ’Cinema: ’|| TRIM(p_Cinema)|| ’, Num of Rooms: ’ || COUNT(*)
|| ’, Total capacity: ’|| SUM(capacity)
INTO v_count,v_cinemaData
FROM Room

3
WHERE codCinema = p_Cinema;

IF v_count = 0 THEN
RAISE NO_DATA_FOUND;
END IF;

DBMS_OUTPUT.PUT_LINE(v_cinemaData);
DBMS_OUTPUT.PUT_LINE(’--------------------------------------------------’);
DBMS_OUTPUT.PUT_LINE(’ Time Room Film Avail.Seats’);
DBMS_OUTPUT.PUT_LINE(’--------------------------------------------------’);

FOR rShow IN CShow


LOOP
DBMS_OUTPUT.PUT_LINE(TO_CHAR(rShow.startTime,’HH24:MI’) || ’ ’
|| TO_CHAR(rShow.numRoom,’9999’) || ’ ’
|| RPAD(rShow.TFilm,25) || ’ ’ || TO_CHAR(rShow.availableSeats,’9G999’));
END LOOP;
DBMS_OUTPUT.PUT_LINE(’--------------------------------------------------’);

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE(’Cinema ’ || p_Cinema || ’ does not exist.’);
END;
/

-- Anonymous block for testing the procedure.


SET SERVEROUTPUT ON;
BEGIN
CinemaShow(333);
END;
/

-- -----------------------------------------------------------------------------
-- 3.g
-- -----------------------------------------------------------------------------

CREATE OR REPLACE TRIGGER TicketsSold


AFTER INSERT OR DELETE OR UPDATE ON Sales
FOR EACH ROW
BEGIN
IF DELETING THEN
UPDATE Show
SET ticketsSold = ticketsSold - :OLD.numSeats
WHERE CodCinema = :OLD.CodCinema
AND NumRoom = :OLD.NumRoom
AND startTime = :OLD.startTime;
ELSIF INSERTING THEN
UPDATE Show
SET ticketsSold = ticketsSold + :NEW.numSeats
WHERE CodCinema = :OLD.CodCinema
AND NumRoom = :OLD.NumRoom
AND startTime = :OLD.startTime;
ELSE
-- The exam does not specify how to deal with this case.
-- If NumRoom or CodCinema changes, we have to record the
-- change in the number of tickets sold in two different rows!
UPDATE Show
SET ticketsSold = ticketsSold - :OLD.numSeats

4
WHERE CodCinema = :OLD.CodCinema
AND NumRoom = :OLD.NumRoom
AND startTime = :OLD.startTime;

UPDATE Show
SET ticketsSold = ticketsSold + :NEW.numSeats
WHERE CodCinema = :OLD.CodCinema
AND NumRoom = :OLD.NumRoom
AND startTime = :OLD.startTime;
END IF;
END;
/

-- We can test the trigger inserting, deleting and updating Sales:


alter session set nls_date_format = ’HH24:MI:SS’;
DELETE FROM Sales;

SELECT * FROM Room WHERE codCinema = 1 AND numRoom = 1 AND startTime = TO_DATE(’18:00:00’);

INSERT INTO Sales (idClient, codCinema, numRoom, startTime, TFilm, numSeats)


VALUES (100, 1, 1, to_date(’18:00:00’), ’Gone with the Wind’, 3);

SELECT * FROM pases WHERE codCinema = 1 AND numSala = 1 AND Hora = TO_DATE(’18:00:00’);

INSERT INTO Sales (idClient, codCinema, numRoom, startTime, TFilm, numSeats)


VALUES (200, 1, 1, to_date(’18:00:00’), ’Gone with the Wind’, 2);

SELECT * FROM Room WHERE codCinema = 1 AND numRoom = 1 AND startDate = TO_DATE(’18:00:00’);

UPDATE Sales SET numSeats = 5 WHERE idClient = 100;

SELECT * FROM Show WHERE codCinema = 1 AND numRoom = 1 AND startDate = TO_DATE(’18:00:00’);

DELETE FROM Sales WHERE idClient = 200;

SELECT * FROM Show WHERE codCinema = 1 AND numRoom = 1 AND startTime = TO_DATE(’18:00:00’);

5
Answers to Ejercicio 4.

-- -----------------------------------------------------------------------------
-- Given the table SALES(Title, TicketsSold), that initially is empty,
-- and the execution of the next sequence of sentences ( autocommit=
-- off)

-- a) Write the values of the column TicketsSold for each row in the
-- table Sales at each point denoted by step N -- (even though the
-- operations have not been committed)

-- b) Show the first sentence of each of the transactions included in


-- the sequence of sentences.

-- c) Does any error arise during execution? If there is an error,


-- explain it.

-- d) At the end of the execution, How many tables would have been
-- created in the database?
-- -----------------------------------------------------------------------------

savepoint step_one;
INSERT INTO SALES VALUES (’My Fair Lady’, 200); -- b) Start of 1st transaction.

-- Step 1 -- a) One row with values (’My Fair Lady’, 200).

savepoint step_two;
update VENTAS
set TicketsSold = TicketsSold + 100
where Title = ’My Fair Lady’;

-- Step 2 -- a) One row with values (’My Fair Lady’, 300).

rollback to savepoint step_two;

-- Step 3 -- a) One row with values (’My Fair Lady’, 200).

update VENTAS
set TicketsSold = TicketsSold + 200
where Title = ’My Fair Lady’;

-- Step 4 -- a) One row with values (’My Fair Lady’, 400).

rollback;

-- Step 5 -- a) All changes are undone. Table SALES is empty


-- (End of transaction. No active transaction).

INSERT INTO VENTAS VALUES (’My Fair Lady’, 1000); -- b) Start of 2nd transaction.
update VENTAS
set TicketsSold = TicketsSold + 300
where Title = ’My Fair Lady’;

-- Step 6 -- a) One row with values (’My Fair Lady’, 1300).

savepoint step_three;
commit;
-- (End of transaction. No active transaction).

6
-- Step 7 -- a) One row with values (’My Fair Lady’, 1300).

create table TopSales(Tfilm varchar(20), Total number(5)); -- b) Start of


-- 3rd transaction, ending with an implicit commit.
-- No active transaction.
Insert into TopSales values(’Breakfast at Tiffany’s’, 100); -- b) Start of
-- 4th transaction.

rollback to savepoint step_three;

-- Step 8 -- a) One row with values (’My Fair Lady’, 1300).


-- c) There is an error "ORA-01086 savepoint ’step_three’ never
-- established in this session or is invalid"
-- (but the transaction does not finish)

select * from TopSales where Tfilm = ’Breakfast at Tiffany’s’;


rollback;

-- Step 9 -- a) One row with values (’My Fair Lady’, 1300).


-- All changes since Step 7 are undone.
-- (End of transaction. No active transaction).
-- d) Rollback sentences do not undo any table creation as
-- they are DDL: Both tables are kept: SALES and TOPSALES.

You might also like