Oracle数据库-主键(primary key)、外键(foreign key)、候选键(candidate key)、超键(super key)和references总结...

主要介绍一下个人对主键(primary key)、外键(foreign key)、候选键(Candidate key)、超键(super key)、references的总结

概念:

主键:用户选择元组标识的一个候选键,主键不允许为空

外键:来描述两个表的关系,外键可为空

超键:能唯一的标识元组的属性集

候选键:不含有多余属性的超键

实例:

假如有以下学生和教师两个表:

Student(student_no,student_name,student_age,student_sex,student_credit,teacher_no)

Teacher(teacher_no,teacher_name,teacher_salary)

超键:Student表中可根据学生编号(student_no),或身份证号(student_credit),或(学生编号,姓名)(student_no,student_name),或(学生编号,身份证号)(student_no,student_credit)等来唯一确定是哪一个学生,因此这些组合都可以作为此表的超键

候选键:候选键属于超键,且是最小的超键,即如果去掉超键组合中任意一个属性就不再是超键了。Student表中候选键为学生编号(student_no),身份证号(student_credit)

主键:主键是候选键中的一个,可人为决定,通常会选择编号来作为表的主键。现分别选取student_no,teacher_no作为Student表,Teacher表的主键

外键:teacher_no为两个表的公共关键字,且是Teacher表的主键,因此teacher_no是Student表的外键,用来描述Student表和Teacher表的关系

 

--References用法

创建一张Student表:

Create table Student(

student_no number(10) not null,

student_name varchar2(10) not null,

student_age number(4) not null,

student_sex varchar2(4) not null,

student_credit  varchar2(18) not null,

teacher_no number(10) not null,

constraint PK_Student primary key(student_no)      --设置主键

);

 

创建一张Teacher表:

Create table Teacher(

teacher_no number(10) not null,

teacher_name varchar2(10) not null,

teacher_salary number(10) not null,

constraint PK_Teacher primary key(teacher_no)       --设置主键

);

--创建外键约束

alter table Student add constraint FK_Student_References_Teacher (teacher_no) references Teacher(teacher_no);

 

转载于:https://www.cnblogs.com/yangyang17/p/6039022.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值