Chapter 6 relational database

6.1 relational database

6.1.1 What is the relationship between data

  Relational database is a database from multiple tables can be composed of interconnected, that the link between objects and objects. Between the object and the object is to establish contact through primary and foreign keys. When each individual objects is made, you can search for data association table by a foreign key. For example, students and establish a relationship class, you can search for data class object by object class number of students.

1, the primary key

  Object has a certain property (field) or a group (a plurality of fields) that uniquely identifies an entity (record), the attribute group can be a primary key. A primary key is to uniquely identify the entity.

  such as:

  Class (class number, class name), where each class is unique ID number, number is a primary key. 

  Student (Student ID, name, sex, number of classes), in which each student is a unique student number, student number is a primary key.

  Curriculum (course number, course name, credits), which is the only course number, course number is a primary key. 

  Results table (student number, course number, results), the results table single attribute can not uniquely identify a record, a combination of student number and the course number before they can uniquely identify a record, so student number and the course number of property groups is a primary key . 

2, the outer key

 Field is used to create relationships between objects and the establishment of the primary key of another object that it is not the primary key of the object itself, but the relationship with the object. With foreign key, the object and another object can be associated.

  For example: Students object class number, the primary key object that it is not a student, but it is the primary key class object, it will associate the students and classes.

  Similarly, the number of high school transcripts and curriculum, student achievement and curriculum and achievement associate.

Design object relationships 6.1.2

  Each table in a relational database has a unique record identifier field, which is called the primary key. A primary key is part of the definition of the relationship; the other is a foreign key. If the primary key values ​​in a table a value of another field in the same table, the foreign key field is the first table. A foreign key can be used as a copy of the master key of another table. The primary key value table and a foreign key to another table of matches, can establish a relationship between two tables. When the relationship between design objects, the object-relational divided into many, many relationships.

1-to-many relationship between design

  Class and student-to-many relationship is a class that contains more than one student, and the student may be attributable to a particular class. Design many relationship, in the "many" table party add a foreign key field, corresponding to the "a" primary key table.

  

2, design-many relationship

 Students and curriculum are many relationships, a student may have many courses, a course can also be more than one student elective. Design many relationships, to create an intermediate table (intermediate objects), create two objects associated foreign key in the middle of the object, the object corresponding to the two associated primary key. While creating two joint field associated objects (score). With the common field there is a relationship exists, the demise and the demise of the relationship.

  

 

6.2 Relationship custom fields

 .1 6.2 What is the relationship between custom fields

   We can create relationships between objects by creating an object from the "Relationship custom fields" another object. Relationship field is a custom field object record that contains a link to another record.

1. Find a relationship field

  Creating a link to this object to another object relationship. Relationship field allows the user to click the Find icon to select a value from the pop-up list. Another object is the source of Values ​​in List. For example, to add a point to the object in the student class record lookup relationship fields, multiple records of students will be associated with the object to a class record. Add a point to find the relationship field student records in the results table object, multiple table records the results will be linked to a student record.

2, the main - Details of a relationship field

  Parent-child relationship between a particular type of object to create a (child or "Details") with another object (parent or "master"), wherein:

  • Relationship field records of all the details required.
  • Ownership and shared details of the record is determined by the master record.
  • When a user deletes master record, record all the details will be deleted.
  • You can create a cumulative summary fields in summary record detailed information on the master record.

  Relationship field allows the user to click the Find icon to select a value from the pop-up list. The main object is the source list values.

 

6.2 .2 create custom fields relationship

1. Create a foreign key

  Example: Creating Jobs relationships with hiring managers: Recruitment and hiring managers is many relationship, add a foreign key in a multi-table, find the relationship field is added in the target position.

  (1) According to the New Field Wizard, create a relationship field is completed.

 

  (2) field created in the post layout window, adjust the layout, in the new role, you will see the following results.

 

2, create to-many relationship

  例:创建职位与应聘者关系:职位与应聘者是多对多关系,需要创建一个中间表—职位申请表来创建两个对象间的关系。应聘者在提交应聘材料时,可以创建多个职位申请,一个职位可以被多个应聘者申请,在职位申请表添加职位和应聘者外键。如果还需要跟踪其他信息,比如说应聘者当前是否已经安排了这些职位中某一个的面试,该应聘者是否被录取或拒绝等,这时需要在职位申请表添加状态字段。

  (1)创建职位申请对象

  

  (2)创建字段

  

  这里创建关系使用主-详细关系是因为当应聘者或职位删除之后,与应聘者或职位相关的职位申请记录没有存在的必要,同时还可以统计申请职位的人数

  (3)调整应聘者布局,在相关列表处找到职位申请部分,添加创建日期和职位、状态字段

  

  点击应聘者选项卡,在应聘者窗口可以申请职位。

  

 

  申请2条职位。

   

 

  点击应聘者选项卡,在应聘者窗口可以看到如下效果: 

  

 

    (4)创建职位申请选项卡

  

  在职位申请窗口能看到刚才申请的2个职位。

      

 (5)关联操作

  在职位选项卡,编辑职位,改变其状态。

    

  再回到应聘者信息窗口,发现状态也已改变。

 

 

  (6)思考:如下效果

  调整职位布局,您将发现什么?

  是否可以将职位布局的“新建职位申请”按钮移除?

  是否可以按申请编号降序排列?

   

 

6.3 累计汇总字段

6.3 .1 什么是累计汇总字段

  在相关列表中显示字段的汇总值、最小值或最大值的只读字段,或在相关列表中列出的所有记录的记录数。只有主-详细关系的主对象才能创建累计汇总字段。比如,要统计学生所修的学分,可以在学生对象创建一个总学分字段,此字段类型为累计汇总,此字段的值从关联对象成绩表中筛选出来。如果要统计招聘的某一职位有多位应聘者申请,可以在职位对象创建一个申请人数字段,用于统计申请该职位的人数,同样该字段的值从关联对象职位申请中筛选出来。

6.3 .2 创建累计汇总字段

1、在职位对象创建申请人数字段。

2、创建完成后调整应聘者布局申请职位部分,添加申请人数字段,效果如下:

 

【总结】

1、关系数据库是由多张能互相联系的表格组成的数据库,即对象与对象之间的联系。对象与对象之间是通过主键和外键来建立联系的。当各个独立的对象建立了关系以后,就可以通过外键来搜索关联表的数据。

2、我们可以通过创建从某个对象到另一个对象的“关系定制字段”在对象间创建关联关系。关系字段是对象记录的一个定制字段,它包含一个指向另一个记录的链接。

【练习】

1、创建班级、学生、课程表、成绩表对象,并创建对象间的关联关系。

班级(班级编号,班级名称)

学生(学号,姓名,性别,班级编号)

课程表(课程编号,课程名,学分)

成绩表(学号,课程号,成绩)

2、如果需要查询学生的总学分,就如何做?

 

Guess you like

Origin www.cnblogs.com/drink186/p/11180573.html