Link tables and keys Access/IPS Walsall College of Arts & Technology.

18
Link tables and Link tables and keys keys Access/IPS Access/IPS Walsall College of Arts & Walsall College of Arts & Technology Technology

Transcript of Link tables and keys Access/IPS Walsall College of Arts & Technology.

Page 1: Link tables and keys Access/IPS Walsall College of Arts & Technology.

Link tables and keysLink tables and keys

Access/IPSAccess/IPS

Walsall College of Arts & Walsall College of Arts & TechnologyTechnology

Page 2: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

22

What we are going to coverWhat we are going to cover

What are primary and foreign keys?What are primary and foreign keys? Where do we use them?Where do we use them? What is a link table?What is a link table? Why use one?Why use one?

Anything else we feel like discussingAnything else we feel like discussing

Page 3: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

33

Attributes – a reminderAttributes – a reminder

A category of information about an A category of information about an entityentity Eg. The common attributes held about a Eg. The common attributes held about a

student may be:student may be: NameName UCAS NumberUCAS Number Degree schemeDegree scheme Year of studyYear of study Date of BirthDate of Birth

Page 4: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

44

KeysKeys

What is a key?What is a key? A A keykey is an is an attributeattribute that is that is alwaysalways unique unique

Why do we use them?Why do we use them? To hold data in a database, each record must To hold data in a database, each record must

be uniquebe unique For it to be unique, the For it to be unique, the primary keyprimary key must be must be

different in each recorddifferent in each record

Page 5: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

55

Primary key examplePrimary key example

We’ll use two Computer Science students in a We’ll use two Computer Science students in a university databaseuniversity database

So STUDENT is the entitySo STUDENT is the entity

Which attributes are unique?Which attributes are unique? We could use Student Name, UCAS Number We could use Student Name, UCAS Number

or DoBor DoB

Student Name UCAS Number Year DoBDegree Scheme

Rufus T Firefly 017653489 1 04/11/82Computer Science

Neddie Seagoon 014859602 1 12/03/1982Computer Science

Page 6: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

66

Picking your primary keyPicking your primary key

From the previous slide we had Student Name, From the previous slide we had Student Name, UCAS number and DoB as possible primary UCAS number and DoB as possible primary keyskeys

These are called These are called candidate keyscandidate keys.. Let’s look closely, if the database is for the Let’s look closely, if the database is for the

whole university is Student Name a suitable whole university is Student Name a suitable choice? Common names are likely to repeatchoice? Common names are likely to repeat

DoB is not ideal as it is not very meaningfulDoB is not ideal as it is not very meaningful

Page 7: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

77

…….and the winner is.and the winner is

UCAS Number!UCAS Number! Using our own knowledge, we know that Using our own knowledge, we know that

UCAS number is always unique and that every UCAS number is always unique and that every student has onestudent has one

So we can use this to make sure every So we can use this to make sure every recordrecord is uniqueis unique

Page 8: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

88

Keys and E-R diagramsKeys and E-R diagrams

With a With a One-to-OneOne-to-One relationship, each entity relationship, each entity has its own primary keyhas its own primary key

With a With a One-to-ManyOne-to-Many relationship, it is a little relationship, it is a little differentdifferent

On the next slide we will examine… On the next slide we will examine…

Page 9: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

99

Keys and One-to-ManyKeys and One-to-Many

Continuing the university themeContinuing the university theme STUDENT-DEGREE SCHEME is a STUDENT-DEGREE SCHEME is a one to one to

manymany as a student can only do one degree as a student can only do one degree scheme, but a degree scheme can have many scheme, but a degree scheme can have many students on it.students on it.

If we are to show this relationship in our If we are to show this relationship in our tables, we have to put the primary key of one tables, we have to put the primary key of one of the entities in the table of the otherof the entities in the table of the other

Page 10: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

1010

Foreign keysForeign keys

When we do this, the travelling primary key is When we do this, the travelling primary key is called a called a foreign keyforeign key..

