Practica 15 Workshop 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Práctica 15 Workshop 1

Rubén Sotelo de León


Create the tables based on the following table instance charts. Choose the appropriate data types
and be sure to add integrity constraints.

Table: member

CREATE TABLE MEMBER (

MEMBER_ID NUMBER(10) PRIMARY KEY,

LAST_NAME VARCHAR2(25) NOT NULL,

FIRST_NAME VARCHAR2(25) NOT NULL,

ADDRESS VARCHAR2(100),

CITY VARCHAR2(30),

PHONE VARCHAR2(15),

JOIN_DATE DATE DEFAULT SYSDATE NOT NULL

);

Table: Title

CREATE TABLE TITLE (

TITLE_ID NUMBER(10) PRIMARY KEY,

TITLE_NAME VARCHAR2(60) NOT NULL,

DESCRIPTION VARCHAR2(400),

RATING VARCHAR2(4) CHECK (RATING IN ('G', 'PG', 'R', 'NC17', 'NR')),

CATEGORY VARCHAR2(20) CHECK (CATEGORY IN ('DRAMA', 'COMEDY', 'ACTION', 'CHILD', 'SCIFI',


'DOCUMENTARY')),

RELEASE_DATE DATE

);

Table: TITLE_COPY

CREATE TABLE TITLE_COPY (

COPY_ID NUMBER(10) NOT NULL,

TITLE_ID NUMBER(10) NOT NULL,


STATUS VARCHAR2(15) NOT NULL CHECK (STATUS IN ('AVAILABLE', 'DESTROYED', 'RENTED',
'RESERVED')),

PRIMARY KEY (COPY_ID),

FOREIGN KEY (TITLE_ID) REFERENCES TITLE(TITLE_ID)

);

Table: RENTAL

CREATE TABLE RENTAL (

BOOK_DATE DATE DEFAULT SYSDATE NOT NULL,

MEMBER_ID NUMBER(10) NOT NULL,

COPY_ID NUMBER(10) NOT NULL,

ACT_RET_DATE NUMBER(10) NOT NULL,

EXP_RET_DATE DATE DEFAULT (SYSDATE + 2) NOT NULL,

TITLE_ID NUMBER(10) NOT NULL,

PRIMARY KEY (BOOK_DATE),

FOREIGN KEY (MEMBER_ID) REFERENCES MEMBER(MEMBER_ID),

FOREIGN KEY (COPY_ID) REFERENCES TITLE_COPY(COPY_ID),

FOREIGN KEY (TITLE_ID) REFERENCES TITLE(TITLE_ID)

);

Table reserva on:

CREATE TABLE RESERVATION (

RES_DATE DATE NOT NULL,

MEMBER_ID NUMBER(10) NOT NULL,

TITLE_ID NUMBER(10) NOT NULL,

PRIMARY KEY (RES_DATE, MEMBER_ID, TITLE_ID),

FOREIGN KEY (MEMBER_ID) REFERENCES MEMBER(MEMBER_ID),

FOREIGN KEY (TITLE_ID) REFERENCES TITLE(TITLE_ID)

);

Create sequences to uniquely iden fy each row in the MEMBER table and the TITLE table. a)
Member number for the MEMBER table: Start with 101; do not allow caching of the values. Name
the sequence MEMBER_ID_SEQ. b) Title number for the TITLE table: Start with 92; do not allow
caching of the values. Name the sequence TITLE_ID_SEQ. c) Verify the existence of the sequences
in the Connec ons Navigator in SQL Developer.

CREATE SEQUENCE MEMBER_ID_SEQ

START WITH 101

CACHE 0;

CREATE SEQUENCE TITLE_ID_SEQ

START WITH 92

CACHE 0;

Add data to the tables. Create a script for each set of data to be added. a) Add movie tles to the
TITLE table. Write a script to enter the movie informa on. Save the statements in a script named
lab_apcs_4a.sql. Use the sequences to uniquely iden fy each tle. Enter the release dates in the
DD-MONYYYY format. Remember that single quota on marks in a character field must be specially
handled. Verify your addi ons.

ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';

SET SERVEROUTPUT ON;

INSERT INTO TITLE (TITLE_ID, TITLE_NAME, AUTHOR, YEAR_PUBLISHED, PUBLISHER)

