设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 3392|回复: 5
打印 上一主题 下一主题

Relationships between Tables

[复制链接]
跳转到指定楼层
1#
发表于 2005-8-29 02:28:00 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
文章标题:Relationships between Tables

文章来源:http://allenbrowne.com/casu-06.html

原文:

Database beginners sometimes struggle with what tables are needed, and how to relate one table to another. It's probably easiest to follow with an example.

As a school teacher, Margaret needs to track each student's name and home details, along with the subjects they have taken, and the grades achieved. To do all this in a single table, she could try making fields for:    Name

    Address

    Home Phone

    Subject

    Grade

But this structure requires her to enter the student's name and address again for every new subject! Apart from the time required for entry, can you imagine what happens when a student changes address and Margaret has to locate and update all the previous entries? She tries a different structure with only one record for each student. This requires many additional fields - something like:    Name

    Address

    Home Phone

    Name of Subject 1

    Grade for Subject 1

    Name of Subject 2

    Grade for Subject 2

    Name of Subject 3

    ...

But how many subjects must she allow for? How much space will this waste? How does she know which column to look in to find "History 104"? How can she average grades that could be in any old column? Whenever you see this repetition of fields, the data needs to be broken down into separate tables.

The solution to her problem involves making three tables: one for students, one for subjects, and one for grades. The Students table must have a unique code for each student, so the computer doesn't get confused about two students with the same names. Margaret calls this field StudentID, so the Students table contains fields:    StudentID  a unique code for each student.

    Surname    split Surname and First Name to make searches easier.

    FirstName

    Address    split Street Address, Suburb, and Postcode for the same reason.

    Suburb

    Postcode

    Phone

The Subjects table will have fields:    SubjectID  a unique code for each subject. (Use the school's subject code)

    Subject    full title of the subject

    Notes      comments or a brief description of what this subject covers.

The Grades table will then have just three fields:    StudentID  a code that ties this entry to a student in the Students table

    SubjectID  a code that ties this entry to a subject in the Subjects table

    Grade      the mark this student achieved in this subject.

After creating the three tables, Margaret needs to create a link between them, by selecting the Database Container window, and choosing Relationships from the Edit menu. ("Grades" will be the "related table" in relationships with both the other tables.)

Now she enters all the students in the Students table, with the unique StudentID for each. Next she enters all the subjects she teaches into the Subjects table, each with a SubjectID. Then at the end of term when the marks are ready, she can enter them in the Grades table using the appropriate StudentID from the Students table and SubjectID from the Subjects table.

To help enter marks, she creates a form, using the "Form/Subform" wizard: "Subjects" is the source for the main form, and "Grades" is the source for the subform. Now with the appropriate subject in the main form, and adds each StudentID and Grade in the subform.

The grades were entered by subject, but Margaret needs to view them by student. She creates another form/subform, with the main form reading its data from the Students table, and the subform from the Grades table. Since she used StudentID when entering grades in her previous form, Access links this code to the one in the new main form, and automatically <
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2006-10-12 22:11:00 | 只看该作者
kjkjkj
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-29 20:58 , Processed in 0.082381 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表