Question

In: Computer Science

Given a relational database that consists of the following relations: Performer (pid: integer, pname: string, years_of_experience:...

Given a relational database that consists of the following relations:

Performer (pid: integer, pname: string, years_of_experience: integer, age: integer) Movie (mname: string, genre: string, minutes: integer, release_year: integer, did: integer) Acted (pid: integer, mname: string)
Director (did: integer, dname: string, earnings: real)

Do the following using your Azure SQL database:
a) Use SQL statements to create the relations.
b) Populate the relations using SQL statements with the given data posted on Canvas. c) Implement the SQL queries for the following:

  1. Display all the data you store in the database to verify that you have populated the relations correctly.

  2. Find the names of all Action movies.

  3. For each genre, display the genre and the average length (minutes) of movies for that genre.

  4. Find the names of all performers with at least 20 years of experience who have acted in a movie

    directed by Black.

  5. Find the age of the oldest performer who is either named “Hanks” or has acted in a movie named

    “The Departed”.

  6. Find the names of all movies that are either a Comedy or have had more than one performer act in

    them.

  7. Find the names and pid's of all performers who have acted in at least two movies that have the same

    genre.

  8. Decrease the earnings of all directors who directed “Up” by 10%.

  9. Delete all movies released in the 70's and 80's (1970 <= release_year <= 1989).

Solutions

Expert Solution

SOLUTION A:

CREATE RELATION:

Perfomer Relation:

create table Performer (pid integer , pname varchar2(20), years_of_experience integer, age integer,primary key(pid))

Movie Relation:

create table Movie (mname varchar2(20), genre varchar2(20), minutes integer, release_year integer, did integer, primary key(mname),foreign key(did) references Director(did))

Actor Relation:

create table Acted (pid integer, mname varchar2(20),foreign key (pid) references Performer(pid),foreign key (mname) references Movie(mname))

Director Relation:

create table Director (did integer, dname varchar2(20), earnings real,primary key(did))

SOLUTION B:

NOTE : I have mentioned the sample data which is less and i have inserted some more data for the output clear

INSERTION INTO MOVIES RELATION:

insert into Movie values ("Romantic2","Romantic",190,2019,1000)
insert into Movie values ("Romantic3","Romantic",190,2019,1002)
insert into Movie values ("Romantic4","Romantic",190,2019,1002)

INSERTION INTO Director RELATION:

insert into Director values(1000,"RGV",100000)
insert into Director values(1001,"Trivikram",100000)
insert into Director values(1002,"black",100000)

INSERTION INTO Performer RELATION:

insert into Performer values (2001,"Hanks",30,60)
insert into Performer values (2004,"Vin0",10,60)
insert into Performer values (2005,"Vin",10,60)

INSERTION INTO Acted RELATION:

insert into Acted values (2004,"Romantic3")
insert into Acted values (2005,"Romantic2")

SOLUTION C:

NOTE: Mentioning questions since the sub-questions numbers are not mentioned

a)Display all the data you store in the database to verify that you have populated the relations correctly.

SELECT * FROM Performer;

SELECT * FROM Movies;

SELECT * FROM Acted;

SELECT * FROM Director;

b)Find the names of all Action movies.

SOLUTION:

SELECT mname FROM Movie WHERE genre=="Action"

EXPLANATION:

Apply where Condition on genre as Action and Project Movie name

c)For each genre, display the genre and the average length (minutes) of movies for that genre.

SOLUTION:

SELECT genre,AVG(minutes) AS  averagelength
FROM Movie
GROUP BY genre

EXPLANATION:

Group all the same genre by using GROUP BY, Then apply aggregate function AVG on the minutes

Project the mname,AVG(minutes)

d)Find the names of all performers with at least 20 years of experience who have acted in a movie directed by Black.

SOLUTION:

SELECT pname
FROM Acted A,Director D,Performer P, Movie M
WHERE dname=="black" AND M.did==D.did AND M.mname==A.mname AND P.pid==A.pid AND years_of_experience>=20

EXPLANATION:

Step 1: First find out director with dname then find the movies directed by him using the Movies table

Step 2: Get the all movies directed by the black and Join with Acted to get the pid

Step 3: Join with the Performer table to the pnames

e)Find the age of the oldest performer who is either named “Hanks” or has acted in a movie named “The Departed”.

SOLUTION:

SELECT P.pname,max(age) AS AGE
FROM Acted A,Performer P, Movie M
WHERE (pname =="Hanks" OR M.mname =="The Departed") AND M.mname==A.mname AND A.pid==P.pid

EXPLANATION :

Step 1: Find out the mname As the Departed or pname as the hanks

Step 2: Join the Movies table with the Acted to get the Pid of the performer

Step 3: Now join with the Performer table using the pid

Step 3: Use the aggregate functions to get the max age

f)Find the names of all movies that are either a Comedy or have had more than one performer act in them

​​​​​​​SOLUTION:

