Table of Contents

Web Development Lesson 7 - Databases

Joins

Objective

In this activity you'll use data from two tables and you'll learn why separating data into multiple tables is a good idea.

Setup

Null Value

character_name hero_villain first_appeared power iron_man first_avenger thor avengers
Black Widow Hero 1964 Skill NULL NULL NULL True
Captain America Hero 1941 Biological NULL True NULL True
Hulk Hero 1962 Biological NULL NULL NULL True
Iron Man Hero 1963 Technology True NULL NULL True
Loki Villain 1949 Magic NULL NULL True True
Scarlet Witch Hero 1964 Magic NULL NULL NULL NULL

Multiple Tables

character_name hero_villain first_appeared power movie
Black Widow Hero 1964 Skill Iron Man
Black Widow Hero 1964 Skill Avengers
Captain America Hero 1941 Biological First Avenger
Captain America Hero 1941 Biological Avengers
Hulk Hero 1962 Bilogical Avengers
Iron Man Hero 1963 Technology Iron Man
Iron Man Hero 1963 Technology Avengers
Loki Villain 1949 Magic Avengers

Unique Index

Joins

SELECT *
FROM heroes
	LEFT JOIN appearances ON heroes.character_name=appearances.character_name
SELECT *
FROM heroes
	RIGHT JOIN appearances ON heroes.character_name=appearances.character_name
SELECT *
FROM heroes
	LEFT JOIN appearances ON heroes.character_name=appearances.character_name
SELECT character_name, hero_villain, first_appeared, power, movie
FROM heroes
	LEFT JOIN appearances ON heroes.character_name=appearances.character_name
SELECT heroes.character_name, hero_villain, first_appeared, power, movie
FROM heroes
	LEFT JOIN appearances ON heroes.character_name=appearances.character_name

Next: PHP Database Connections