COMMANDS Flashcards
(11 cards)
Create a view.
CREATE VIEW view_name
AS SELECT column_name(s)
FROM table_name
WITH CHECK OPTION;
Example: CREATE VIEW MyMovies AS SELECT Title, Genre, Year FROM Movie;
Delete View
DROP VIEW MovieView;
Create table with Foreign Key
CREATE TABLE table2(
column1 datatype,
column2 datatype,
…
CONSTRAINT constraint_name
FOREIGN KEY (column2)
REFERENCES table1(column1)
);
Creating Foreign Key on Existing Column
ALTER TABLE table_name2
ADD CONSTRAINT constraint_name
FOREIGN KEY(column_name2)
REFERENCES table_name1(column_name1);
Creating Indexes on New Table
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
INDEX(ID)
);
Creating Indexes on Existing Table
CREATE INDEX NAME_INDEX ON CUSTOMERS (Name);
The MySQL INSERT Statement
INSERT INTO TABLE_NAME (column1, column2, column3,…columnN)
VALUES (value1, value2, value3,…valueN);
The MySQL UPDATE Statement
UPDATE table_name SET field1 = new-value1, field2 = new-value2
[WHERE Clause]
MySQL ORDER BY Clause
SELECT * FROM Table
ORDER BY Column;
SELECT * FROM Movie
ORDER BY Title ASC;
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;
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 query to display both the Title and the TotalGross (if available) for all movies. Ensure your result set returns the columns in the order indicated.
SELECT
Movie.Title,
YearStats.TotalGross
FROM
Movie
LEFT JOIN
YearStats
ON
Movie.Year = YearStats.Year;