VALUES (TITLE_ID_SEQ.NEXTVAL, 'The Shawshank Redemp on', 'Stephen King', 1994, 'Castle Rock
Entertainment');

INSERT INTO TITLE (TITLE_ID, TITLE_NAME, AUTHOR, YEAR_PUBLISHED, PUBLISHER)

VALUES (TITLE_ID_SEQ.NEXTVAL, 'The Godfather', 'Mario Puzo', 1972, 'Paramount Pictures');

INSERT INTO TITLE (TITLE_ID, TITLE_NAME, AUTHOR, YEAR_PUBLISHED, PUBLISHER)

VALUES (TITLE_ID_SEQ.NEXTVAL, 'The Dark Knight', 'Christopher Nolan', 2008, 'Warner Bros.');

COMMIT;

SELECT * FROM TITLE;

Add data to the MEMBER table. Save the insert statements in a script named lab_apcs_4b.sql.
Execute commands in the script. Be sure to use the sequence to add the member numbers.
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';

SET SERVEROUTPUT ON;

INSERT INTO MEMBER (MEMBER_ID, LAST_NAME, FIRST_NAME, ADDRESS, CITY, PHONE,


JOIN_DATE)

VALUES (MEMBER_ID_SEQ.NEXTVAL, 'Velasquez', 'Carmen', '283 King Street', 'Sea le', '206-899-
6666', '08-MAR-1990');

INSERT INTO MEMBER (MEMBER_ID, LAST_NAME, FIRST_NAME, ADDRESS, CITY, PHONE,


JOIN_DATE)

VALUES (MEMBER_ID_SEQ.NEXTVAL, 'Ngao', 'LaDoris', '5 Modrany', 'Bra slava', '586-355-8882',


'08-MAR-1990');

INSERT INTO MEMBER (MEMBER_ID, LAST_NAME, FIRST_NAME, ADDRESS, CITY, PHONE,


JOIN_DATE)

VALUES (MEMBER_ID_SEQ.NEXTVAL, 'Nagayama', 'Midori', '68 Via Centrale', 'Sao Paolo', '254-852-
5764', '17-JUN-1991');

INSERT INTO MEMBER (MEMBER_ID, LAST_NAME, FIRST_NAME, ADDRESS, CITY, PHONE,


JOIN_DATE)

VALUES (MEMBER_ID_SEQ.NEXTVAL, 'Quick-to-See', 'Mark', '6921 King Way', 'Lagos', '63-559-


7777', '07-APR-1990');

INSERT INTO MEMBER (MEMBER_ID, LAST_NAME, FIRST_NAME, ADDRESS, CITY, PHONE,


JOIN_DATE)

VALUES (MEMBER_ID_SEQ.NEXTVAL, 'Ropeburn', 'Audry', '86 Chu Street', 'Hong Kong', '41-559-87',
'18-JAN-1991');

INSERT INTO MEMBER (MEMBER_ID, LAST_NAME, FIRST_NAME, ADDRESS, CITY, PHONE,


JOIN_DATE)

VALUES (MEMBER_ID_SEQ.NEXTVAL, 'Urguhart', 'Molly', '3035 Laurier', 'Quebec', '418-542-9988',


'18-JAN-1991');

COMMIT;
SELECT * FROM MEMBER;

Add the following movie copies in the TITLE_COPY table: Note: Have the TITLE_ID numbers
available for this exercise. Title Copy_Id Status Title Copy_Id Willie and Christmas Too 1 AVAILABLE
Willie and Christmas Too 1 Alien Again 1 AVAILABLE Alien Again 1 2 RENTED 2 The Glob 1
AVAILABLE The Glob 1 My Day Off 1 AVAILABLE My Day Off 1 2 AVAILABLE 2 3 RENTED 3 Miracles
on Ice 1 AVAILABLE Miracles on Ice 1 Soda Gang 1 AVAILABLE Soda Gang 1

INSERT INTO TITLE_COPY (TITLE_COPY_ID, TITLE_ID, STATUS) VALUES (1, 1, 'AVAILABLE');

INSERT INTO TITLE_COPY (TITLE_COPY_ID, TITLE_ID, STATUS) VALUES (1, 2, 'AVAILABLE');

INSERT INTO TITLE_COPY (TITLE_COPY_ID, TITLE_ID, STATUS) VALUES (2, 2, 'RENTED');

INSERT INTO TITLE_COPY (TITLE_COPY_ID, TITLE_ID, STATUS) VALUES (1, 3, 'AVAILABLE');

INSERT INTO TITLE_COPY (TITLE_COPY_ID, TITLE_ID, STATUS) VALUES (2, 3, 'AVAILABLE');

INSERT INTO TITLE_COPY (TITLE_COPY_ID, TITLE_ID, STATUS) VALUES (3, 3, 'RENTED');

INSERT INTO TITLE_COPY (TITLE_COPY_ID, TITLE_ID, STATUS) VALUES (1, 4, 'AVAILABLE');

INSERT INTO TITLE_COPY (TITLE_COPY_ID, TITLE_ID, STATUS) VALUES (2, 4, 'RENTED');

INSERT INTO TITLE_COPY (TITLE_COPY_ID, TITLE_ID, STATUS) VALUES (3, 4, 'AVAILABLE');

INSERT INTO TITLE_COPY (TITLE_COPY_ID, TITLE_ID, STATUS) VALUES (1, 5, 'AVAILABLE');

Add the following rentals to the RENTAL table: Note: The tle number may be different depending
on the sequence number. Title_ Id Copy_ Id Member_Id Book_date Exp_Ret_Date 92 1 101 3 days
ago 1 day ago 93 2 101 1 day ago 1 day from now 95 3 102 2 days ago Today 97 1 106 4 days ago 2
days ago

INSERT INTO RENTAL (TITLE_ID, COPY_ID, MEMBER_ID, BOOK_DATE, EXP_RET_DATE)

VALUES (92, 1, 101, TO_DATE('18-APR-2023', 'DD-MON-YYYY'), TO_DATE('20-APR-2023', 'DD-MON-


YYYY'));

INSERT INTO RENTAL (TITLE_ID, COPY_ID, MEMBER_ID, BOOK_DATE, EXP_RET_DATE)

VALUES (93, 2, 101, TO_DATE('20-APR-2023', 'DD-MON-YYYY'), TO_DATE('22-APR-2023', 'DD-MON-


YYYY'));
INSERT INTO RENTAL (TITLE_ID, COPY_ID, MEMBER_ID, BOOK_DATE, EXP_RET_DATE)

VALUES (95, 3, 102, TO_DATE('19-APR-2023', 'DD-MON-YYYY'), TO_DATE('21-APR-2023', 'DD-MON-


YYYY'));

INSERT INTO RENTAL (TITLE_ID, COPY_ID, MEMBER_ID, BOOK_DATE, EXP_RET_DATE)

VALUES (97, 1, 106, TO_DATE('17-APR-2023', 'DD-MON-YYYY'), TO_DATE('19-APR-2023', 'DD-MON-


YYYY'));

Create a view named TITLE_AVAIL to show the movie tles, the availability of each copy, and its
expected return date if rented. Query all rows from the view. Order the results by tle.

CREATE VIEW TITLE_AVAIL AS

SELECT t.TITLE_NAME, tc.COPY_ID, tc.STATUS, r.EXP_RET_DATE

FROM TITLE t

INNER JOIN TITLE_COPY tc

ON t.TITLE_ID = tc.TITLE_ID

LEFT JOIN RENTAL r

ON tc.TITLE_ID = r.TITLE_ID AND tc.COPY_ID = r.COPY_ID;

Make changes to the data in the tables. a) Add a new tle. The movie is “Interstellar Wars,” which
is rated PG and classified as a science fic on movie. The release date is 07-JUL-77. The descrip on
is “Futuris c interstellar ac on movie. Can the rebels save the humans from the evil empire?” Be
sure to add a tle copy record for two copies. b) Enter two reserva ons. One reserva on is for
Carmen Velasquez, who wants to rent “Interstellar Wars.” The other is for Mark Quick-to-See, who
wants to rent “Soda Gang.”

-- Add a new tle

INSERT INTO TITLE(TITLE_ID, TITLE_NAME, RATING, MOVIE_TYPE, DESCRIPTION, RELEASE_DATE)

VALUES(TITLE_ID_SEQ.NEXTVAL, 'Interstellar Wars', 'PG', 'Science Fic on', 'Futuris c interstellar


ac on movie. Can the rebels save the humans from the evil empire?', TO_DATE('07-JUL-77', 'DD-
MON-YYYY'));

-- Add tle copy records for two copies

INSERT INTO TITLE_COPY(TITLE_COPY_ID, TITLE_ID, STATUS)

VALUES(TITLE_COPY_ID_SEQ.NEXTVAL, (SELECT TITLE_ID FROM TITLE WHERE TITLE_NAME =


'Interstellar Wars'), 'AVAILABLE');
INSERT INTO TITLE_COPY(TITLE_COPY_ID, TITLE_ID, STATUS)

VALUES(TITLE_COPY_ID_SEQ.NEXTVAL, (SELECT TITLE_ID FROM TITLE WHERE TITLE_NAME =


'Interstellar Wars'), 'AVAILABLE');

-- Reserva on for Carmen Velasquez for Interstellar Wars

INSERT INTO RENTAL(TITLE_ID, COPY_ID, MEMBER_ID, BOOK_DATE, EXP_RET_DATE)

VALUES((SELECT TITLE_ID FROM TITLE WHERE TITLE_NAME = 'Interstellar Wars'), (SELECT


TITLE_COPY_ID FROM TITLE_COPY WHERE TITLE_ID = (SELECT TITLE_ID FROM TITLE WHERE
TITLE_NAME = 'Interstellar Wars') AND STATUS = 'AVAILABLE' AND ROWNUM = 1), (SELECT
MEMBER_ID FROM MEMBER WHERE FIRST_NAME = 'Carmen' AND LAST_NAME = 'Velasquez'),
SYSDATE, SYSDATE+3);

-- Reserva on for Mark Quick-to-See for Soda Gang

INSERT INTO RENTAL(TITLE_ID, COPY_ID, MEMBER_ID, BOOK_DATE, EXP_RET_DATE)

VALUES((SELECT TITLE_ID FROM TITLE WHERE TITLE_NAME = 'Soda Gang'), (SELECT TITLE_COPY_ID
FROM TITLE_COPY WHERE TITLE_ID = (SELECT TITLE_ID FROM TITLE WHERE TITLE_NAME = 'Soda
Gang') AND STATUS = 'AVAILABLE' AND ROWNUM = 1), (SELECT MEMBER_ID FROM MEMBER
WHERE FIRST_NAME = 'Mark' AND LAST_NAME = 'Quick-to-See'), SYSDATE, SYSDATE+3);

Customer Carmen Velasquez rents the movie "Interstellar Wars," copy 1. Remove her reserva on
for the movie. Record the informa on about the rental. Allow the default value for the expected
return date to be used. Verify that the rental was recorded by using the view you created

-- Delete the reserva on

DELETE FROM RESERVATION

WHERE MEMBER_ID = 101 AND TITLE_ID = 99;

-- Insert the rental informa on

INSERT INTO RENTAL (TITLE_ID, COPY_ID, MEMBER_ID, BOOK_DATE)

SELECT 99, 1, 101, SYSDATE FROM DUAL;

-- Query the TITLE_AVAIL view to verify the rental was recorded

SELECT * FROM TITLE_AVAIL


ORDER BY TITLE;

Make a modifica on to one of the tables.

a. Add a PRICE column to the TITLE table to record the purchase price of the video. The
column should have a total length of eight digits and two decimal places. Verify your
modifica ons.

ALTER TABLE TITLE

ADD PRICE NUMBER(8,2);

DESCRIBE TITLE;

Customer Carmen Velasquez rents the movie "Interstellar Wars," copy 1. Remove her reserva on
for the movie. Record the informa on about the rental. Allow the default value for the expected
return date to be used. Verify that the rental was recorded by using the view you created.

INSERT INTO RENTAL (TITLE_ID, COPY_ID, MEMBER_ID, BOOK_DATE)

VALUES (101, 1, 101, SYSDATE);

Make a modifica on to one of the tables.

a. Add a PRICE column to the TITLE table to record the purchase price of the video. The
column should have a total length of eight digits and two decimal places. Verify your
modifica ons.

ALTER TABLE TITLE ADD PRICE NUMBER(8,2);

SELECT * FROM TITLE;

b. Create a script named p15q7b.sql to update each video with a price according to the following
list.

Note: Have the tle_id numbers available for this exercise.

UPDATE TITLE SET PRICE = 25 WHERE TITLE_ID = 1;

UPDATE TITLE SET PRICE = 35 WHERE TITLE_ID = 2;

UPDATE TITLE SET PRICE = 35 WHERE TITLE_ID = 3;

UPDATE TITLE SET PRICE = 35 WHERE TITLE_ID = 4;

UPDATE TITLE SET PRICE = 30 WHERE TITLE_ID = 5;

UPDATE TITLE SET PRICE = 35 WHERE TITLE_ID = 6;


UPDATE TITLE SET PRICE = 29 WHERE TITLE_ID = 7;

Create a report tled Customer History Report. This report will contain each customer's history of
ren ng videos. Be sure to include the customer name, movie rented, dates of the rental, and
dura on of rentals. Total the number of rentals for all customers for the repor ng period. Save the
script in a file named p15q8.sql.

SELECT m.First_Name || ' ' || m.Last_Name AS Customer_Name, t.Title_Name AS Movie_Rented,


r.Book_Date AS Rental_Date, r.Dura on AS Rental_Dura on

FROM MEMBER m

JOIN RENTAL r ON m.Member_Id = r.Member_Id

JOIN TITLE t ON r.Title_Id = t.Title_Id

ORDER BY m.Last_Name, m.First_Name, r.Book_Date;

SELECT m.First_Name || ' ' || m.Last_Name AS Customer_Name, COUNT(*) AS Total_Rentals

FROM MEMBER m

JOIN RENTAL r ON m.Member_Id = r.Member_Id

JOIN TITLE t ON r.Title_Id = t.Title_Id

GROUP BY m.Member_Id, m.First_Name, m.Last_Name

ORDER BY m.Last_Name, m.First_Name;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy