Ch 8 Pretest Flashcards
The Member table will have the following columns:
- ID—positive integer
- FirstName—variable-length string with up to 100 characters
- MiddleInitial—fixed-length string with 1 character
- LastName—variable-length string with up to 100 characters
- DateOfBirth—date
- AnnualPledge—positive decimal value representing a cost of up to $999,999, with 2 digits for cents
Write a SQL statement to create the Member table.
CREATE TABLE Member (
ID INT UNSIGNED,
FirstName VARCHAR(100),
MiddleInitial CHAR(1),
LastName VARCHAR(100),
DateOfBirth DATE,
AnnualPledge DECIMAL(8,2) UNSIGNED
);
The Rating table has the following columns:
- RatingCode—variable-length string, primary key
- RatingDescription—variable-length string
The Movie table should have the following columns:
- Title—variable-length string, maximum 30 characters
- RatingCode—variable-length string, maximum 5 characters
Write a SQL statement to create the Movie table. Designate the RatingCode column in the Movie table as a foreign key to the RatingCode column in the Rating table.
CREATE TABLE Movie (
Title VARCHAR(30),
RatingCode VARCHAR(5),
FOREIGN KEY(RatingCode) REFERENCES Rating(RatingCode)
);
The Movie table has the following columns:
- ID—integer, primary key
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
A new column must be added to the Movie table:
- Column name: Score
- Data type: decimal(3,1)
Write a SQL statement to add the Score column to the Movie table.
ALTER TABLE Movie
ADD Score Decimal(3,1);
The Movie table has the following columns:
- ID—integer, primary key
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
Write a SQL statement to create a view named MyMovies that contains the Title, Genre, and Year columns for all movies. Ensure your result set returns the columns in the order indicated.
CREATE VIEW MyMovies AS
SELECT Title, Genre, Year
FROM Movie;
A database has a view named MovieView.
Write a SQL statement to delete the view named MovieView from the database.
DROP VIEW MovieView;
The Movie table has the following columns:
- ID—integer
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
Write a SQL statement to modify the Movie table to make the ID column the primary key.
ALTER TABLE Movie
ADD PRIMARY KEY (ID);
The Movie table has the following columns:
- ID—integer, primary key
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
The YearStats table has the following columns:
- Year—integer
- TotalGross—bigint unsigned
- Releases—integer
Write a SQL statement to designate the Year column in the Movie table as a foreign key to the Year column in the YearStats table.
ALTER TABLE Movie
ADD FOREIGN KEY (Year) REFERENCES YearStats(Year);
The Movie table has the following columns:
- ID—integer, primary key
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
Write a SQL statement to create an index named idx_year on the Year column of the Movie table.
CREATE INDEX idx_year
ON Movie (Year);
The Movie table has the following columns:
- ID—integer, primary key, auto-increment
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
The following data needs to be added to the Movie table:
|Title|Genre|RatingCode|Year|
|—|—|—|—|
|Pride and Prejudice|Romance|G|2005|
Write a SQL statement to insert the indicated data into the Movie table.
INSERT Movie (Title, Genre, RatingCode, Year)
VALUES (‘Pride and Prejudice’, ‘Romance’, ‘G’, 2005);
The Movie table has the following columns:
- ID—integer, primary key
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
Write a SQL statement to delete the row with the ID value of 3 from the Movie table.
To remove COLUMNS, you would use an ALTER TABLE statement. For this question it is asking to delete a row. We use a DELETE & WHERE clause.
DELETE FROM Movie
WHERE ID = 3;
The Movie table has the following columns:
- ID—integer, primary key
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
Write a SQL statement to update the Year value to be 2022 for all movies with a Year value of 2020.
UPDATE Movie
SET Year = 2022
WHERE Year = 2020;
The database contains a table named Movie.
Write a SQL query to return all data from the Movie table without directly referencing any column names.
SELECT *
FROM Movie;
The Movie table has the following columns:
- ID—integer, primary key
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
Write a SQL query to retrieve the Title and Genre values for all records in the Movie table with a Year value of 2020. Ensure your result set returns the columns in the order indicated.
Select Title, Genre
FROM Movie
WHERE Year = 2020;
The Movie table has the following columns:
- ID—integer, primary key
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
Write a SQL query to display all Title values in alphabetical order A–Z.
SELECT Title
FROM Movie
ORDER BY Title;
The Movie table has the following columns:
- ID—integer, primary key
- Title—variable-length string
- Genre—variable-length string
- RatingCode—variable-length string
- Year—integer
Write a SQL query to output the unique RatingCode values and the number of movies with each rating value from the Movie table as RatingCodeCount. Sort the results by the RatingCode in alphabetical order A–Z. Ensure your result set returns the columns in the order indicated.
SELECT RatingCode, COUNT(*) AS RatingCodeCount
FROM Movie
GROUP BY RatingCode
ORDER BY RatingCode ASC;