Based on the relationship between tables emp and dept, one department can have multiple employees, while one employee only belongs to one department. Therefore, empno in emp table can be set as primary key, and deptno as foreign key. Or deptno in dept table can be set as primary key.
正確答案是B:Set deptno in emp table as foreign key。
專業(yè)分析如下:
在關(guān)系數(shù)據(jù)庫中,外鍵(Foreign Key)是用來建立和強化兩個表之間的連接的。外鍵的作用是保證引用完整性,即確保一個表中的數(shù)據(jù)在另一個表中是存在的。
具體到題目中的兩個表:
1. `emp` 表(員工表),包含字段 `empno`(員工編號)、`ename`(員工姓名)、`sal`(工資)、`deptno`(部門編號)。
2. `dept` 表(部門表),包含字段 `deptno`(部門編號)、`dname`(部門名稱)。
從業(yè)務(wù)邏輯上看,每個員工屬于一個部門,因此 `emp` 表中的 `deptno` 字段應(yīng)該引用 `dept` 表中的 `deptno` 字段。這意味著 `emp` 表中的 `deptno` 字段應(yīng)當(dāng)是一個外鍵,指向 `dept` 表中的 `deptno` 字段。
因此,正確的外鍵設(shè)置是將 `emp` 表中的 `deptno` 字段設(shè)置為外鍵,引用 `dept` 表中的 `deptno` 字段。
其他選項的分析:
A: Set empno in emp table as foreign key
- `empno` 是員工表中的主鍵,不應(yīng)作為外鍵。
C: Set deptno in dept table as foreign key
- `deptno` 是部門表中的主鍵,不應(yīng)作為外鍵。
D: Cannot set foreign key
- 這是錯誤的,因為根據(jù)業(yè)務(wù)需求,`emp` 表中的 `deptno` 應(yīng)該是 `dept` 表中的 `deptno` 的外鍵。
綜上所述,正確答案是B。