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, 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...
INTRO TO DATABASE Consider the Sailors-Boats-Reserves database described below. Sailors(sid: integer, sname: string, rating: integer, age:...
INTRO TO DATABASE Consider the Sailors-Boats-Reserves database described below. Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date) Write each of the following queries in SQL. 1. Find the names and ages of sailors who have reserved at least two boats. 2. For each boat reserved by at least 2 distinct sailors, find the boat id and the average age of sailors who reserved it.
Database Design and SQL The following relations keep track of airline flight information: Flights (flno: integer,...
Database Design and SQL The following relations keep track of airline flight information: Flights (flno: integer, from : string, to: string, distance: integer, departs: time, arrive: time, price: integer) Aircraft (aid: integer, aname : string, cruisingrange: integer) Certified (eid: integer, aid: integer) Employees (eid: integer, ename : string, salary: integer) The Employees relation describe pilots and other kinds of employees as well. Every pilot is certified for some aircraft and only pilots are certified to fly. Based on the schemas,...
The following tables form part of a database (Flights Database) held in a relational DBMS: employee...
The following tables form part of a database (Flights Database) held in a relational DBMS: employee (empNo, empName, empSalary, empPosition) aircraft (aircraftNo, acName, acModel, acFlyingRange) flight (flightNo, aircraftNo, fromAirport, toAirport, flightDistance, departTime, arriveTime) certified (empNo, aircraftNo) Where:  employee contains details of all employees (pilots and non-pilots) and empNo is the primary key;  aircraft contains details of aircraft and C is the primary key.  flight contains details of flights and (flightNo, aircraftNo) form the primary key.  certified...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT