CREATE TABLE Movies
(
Code INTEGER PRIMARY KEY NOT NULL,
Title TEXT NOT NULL, -- 标题
Rating TEXT --评分
);
CREATE TABLE MovieTheaters
(
Code INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,--名称
Movie INTEGER --电影
CONSTRAINT fk_Movies_Code REFERENCES Movies (Code)
);
INSERT INTO Movies
VALUES (9, 'Citizen King', 'G'),
(1, 'Citizen Kane', 'PG'),
(2, 'Singin'' in the Rain', 'G'),
(3, 'The Wizard of Oz', 'G'),
(4, 'The Quiet Man', NULL),
(5, 'North by Northwest', NULL),
(6, 'The Last Tango in Paris', 'NC-17'),
(7, 'Some Like it Hot', 'PG-13'),
(8, 'A Night at the Opera', NULL);
INSERT INTO MovieTheaters
VALUES (1, 'Odeon', 5),
(2, 'Imperial', 1),
(3, 'Majestic', NULL),
(4, 'Royale', 6),
(5, 'Paraiso', 3),
(6, 'Nickelodeon', NULL);
One, Two, Three
G
NC-17
的电影院SELECT Title
FROM Movies;
SELECT DISTINCT Rating
FROM Movies;
SELECT *
FROM Movies
WHERE Rating IS NULL;
SELECT *
FROM MovieTheaters
WHERE Movie IS NULL;
SELECT *
FROM MovieTheaters
LEFT JOIN Movies
ON MovieTheaters.Movie = Movies.Code;
SELECT *
FROM MovieTheaters
RIGHT JOIN Movies
ON MovieTheaters.Movie = Movies.Code;
/* With JOIN */
SELECT Movies.Title
FROM MovieTheaters
RIGHT JOIN Movies
ON MovieTheaters.Movie = Movies.Code
WHERE MovieTheaters.Movie IS NULL;
/* With subquery */
SELECT Title
FROM Movies
WHERE Code NOT IN
(
SELECT Movie
FROM MovieTheaters
WHERE Movie IS NOT NULL
);
One, Two, Three
INSERT INTO Movies
VALUES (10, 'One, Two, Three', NULL);
G
UPDATE Movies
SET Rating='G'
WHERE Rating IS NULL;
NC-17
的电影院DELETE
FROM MovieTheaters
WHERE Movie IN
(SELECT Code FROM Movies WHERE Rating = 'NC-17');