With a one to many, we always put the foreign With a one to many, we always put the foreign key in the “many” table.key in the “many” table.

Eg. If the primary key of student is UCAS Eg. If the primary key of student is UCAS number and the primary key of Degree scheme number and the primary key of Degree scheme is Degree Name, the Student table will include is Degree Name, the Student table will include Degree Name as an attributeDegree Name as an attribute

This is shown in the table we used earlierThis is shown in the table we used earlier

Page 11: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

1111

For your convenienceFor your convenience

To save you from having to go back through To save you from having to go back through the slides, here’s that table again:the slides, here’s that table again:

So the foreign key is “Degree Scheme”So the foreign key is “Degree Scheme”

However, However, Many-to-ManyMany-to-Many relationships present relationships present more problemsmore problems

Student Name UCAS Number Year DoBDegree Scheme

Rufus T Firefly 017653489 1 04/11/82Computer Science

Neddie Seagoon 014859602 1 12/03/1982Computer Science

Page 12: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

1212

Many-to-ManyMany-to-Many

Unfortunately, we can’t transfer keys, because Unfortunately, we can’t transfer keys, because it would result in lists and duplications in both it would result in lists and duplications in both tables:tables:

STUDENT-MODULE is a STUDENT-MODULE is a many-to-manymany-to-many because a student takes many modules and a because a student takes many modules and a module has many students on itmodule has many students on it

If we have a table for Students and a table for If we have a table for Students and a table for Modules, and if we transfer the keys we end Modules, and if we transfer the keys we end up in a right mess…up in a right mess…

Page 13: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

1313

That’s another fine mess…That’s another fine mess…

If we transfer the keys, the tables look like If we transfer the keys, the tables look like this:this:

But this is impossible, because the primary key But this is impossible, because the primary key is not unique!is not unique!

Student Name UCAS Number Year DoBDegree Scheme

Rufus T Firefly 017653489 1 04/11/82Programming

Rufus T Firefly 017653489 1 04/11/82Databases

Rufus T Firefly 017653489 1 04/11/82Networking

Page 14: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

1414

So what do we do?So what do we do?

We use a We use a link tablelink table A link table is a purpose built table designed to A link table is a purpose built table designed to

break up many-to-many break up many-to-many relationshipsrelationships, by , by converting them to one-to-many’sconverting them to one-to-many’s

So what does the link table contain?So what does the link table contain?

STUDENT MODULELINK

Page 15: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

1515

A Link tableA Link table

The link table contains both primary keys (As The link table contains both primary keys (As foreign keys)foreign keys)

These foreign keys are then combined to make These foreign keys are then combined to make a a joint primary keyjoint primary key

In other words, either part of the key can In other words, either part of the key can repeat in the table, but a combination can’trepeat in the table, but a combination can’t

See next slide for exampleSee next slide for example

Page 16: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

1616

Link table exampleLink table example

The link table for STUDENT-MODULE looks The link table for STUDENT-MODULE looks like this:like this:

However, this would not be allowed:However, this would not be allowed:

UCAS Number Module Name

017653489

014859602 Databases

DatabasesThese are the same, but the

other part of the key is different

UCAS Number Module Name

017653489 Databases

017653489 Databases

Page 17: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

1717

So what now?So what now?

There is an exercise on this subject as well as There is an exercise on this subject as well as the solutionthe solution

These are available on my personal website, These are available on my personal website, the address is:the address is:

http://www.geocities.com/http://www.geocities.com/exiledsaddlerexiledsaddler/Teaching./Teaching.htmhtm

Page 18: Link tables and keys Access/IPS Walsall College of Arts & Technology.

November 2002November 2002 Walsall College of Arts & TechnologWalsall College of Arts & Technologyy

1818

That’s all folksThat’s all folks

That’s it for now, if you have any further That’s it for now, if you have any further questions, feel free to ask mequestions, feel free to ask me

If you can’t find me, you can email me your If you can’t find me, you can email me your questions to [email protected] to [email protected]