Upload
candice-bennett
View
217
Download
0
Embed Size (px)
Citation preview
Table Design and Relationships
Name
Room
Phone Artist Song Song Song
Bob 212 555-1212
Jimmy Buffet
Brown Eyed Girl Son of a Son of a Sailor
Margaritaville
Sam 213 555-1232
Santana Devil Woman
Fred 215 555-4455
Beatles Dear Prudence Abbey Road Hey Jude
… … … … …
Mary 234 555-1245
Sarah McLachlan
Surfacing Winter Song Angel
… … … … …
Recall our music tracking system
Table Design and Relationships
Name Room Phone Artist Song
Bob 212 555-1212 Jimmy Buffet
Margaritaville
Bob 212 555-1212 Jimmy Buffet Brown Eyed Girl
Bob 212 555-1212 Jimmy Buffet Son of a Son of Sailor
… … … … …
Mary 234 555-1245 Sarah McLachlan
Angel
… … … … …
Recall our music tracking system
Table Design and Relationships
A relational database gets its power from the ability to manage multiple tables of data and the relationships among those tablesMS Access is a relational database management systemIn designing and building a relational database you will define the tables (the fields and their properties, and The relationships among these tables
Splitting the previous table into two tables…
ID Name Room Phone
1 Bob 212 555-1212
2 Mary 234 555-1245
… … … …
ID Artist Song
1 Jimmy Buffet
Margaritaville
1 Jimmy Buffet
Brown Eyed Girl
1 Jimmy Buffet
Son of a Son of Sailor
… … …
2 Sarah McLachlan
Angle
2 Rolling Stones
…
Database Design
Splitting the previous table into two tables… More efficient We don’t have to reenter the people information
for every song If we find a song in the song table how do we find
the owner in the people table In a relation one table has a field that is unique (no
other record can have the same value This field is called a Primary Key
A related table will have a field with matching value (as the primary field in the other table), … but it does not have to be unique –
This is called a foreign key In our example, ID is the foreign key in the people
table
Splitting the previous table into two tables…
ID Name Room Phone
1 Bob 212 555-1212
2 Mary 234 555-1245
… … … …
ID Artist Song
1 Jimmy Buffet
Margaritaville
1 Jimmy Buffet
Brown Eyed Girl
1 Jimmy Buffet
Son of a Son of Sailor
… … …
2 Sarah McLachlan
Angle
2 Rolling Stones
…
So finding the owner of a particular song… We find a song in the music table, and get the ID value Go to the People table and find a record
where the ID matches the one that we picking in the music table music.id = people.id
Once you have setup the database Access does all of this for you
Types of relationships in a relational database One to many – one record in table A matches
(potentially) many records in table B One to one – one record in table A matches only
one record in table B Many to many – records in table A can have
multiple matching records in table B --- and vis-versa This one is tricky Consider an Orders table and a Products table
Referential Integrity (what?!) If a record in one table is linked (has a
relationship) to one or more records in another table..
We must protect that link Referential Integrity – the assurance that this link
does not get broken How could we violate referential integrity?
Delete a record on one side of the relation without first deleting linked records on the other side.
If Bob moves we can’t delete his record from the people table without first deleting his songs from the music table
Add a record on the many side of the relation without the corresponding record existing on the one side of the relation
We can’t add the new guy – Fernando’s songs to the music table unless Fernando’s information is in the people table
Neighbor08a.mdb
Remember that we split our House Survey data into two relations (tables)…so now we need to create a table for the rest of the dataDon’t forget that each record must have a unique house identifier…and what we will use this for?
Neighbor08a.mdb
Make a relationship between these two tablesHow do you do this?What kind of relationship is this?
Neighbor08a.mdb
Then, we are also interested in who lives in these houses……so create another table for peopleYou will need – PersonID HouseID –to tell which house they live in FirstName LastName DOB (what data type?) CellPhone (use an input mask)
Neighbor08a.mdb
Make a relationship between what two tables?How do you do this?What kind of relationship is this?
College.mdb
Let’s look at another databaseSuppose you need to create a course registration system for college courses (or workshops, etc.)What are the objects that you need to work with? Students, Courses, ???
So, you have a table of studentsYou have table of coursesHow do you relate these two tables?Does this present any problems?