|
Here is a complete script where three tables are created. The scenario is a video hire shop.
CUSTOMER-----====< LOAN >====-----FILM
Tables are created.
They are populated with data.
A SELECT query is then run on the three tables. Who has borrowed whch film?
This script was run by nbauers.
You should replace references to nbauers with your own username.
You can copy from this script by highlighting and using CRTL+C.
You can paste into PuTTY by right clicking the mouse.
CREATE DATABASE nbauers;
USE nbauers;
CREATE TABLE customer
(
customer_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY
KEY,
title VARCHAR(5),
last_name VARCHAR(20) NOT NULL,
first_name VARCHAR(20) NOT NULL,
date_of_birth DATE
);
insert into customer
(
title, last_name, first_name, date_of_birth
)
VALUES
(
"Mr", "Bloggs", "Fred", "1997-12-22"
);
SELECT * FROM customer;
create table film
(
film_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
film_title VARCHAR(50) NOT NULL
);
show tables;
insert into film
(
film_title
)
VALUES
(
"Lord of the Rings"
);
create table loan
(
customer_id MEDIUMINT UNSIGNED NOT NULL,
film_id MEDIUMINT UNSIGNED NOT NULL
);
show tables;
insert into loan
(
customer_id, film_id
)
VALUES
(
"1", "1"
);
insert into customer
(
title, last_name, first_name, date_of_birth
)
VALUES
(
"Ms", "Smith", "Sally", "1982-11-18"
);
insert into customer
(
title, last_name, first_name, date_of_birth
)
VALUES
(
"Mrs", "Zonk", "Zebadee", "1950-10-14"
);
insert into customer
(
title, last_name, first_name, date_of_birth
)
VALUES
(
"Mr", "Jones", "Sam", "1947-04-27"
);
insert into film
(
film_title
)
VALUES
(
"Lord of the Rings"
);
insert into film
(
film_title
)
VALUES
(
"War and Peace"
);
insert into film
(
film_title
)
VALUES
(
"ScoobyDoo"
);
SELECT * FROM film;
insert into loan
(
customer_id, film_id
)
VALUES
(
"2", "3"
);
insert into loan
(
customer_id, film_id
)
VALUES
(
"3", "2"
);
insert into loan
(
customer_id, film_id
)
VALUES
(
"4", "4"
);
SELECT * FROM loan;
SELECT
customer.first_name,
customer.last_name,
film.film_title
FROM
customer, loan, film
WHERE
customer.customer_id = loan.customer_id
AND
loan.film_id = film.film_id;
|