SELECT mname
FROM Movie
WHERE genre=="Comedy"
UNION
SELECT A.mname
FROM Acted A, Movie M
WHERE M.mname==A.mname
GROUP BY A.mname
HAVING count(A.mname)>=1

EXPLANATION:

Step 1: Find the movies which has genre As comedy

Step 2: Find the movies that has more than one performer by grouping the mname and checking whether it has more than one actor using the Having Clause

Step 3: Use Union to Get the complete Result

g)Find the names and pid's of all performers who have acted in at least two movies that have the same

​​​​​​​SOLUTION:

SELECT pname,P.pid
FROM Acted A, Movie M,Performer p
WHERE M.mname==A.mname AND A.pid==P.pid
GROUP BY M.genre,P.pid,pname
HAVING count(*)>=2

EXPLANATION:

Step 1: Join Movies table with Acted table so that we can get genre

Step 2: Join the previous one with the Performer table to get the mapping of the performer with movies table

Step 3: Group the data based on the genre

Step 4: Filter the data based on the number Of rows of each genre for performer based on the HAVING clause condition

h)Decrease the earnings of all directors who directed “Up” by 10%.

NOTE : the Way I have understood the Question is like for the movie Up all the directors income shoukd be reduced by 10%

Excuse me if the way i have understood the question is Wrong

​​​​​​​SOLUTION:

UPDATE Director
SET earnings=(earnings*(0.90))
where Director.did IN (SELECT did FROM Movie WHERE mname="Up")

EXPLANATION:

Step 1: Check for the did of all the directors who directed up and then update their earnings by 10%

i)Delete all movies released in the 70's and 80's (1970 <= release_year <= 1989).

SOLUTION:

DELETE FROM Movie
Where release_year>=1970 AND release_year<=1989


Related Solutions

INRO TO DATABASES Consider the following Schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname:...
INRO TO DATABASES Consider the following Schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in SQL using join, nested queries and set operators. 1. Find names of suppliers who supply every red or green part. 2. Find the sids of suppliers who supply every red part or supply every green part. 3. Find sids...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours) Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size) where, Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (year i.e, freshman, sophomore, junior, etc). Courses:...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours)Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size), Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (yeari.e, freshman, sophomore, junior, etc). Courses: All courses have...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours) Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size) where, Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (year i.e, freshman, sophomore, junior, etc). Courses:...
Question (3) Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer)...
Question (3) Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer) Class(name: string, meets at: string, room: string, fid: integer) Enrolled(snum: integer, cname: string) Faculty(fid: integer, fname: string, deptid: integer) The meaning of these relations is straightforward; for example, Enrolled has one record per student-class pair such that the student is enrolled in the class. Write the following queries in Oracle SQL. No duplicates should be printed in any of the answers. i) (2 points)...
Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) write the following...
Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) write the following queries in SQL: * Find the names of all suppliers who supply a green part. *Find the names of all suppliers who are from Illinois. *Find the names of all suppliers who sell a red part costing less than $100. *Find the names and colors of all parts that are green or red. In writing these queries you may make the following assumptions: a....
Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) In a plain...
Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) In a plain text file, write the following queries in SQL: 1.Find the names of all suppliers who supply a green part. 2.Find the names of all suppliers who are from Illinois. 3.Find the names of all suppliers who sell a red part costing less than $100. 4.Find the names and colors of all parts that are green or red. In writing these (basic) queries you may...
Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) In a plain...
Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) In a plain text file, write the following queries in SQL: Find the names of all suppliers who supply a green part. Find the names of all suppliers who are from Illinois. Find the names of all suppliers who sell a red part costing less than $100. Find the names and colors of all parts that are green or red. In writing these queries you may make...
Consider the following schema: Suppliers (sid, sname, address) Parts (pid, pname, colour) Catalog(sid, pid, cost) The key for Suppliers is sid, for Parts is pid, and for Catalog is sid and pid The Ca...
Consider the following schema: Suppliers (sid, sname, address) Parts (pid, pname, colour) Catalog(sid, pid, cost) The key for Suppliers is sid, for Parts is pid, and for Catalog is sid and pid The Catalog relation associates prices charged for parts by suppliers. Write the following queries using relational algebra. For items (a) through (e), use the "sequences of assignments" form. For items (f) and (g), use the "expression tree" form. List all assumptions. (Some marks will be given for the quality of your answers.) (a) Find...
Consider the following relational model for a basketball league: • Player (PlayerID, PName, Position, TeamID) •...
Consider the following relational model for a basketball league: • Player (PlayerID, PName, Position, TeamID) • Team (TeamID, TeamName, Venue) • Game (GameNo, Date, Time, HomeTeamID, AwayTeamID) • Record (GameNo, PlayerID, Points, Rebounds, Assists) In this basketball league, each team has a unique name and each player plays for only one team. One team has at least 10 players. Two teams (home team versus away team) participate in each game at home team’s venue. Each team meets all other teams...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT