考试报名
考试报名
考试内容
考试大纲
在线客服
返回顶部

备考刷题,请到

CDA认证小程序

For the table: emp(empno, ename, sal, deptno), where fields represent employee id, name, salary and department number; and dept(deptno, dname), where fields represent department number and name. Which of the following statements about foreign key constraints is correct?
A. Set empno in emp table as foreign key
B. Set deptno in emp table as foreign key
C. Set deptno in dept table as foreign key
D. Cannot set foreign key
上一题
下一题
收藏
点赞
评论
题目解析
题目评论(0)

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。

专业分析如下:

在关系数据库中,外键(Foreign Key)是用来建立和强化两个表之间的连接的。外键的作用是保证引用完整性,即确保一个表中的数据在另一个表中是存在的。

具体到题目中的两个表:
1. `emp` 表(员工表),包含字段 `empno`(员工编号)、`ename`(员工姓名)、`sal`(工资)、`deptno`(部门编号)。
2. `dept` 表(部门表),包含字段 `deptno`(部门编号)、`dname`(部门名称)。

从业务逻辑上看,每个员工属于一个部门,因此 `emp` 表中的 `deptno` 字段应该引用 `dept` 表中的 `deptno` 字段。这意味着 `emp` 表中的 `deptno` 字段应当是一个外键,指向 `dept` 表中的 `deptno` 字段。

因此,正确的外键设置是将 `emp` 表中的 `deptno` 字段设置为外键,引用 `dept` 表中的 `deptno` 字段。

其他选项的分析:
A: Set empno in emp table as foreign key
- `empno` 是员工表中的主键,不应作为外键。

C: Set deptno in dept table as foreign key
- `deptno` 是部门表中的主键,不应作为外键。

D: Cannot set foreign key
- 这是错误的,因为根据业务需求,`emp` 表中的 `deptno` 应该是 `dept` 表中的 `deptno` 的外键。

综上所述,正确答案是B。