Docsity

Database Management: Creating Tables, Relationships, and Indexes, Slides of Database Programming

Partial preview of the text

Download Database Management: Creating Tables, Relationships, and Indexes and more Slides Database Programming in PDF only on Docsity!

Topic 02

Multiple Tables and Relationships

Queries with Totals

Forms, Indexes

Multiple Tables and Relationships

Creating Many-to-Ones | Queries |Referential Integrity | Unrelated Tables |

Cartesian Products | Relationship Diagrams

Creating Multiple Tables

The Movie table has a field called RatingCode:

movieNo title relyear runtime ratingcode

15 The Hunger Games 2012 142 PG 41 Silver Linings Playbook 2012 122 MA 135 The Hangover 2009 100 MA 156 X-Men Days of Future Past 2014 101 PG

What is a " PG " rated movie?

  • RatingName: "PARENTAL GUIDANCE"
  • Description: "Parental guidance recommended for persons under 15 years."

What is a " MA " rated movie?

  • RatingName: "MATURE ACCOMPANIED (15+)"
  • Description: "Persons under 15 years must be accompanied by a mature adult."

Creating Multiple Tables (cont.)

The Rating information should be stored in our Database in

a Table.

RATINGCODE RATINGNAME DESCRIPTION G General Suitable for all ages PG Parental Guidance Recommended for mature audiences 15 years and over M Mature (15+) Persons under 15 years must be accompanied by a mature adult MA Mature Accompanied Parental guidance recommended for persons under 15 years (15+) R Restricted (18+) Restricted to mature adults 18 years and over

Creating Relationships (cont.)

There is a connection

between Movie and

Rating

Drag the field

RATINGCODE in the

movie table and drop it

over RATINGCODE in

the Rating table

Creating Relationships (cont.)

This creates a Many to One

relationship

The movie.ratingcode field is

referred to as

a Foreign Key.

Each Foreign Key value in the

Movie table references a Primary

Key value in the Rating Table

Creating Relationships (cont.)

This is a Many to One relationship. Many FKs refer to one PK. Foreign Key movieNo title relyear runtime ratingcode 15 The Hunger Games 2012 142 PG 41 Silver Linings Playbook 2012 122 MA 135 The Hangover 2009 100 MA 156 X-Men Days of Future Past 2014 101 PG Primary Key RatingName Description ratingcode G General Suitable for all ages PG Parental Guidance Recommended for mature audiences 15 years and over M Mature (15+) Persons under 15 years must be accompanied by a mature adult MA Mature Accompanied (15+) Parental guidance recommended for persons under 15 years R Restricted (18+) Restricted to mature adults 18 years and over

Queries Using Relationships

Now design a new Query using both the Movie and Rating tables. As you add the tables to the query, the relationship is shown. This query has data from both tables. The query matches every FK value in the Movie table with a PK value in the Rating table.

Beware! Unrelated Tables

Imagine that you did not create the relationship between the Movie and Rating tables. They are simply two unrelated tables. When you create a query, the RDBMS does not know what these tables have in common There is no connection between the rating codes in the two tables (at least that's what the RDBMS thinks).

Beware! Cartesian Products

If you run a query that uses data from both unrelated tables, then a ' cartesian product ' is created. Every row in movie table matches every row in the ratings table. 318 movies * 5 ratings = 1590 rows! Utterly useless. Can be very slow to create. AVOID

…Another Table ( Actor )

The Movie Database also requires information about

actors

The following data about actors will be stored:

actorNo firstname surname gender 72129 Jennifer Lawrence F 51329 Bradley Cooper M 1245 Scarlett Johansson F 6968 Hugh Jackman M

Using Tables to Relate ( Actor/Movie )

We now have data about Movies and Actors

The next obvious task is to nominate the actors who appear in each

movie…

movieNo title relyear runtime ratingcode 70160 The Hunger Games 2012 142 PG 82693 Silver Linings Playbook 2012 122 MA 18785 The Hangover 2009 100 MA 127585 X-Men Days of Future Past 2014 101 PG actorNo firstname surname gender 72129 Jennifer Lawrence F 51329 Bradley Cooper M 1245 Scarlett Johansson F 6968 Hugh Jackman M

Using Tables to Relate  Casting Table

We will create a table called Casting

Each record in this table represents One Actor in One Movie

The CastId is simply a auto generated number to give each

row a unique identity (a primary key value).

Updated Relationship Diagram

Looks complicated, however, it is simply a collection of

multiple Many to One relationships

Each relationship has a Foreign Key referencing a Primary

Key