Oracle数据库-主键(primary key)、外键(foreign key)、候选键(candidate key)、超键(super key)和references总结...
主要介紹一下個(gè)人對(duì)主鍵(primary key)、外鍵(foreign key)、候選鍵(Candidate key)、超鍵(super key)、references的總結(jié)
概念:
主鍵:用戶(hù)選擇元組標(biāo)識(shí)的一個(gè)候選鍵,主鍵不允許為空
外鍵:來(lái)描述兩個(gè)表的關(guān)系,外鍵可為空
超鍵:能唯一的標(biāo)識(shí)元組的屬性集
候選鍵:不含有多余屬性的超鍵
實(shí)例:
假如有以下學(xué)生和教師兩個(gè)表:
Student(student_no,student_name,student_age,student_sex,student_credit,teacher_no)
Teacher(teacher_no,teacher_name,teacher_salary)
超鍵:Student表中可根據(jù)學(xué)生編號(hào)(student_no),或身份證號(hào)(student_credit),或(學(xué)生編號(hào),姓名)(student_no,student_name),或(學(xué)生編號(hào),身份證號(hào))(student_no,student_credit)等來(lái)唯一確定是哪一個(gè)學(xué)生,因此這些組合都可以作為此表的超鍵
候選鍵:候選鍵屬于超鍵,且是最小的超鍵,即如果去掉超鍵組合中任意一個(gè)屬性就不再是超鍵了。Student表中候選鍵為學(xué)生編號(hào)(student_no),身份證號(hào)(student_credit)
主鍵:主鍵是候選鍵中的一個(gè),可人為決定,通常會(huì)選擇編號(hào)來(lái)作為表的主鍵。現(xiàn)分別選取student_no,teacher_no作為Student表,Teacher表的主鍵
外鍵:teacher_no為兩個(gè)表的公共關(guān)鍵字,且是Teacher表的主鍵,因此teacher_no是Student表的外鍵,用來(lái)描述Student表和Teacher表的關(guān)系
?
--References用法
創(chuàng)建一張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)????? --設(shè)置主鍵
);
?
創(chuàng)建一張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)?????? --設(shè)置主鍵
);
--創(chuàng)建外鍵約束
alter table Student add constraint FK_Student_References_Teacher (teacher_no) references Teacher(teacher_no);
?
轉(zhuǎn)載于:https://www.cnblogs.com/yangyang17/p/6039022.html
總結(jié)
以上是生活随笔為你收集整理的Oracle数据库-主键(primary key)、外键(foreign key)、候选键(candidate key)、超键(super key)和references总结...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java8 (1)
- 下一篇: Greenplum 数据库架构分析