Question

In: Computer Science

Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The...

Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The first database table will contain the names of at least four movies. The second table will be a list of actors who appear in the movies. The third table will be an associative table that describes the relationship between the actors and their movies (which actors appear in which movies). Actors and movies have a “many-to-many relationship,” meaning an actor can be in multiple movies and a movie can have multiple actors.

  • Use the MySQL Database Instructions listed below to create the three tables.
  • Once the three tables are completed, create a “join” statement that joins data from all three tables. Remember that field names used in more than one table (ASIN, ActorID) must be fully qualified (i.e., dvdtitle, ASIN).
  • The previous SQL query returns a separate record for each actor. Provide a way to group results from several records into a single row.

General guidelines for this:

  • String values in SQL statements are always enclosed within single quotes. ASIN and Title are both strings and require single quotes. For instance, your insert statement will look something like this:
    $sql = “Insert into dvdtitles (asin, title, price) VALUES (‘$asin’, ‘$title’, $price)”;
  • SQL debugging tip: check your SQL statement before you execute it. Put the following line of code immediately before the mysql_query($strSQL) statement.
  • When retrieving ASIN from the querystring, remember that ASIN is a string, not a number.
  • Explain in detail (like im 5) the process on how to group results from several records into a single row.

Table 1

1. Create a MySQL database table named “tbldvdtitles.” You will be adding information about a minimum of four of your favorite movies.

The table should have the following structure:

Field name Type Attributes
asin varchar(15)

primary

key

title varchar(100)
price double(5,2)

2. Write a SQL statement to add the ASIN, price, and title for your favorite movies.

Table 2

1. Create a second database table named “tbldvdActors.” Your table should have the following structure:

Field Type Attributes
actorID int(5) auto_increment, primary key
fname varchar(20)
lname varchar(20)
  1. Write a SQL statement to add at least four actors, one from each of your listed movies.

  2. Write a SQL statement to update the last actor fname, and lname information.

  3. Write a SQL statement to delete the first actor in the tbldvdActors table.

Table 3

  1. Create a third database table of relationships between actors and movie titles. The table should have the following structure:

Field Type Attributes
asin varchar(15) primary key (composite)
actorID int(5) primary key (composite)
  1. (Because this table uses a composite key, the delete statement must reference both the asin and actorID fields.)

  2. Add data that describes the relationship between your movies and actors.

Solutions

Expert Solution

SOLUTION:

The following solution has been implemented in MySQL. A separate database “DVD” has been created for this purpose.

The following 3 tables have been created

1. tbldvdTitles

2. tbldvdActors

3. tbldvdTitleActors

1. Create a MySQL database table named “tbldvdtitles.”

CREATE TABLE tbldvdTitles(asin VARCHAR(15) PRIMARY KEY, title VARCHAR(100), price DOUBLE);

A. Write a SQL statement to add the ASIN, price, and title for your favorite movies.

INSERT INTO tbldvdTitles VALUES('B079535TSM','The God Father',8.59);
INSERT INTO tbldvdTitles VALUES('B0019L21GA','The God Father Part II',10.27);
INSERT INTO tbldvdTitles VALUES('B00062IVKS','Raging Bull',12.94);
INSERT INTO tbldvdTitles VALUES('B08G9GW94R','The Irishman',22.99);

2. Create a second database table named “tbldvdActors”.

CREATE TABLE tbldvdActors(actorID INT PRIMARY KEY AUTO_INCREMENT, fname VARCHAR(20), lname VARCHAR(20));

A. Write a SQL statement to add at least four actors, one from each of your listed movies.

INSERT INTO tbldvdActors VALUES('Diane','Keaton');
INSERT INTO tbldvdActors VALUES('Marlon','Brando');
INSERT INTO tbldvdActors VALUES('Al','Pacino');
INSERT INTO tbldvdActors VALUES('Robert','De Niro');
INSERT INTO tbldvdActors VALUES('Joe','Pesci');

B. Write a SQL statement to update the last actor fname, and lname information.

UPDATE tbldvdActors SET fname='Joseph', lname='Frank Pesci' ORDER BY actorID DESC LIMIT 1;

C. Write a SQL statement to delete the first actor in the tbldvdActors table.

DELETE FROM tbldvdActors ORDER BY actorID ASC LIMIT 1;

3. Create a third database table of relationships between actors and movie titles.

CREATE TABLE tbldvdTitleActors(asin VARCHAR(15), actorID INT,
CONSTRAINT pk_dvdTitleActors PRIMARY KEY(asin, actorID),
CONSTRAINT fk_dvd_asin FOREIGN KEY(asin) REFERENCES tbldvdTitles(asin),
CONSTRAINT fk_dvd_actorID FOREIGN KEY(actorID) REFERENCES tbldvdActors(actorID));

A. Add data that describes the relationship between your movies and actors

INSERT INTO tbldvdTitleActors VALUES('B079535TSM',2);
INSERT INTO tbldvdTitleActors VALUES('B0019L21GA',3);
INSERT INTO tbldvdTitleActors VALUES('B00062IVKS',4);
INSERT INTO tbldvdTitleActors VALUES('B08G9GW94R',5);

