MySQL - 新手问题:这些表的 PK 和 FK 是什么? [英] MySQL - Newbie question: Which are the PK and the FK for these tables?

查看:65
本文介绍了MySQL - 新手问题:这些表的 PK 和 FK 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 张桌子:

员工 T

emp_id |姓名 |地址

emp_id | name | address

T 部

dep_id |名字

工资标准

emp_id |dep_id |月 |年 |工资

emp_id | dep_id | month | year | salary

对于每个表,主键和外键分别是什么?

For each table, what are the primary keys and the foreign keys?

我的回答:

表名|PK |FK|

  • 员工:emp_id |dep_id
  • 部门:dep_id ||emp_id
  • 薪资:emp_id、dep_id |emp_id,dep_id

我的回答正确吗?

推荐答案

EmployeesDepartment 表有主键(你猜对了),但没有外键.

The Employees and Department tables have primary keys (which you got correct), but no foreign keys.

Salaries中,emp_iddep_id都是外键.

Salaries 没有单一的主键,尽管 emp_iddep_idmonthyear 可能是一个复合键(因为这 4 个的组合总是唯一的,假设一个员工每个月只被一个部门支付一次工资:)).

There's no single primary key on Salaries, although emp_id, dep_id, month and year could possibly be a composite key (as the combination of these 4 would always be unique, assuming an employee is only salaried once per month by a department :) ).

这篇关于MySQL - 新手问题:这些表的 PK 和 FK 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