Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Link tables and keys Access/IPS Walsall College of Arts & Technology."— Presentation transcript:

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

2 November 2002Walsall College of Arts & Technology 2 What 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 discussing Anything else we feel like discussing

3 November 2002Walsall College of Arts & Technology 3 Attributes – a reminder A category of information about an entity A category of information about an entity Eg. The common attributes held about a student may be: Eg. The common attributes held about a student may be: Name Name UCAS Number UCAS Number Degree scheme Degree scheme Year of study Year of study Date of Birth Date of Birth

4 November 2002Walsall College of Arts & Technology 4 Keys What is a key? What is a key? A key is an attribute that is always unique A key is an attribute that is always unique Why do we use them? Why do we use them? To hold data in a database, each record must be unique To hold data in a database, each record must be unique For it to be unique, the primary key must be different in each record For it to be unique, the primary key must be different in each record

5 November 2002Walsall College of Arts & Technology 5 Primary key example We’ll use two Computer Science students in a university database We’ll use two Computer Science students in a university database So STUDENT is the entity So STUDENT is the entity Which attributes are unique? Which attributes are unique? We could use Student Name, UCAS Number or DoB We could use Student Name, UCAS Number or DoB Student NameUCAS NumberYearDoBDegree Scheme Rufus T Firefly017653489104/11/82Computer Science Neddie Seagoon014859602112/03/1982Computer Science

6 November 2002Walsall College of Arts & Technology 6 Picking your primary key From the previous slide we had Student Name, UCAS number and DoB as possible primary keys From the previous slide we had Student Name, UCAS number and DoB as possible primary keys These are called candidate keys. These are called candidate keys. Let’s look closely, if the database is for the whole university is Student Name a suitable choice? Common names are likely to repeat Let’s look closely, if the database is for the whole university is Student Name a suitable choice? Common names are likely to repeat DoB is not ideal as it is not very meaningful DoB is not ideal as it is not very meaningful

7 November 2002Walsall College of Arts & Technology 7 ….and the winner is UCAS Number! UCAS Number! Using our own knowledge, we know that UCAS number is always unique and that every student has one Using our own knowledge, we know that UCAS number is always unique and that every student has one So we can use this to make sure every record is unique So we can use this to make sure every record is unique

8 November 2002Walsall College of Arts & Technology 8 Keys and E-R diagrams With a One-to-One relationship, each entity has its own primary key With a One-to-One relationship, each entity has its own primary key With a One-to-Many relationship, it is a little different With a One-to-Many relationship, it is a little different On the next slide we will examine… On the next slide we will examine…

9 November 2002Walsall College of Arts & Technology 9 Keys and One-to-Many Continuing the university theme Continuing the university theme STUDENT-DEGREE SCHEME is a one to many as a student can only do one degree scheme, but a degree scheme can have many students on it. STUDENT-DEGREE SCHEME is a one to many as a student can only do one degree scheme, but a degree scheme can have many students on it. If we are to show this relationship in our tables, we have to put the primary key of one of the entities in the table of the other If we are to show this relationship in our tables, we have to put the primary key of one of the entities in the table of the other

10 November 2002Walsall College of Arts & Technology 10 Foreign keys When we do this, the travelling primary key is called a foreign key. When we do this, the travelling primary key is called a foreign key. With a one to many, we always put the foreign key in the “many” table. With a one to many, we always put the foreign key in the “many” table. Eg. If the primary key of student is UCAS number and the primary key of Degree scheme is Degree Name, the Student table will include Degree Name as an attribute Eg. If the primary key of student is UCAS number and the primary key of Degree scheme is Degree Name, the Student table will include Degree Name as an attribute This is shown in the table we used earlier This is shown in the table we used earlier

11 November 2002Walsall College of Arts & Technology 11 For your convenience To save you from having to go back through the slides, here’s that table again: To save you from having to go back through the slides, here’s that table again: So the foreign key is “Degree Scheme” So the foreign key is “Degree Scheme” However, Many-to-Many relationships present more problems However, Many-to-Many relationships present more problems Student NameUCAS NumberYearDoBDegree Scheme Rufus T Firefly017653489104/11/82Computer Science Neddie Seagoon014859602112/03/1982Computer Science

12 November 2002Walsall College of Arts & Technology 12 Many-to-Many Unfortunately, we can’t transfer keys, because it would result in lists and duplications in both tables: Unfortunately, we can’t transfer keys, because it would result in lists and duplications in both tables: STUDENT-MODULE is a many-to-many because a student takes many modules and a module has many students on it STUDENT-MODULE is a many-to-many because a student takes many modules and a module has many students on it If we have a table for Students and a table for Modules, and if we transfer the keys we end up in a right mess… If we have a table for Students and a table for Modules, and if we transfer the keys we end up in a right mess…

13 November 2002Walsall College of Arts & Technology 13 That’s another fine mess… If we transfer the keys, the tables look like this: If we transfer the keys, the tables look like this: But this is impossible, because the primary key is not unique! But this is impossible, because the primary key is not unique! Student NameUCAS NumberYearDoBDegree Scheme Rufus T Firefly017653489104/11/82Programming Rufus T Firefly017653489104/11/82Databases Rufus T Firefly017653489104/11/82Networking

14 November 2002Walsall College of Arts & Technology 14 So what do we do? We use a link table We use a link table A link table is a purpose built table designed to break up many-to-many relationships, by converting them to one-to-many’s A link table is a purpose built table designed to break up many-to-many relationships, by converting them to one-to-many’s So what does the link table contain? So what does the link table contain? STUDENTMODULE LINK

15 November 2002Walsall College of Arts & Technology 15 A Link table The link table contains both primary keys (As foreign keys) The link table contains both primary keys (As foreign keys) These foreign keys are then combined to make a joint primary key These foreign keys are then combined to make a joint primary key In other words, either part of the key can repeat in the table, but a combination can’t In other words, either part of the key can repeat in the table, but a combination can’t See next slide for example See next slide for example

16 November 2002Walsall College of Arts & Technology 16 Link table example The link table for STUDENT-MODULE looks like this: The link table for STUDENT-MODULE looks like this: However, this would not be allowed: However, this would not be allowed: UCAS NumberModule Name 017653489 014859602Databases These are the same, but the other part of the key is different UCAS NumberModule Name 017653489Databases 017653489Databases

17 November 2002Walsall College of Arts & Technology 17 So what now? There is an exercise on this subject as well as the solution There is an exercise on this subject as well as the solution These are available on my personal website, the address is: These are available on my personal website, the address is: http://www.geocities.com/exiledsaddler/Teachi ng.htm http://www.geocities.com/exiledsaddler/Teachi ng.htm http://www.geocities.com/exiledsaddler/Teachi ng.htm http://www.geocities.com/exiledsaddler/Teachi ng.htm

18 November 2002Walsall College of Arts & Technology 18 That’s all folks That’s it for now, if you have any further questions, feel free to ask me That’s it for now, if you have any further questions, feel free to ask me If you can’t find me, you can email me your questions to sww0@aber.ac.uk If you can’t find me, you can email me your questions to sww0@aber.ac.uk


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

Similar presentations


Ads by Google