Query combining Data from the 3 tables created.

SELECT tbldvdTitles.title, CONCAT(tbldvdActors.fname,' ',tbldvdActors.lname) AS "Actor"
FROM tbldvdTitles, tbldvdTitleActors, tbldvdActors
WHERE tbldvdTitles.asin = tbldvdTitleActors.asin
AND tbldvdTitleActors.actorID = tbldvdActors.actorID;

Grouping results from several records into a single row

For the purpose of grouping 2 records have been added.

INSERT INTO tbldvdTitleActors VALUES('B079535TSM',3);
INSERT INTO tbldvdTitleActors VALUES('B0019L21GA',4);

SELECT tbldvdTitles.title, COUNT(tbldvdActors.actorID) AS "Actors"
FROM tbldvdTitles, tbldvdTitleActors, tbldvdActors
WHERE tbldvdTitles.asin = tbldvdTitleActors.asin
AND tbldvdTitleActors.actorID = tbldvdActors.actorID
GROUP BY tbldvdTitles.title;

The above query gives the result of the number of actors in each movie.

Hope this helps.


Related Solutions

Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The...
Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The first database table will contain the names of at least four movies. The second table will be a list of actors who appear in the movies. The third table will be an associative table that describes the relationship between the actors and their movies (which actors appear in which movies). Actors and movies have a “many-to-many relationship,” meaning an actor can be in multiple...
Develop the SQL scripts that will create the tables and enforce all the appropriate constraints •...
Develop the SQL scripts that will create the tables and enforce all the appropriate constraints • Develop sample SQL scripts that will insert the data to the database (one row in each table) • Convert at least 2 entities to MongoDB Collections. Write the scripts that will Create the collection(s)
Using your downloaded DBMS (MS SQL Server), create a new database. Create the database tables based...
Using your downloaded DBMS (MS SQL Server), create a new database. Create the database tables based on your entities defining The attributes within each table The primary and foreign keys within each table *****Show your database tables, tables attributes, primary and foreign keys***** Do not forget to check the lesson slides and videos that show you how to convert an ER/EER into a database schema, and how to create a database and tables using MS SQL Server.
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as if it were being added to MySQL (please give explanations for each line of SQL code and a copy of the code as it would be entered into the query by itself: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
Write CREATE TABLE and INSERT INTO statements in order to create and populate five tables in...
Write CREATE TABLE and INSERT INTO statements in order to create and populate five tables in Oracle’s SQL*Plus.The information you need about the database ARE IN THE CHARTS BELOW. Each worksheet includes the following information about its associated table: ➢ Column names (for example, the jr_order table contains the orderID, customerID, orderDate, orderStatus, and orderShippedDate columns); ➢ Column data types (for example, orderID is INT, orderStatus is VARCHAR2(2), etc.); ➢ Column constraints, if any (for example, orderID in the jr_order...
SQL- Trigger I have two tables (below) I need to write a trigger that would delete...
SQL- Trigger I have two tables (below) I need to write a trigger that would delete everything for a pid from the Appt table if the pid is deleted from the patient table. Create table Appt(                 pid numeric Foreign Key references patient(pid),                 ptname varchar(50) Foreign Key references patient(name),                 dob date Foreign Key references patient(dob),                 dr varchar(20),                 appdate date,                 apptime time, ); and Create table Patient(                 pid numeric primary key,                 name varchar(50),                ...
Homework: Populate Sales Order tables. Write SQL to : 1. insert 5 records in each table:...
Homework: Populate Sales Order tables. Write SQL to : 1. insert 5 records in each table: Market, Region, ProductFamily, Manager (all Managers must have different % commissions, Commission is an attribute of the Manger). 2. Insert 5 records in Agent using all Managers 3. Insert 15 records in Product using  all ProductFamily 4. Insert 15 records in Customer using various Regions and Markets 5. Insert 50 records in SalesOrder using various Customers, Products, Agents Notes : ALL the Names ( Description)...
Using the sample.sql script, create the sample database in MySQL. Submit the MySQL interactive screen that...
Using the sample.sql script, create the sample database in MySQL. Submit the MySQL interactive screen that results. create database sample; use sample; create table customer (custno int auto_increment primary key, firstname varchar(20), middle varchar(20), lastname varchar(20), address varchar(60), telnum1 varchar(10), telnum2 varchar(10), telnum3 varchar(10), pin varchar(6), email varchar(30)); create table accttype (id int primary key, type varchar(10)); insert into accttype (id, type) values (1,'check'); insert into accttype (id, type) values (2,'save'); insert into accttype (id, type) values (3,'cd'); insert into...
Write create table statements to create tables for the Exoproduct employees computers database depicted by the...
Write create table statements to create tables for the Exoproduct employees computers database depicted by the relational schema created in a mini case MC5 in chapter 3. Use insert into statements to insert no fewer than 2 and no more than 10 records per table.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT