MySQL - Advanced SQL
Mark Fontenot, PhD
Northeastern University
Installing Sakila in MySQL
2
Installing Sakila in MySQL
3
Create a New Table from Existing Data
Context is Sakila DB.
Task: Create a table that only contains information for films that start with the letter A.
4
CREATE TABLE films_start_with_a AS
SELECT *
FROM film
WHERE title LIKE 'A%';
Create a New Table from Existing Data
5
CREATE TABLE films_start_with_a AS
SELECT *
FROM film
WHERE title LIKE 'A%';
Database Views
6
Create View
7
CREATE VIEW <view name> AS
<some select statement>;
CREATE VIEW films_start_with_b AS
SELECT *�FROM film
WHERE title like ‘B%’;
Create Views
8
SELECT COUNT(*) AS count
FROM film
WHERE title LIKE 'B%';
SELECT COUNT(*) AS count
FROM films_start_with_b;
Why Views?
9
Alter Schema
MySQL and other RDBMSs allow you to modify the schema objects.
10
ALTER TABLE films_start_with_a
RENAME COLUMN title TO film_title;
ALTER TABLE films_start_with_a
ADD COLUMN subtitle VARCHAR(128) NULL;
ALTER TABLE films_start_with_a
DROP COLUMN subtitle;