🗒️FIT 2094 W4 寻找键

Cs, Go!

FIT 2094|2021-7-2|最后更新: 2025-9-13|
Place
人员

三种键的概念

1. 超级键(Super Key):

  • 定义:任意一个能 唯一标识一行记录 的属性集合
  • 特点:可以包含冗余属性,不要求最小化
  • 方法:从业务规则出发,把可能唯一标识一行的字段组合都列出来,即使里面有多余的属性也没关系。

2. 候选键(Candidate Key)

  • 定义:从超级键中挑出来的 最小唯一属性集合(去掉任何一个属性就无法唯一标识记录)
  • 特点:不包含冗余属性,可能有多个
  • 方法:只要组合能唯一标识,就列出来,哪怕包含冗余属性。

3. 主键(Primary Key)

选择标准(仅供参考):
标准
说明
举例
稳定性
值不会频繁变更,避免更新困难和外键断裂
人的身份证号比电话号码稳定
简洁性
属性数量少,数据类型简单
(student_id) 优于 (first_name, last_name, birth_date)
非空性
在全表中不存在缺失值
邮箱地址可能为空,不适合作主键
业务独立性
不依赖业务规则中容易变化的内容
“办公室房间号”可能随搬迁变化
可读性(可选)
人类容易理解或记忆
图书ISBN、员工工号
  • 定义:在候选键中 选择的一个 作为表的主要唯一标识
  • 特点:数据库用它作为默认的行识别方式
  • 方法:从候选键中选出最优的一个作主键
    • 顺序建议
        1. 单一属性优于多属性
        1. 内部生成的、无业务含义的ID(如自增整数、UUID)往往稳定
        1. 如果多个候选键在稳定性和简洁性上相当,则任选其一即可

例子:牙医预约系统

In any relation, tuples must be unique. However, in many cases, the set of all the attributes in a relation is not considered a candidate key. Why not? On the other hand, suppose we do have a relation where the set of all attributes is a candidate key. In this case, show that this set must, therefore, be the only candidate key and hence the primary key.
2. Identify the primary key and foreign key for these three relations:
ORDER (order_id, order_date, cust_id)
ORDERLINE (order_id, prod_no, ol_qtyordered, ol_lineprice)
PRODUCT (prod_no, prod_desc, prod_unitprice)
3. Consider a relation that depicts a dental surgery appointment system.
APPOINTMENT (dentist_id, dentist_name, patient_id, patient_name, appointment_datetime, surgery_roomno)
Identify the superkey(s), candidate key(s) and the primary key for the relation if the following business rules are applicable:
  • A dentist can only see a single patient at a particular date and time
  • A dentist treats a patient in a particular surgery room (only one patient is treated in the room at a time), and
  • A patient can see the same dentist multiple times.
  • 那么寻找超级键就可以直接照搬:任何包含候选键全部属性的属性集合都是超级键的方法
    • SK:(dentist_id, dentist_name, patient_id, patient_name, appointment_datetime, surgery_roomno)
    • SK:(dentist_id, patient_id, appointment_datetime)
    • 寻找候选键
    • 核心目标:理解在实际业务中,哪些属性组合能唯一标识一条记录
    • 例如:在牙医预约表中,规则说 同一牙医同一时间只看一个病人,这立刻暗示 (dentist_id, appointment_datetime,patient_id) 能唯一,但是可能需要精简。
    • 列出所有超级键(Superkeys)
    • 原则:只要组合能唯一标识,就列出来,哪怕包含冗余属性。
    • 例:
      • (dentist_id, appointment_datetime)
      • (appointment_datetime, surgery_roomno)
      • (dentist_id, appointment_datetime, surgery_roomno)
      • (dentist_id, appointment_datetime, patient_id) …它们都是超级键。
  • 寻找主键
    • 思路:主键的选择其实要依赖你前面找到的候选键集合,根据上文要求:
      • A dentist can only see a single patient at a particular date and time. A dentist treats a patient in a particular surgery room (only one patient is treated in the room at a time), and A patient can see the same dentist multiple times.
      倾向选 (dentist_id, appointment_datetime)
    • 稳定性dentist_id 是医生的唯一标识,不会变;预约时间点一旦确定,也基本不会修改。
    • 简洁性:只有两个属性,而且类型简单(一个ID + 一个时间)。
    • 业务逻辑符合性:业务规则直接说“同一医生同一时间不会有两个病人”,跟这个组合完全吻合。
    • 避免位置依赖:房间号(surgery_roomno)可能会换,比如临时调整到别的手术室,但医生与时间的绑定关系更稳定。
    • 避免多重预约:避免患者在同一时间预约多个医生/诊疗室(patient_id)。
 
Loading...