加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

Oracle数据库-primary key/foreign key和references相关

发布时间:2021-01-25 18:42:16 所属栏目:站长百科 来源:网络整理
导读:首要先容一下小我私人对主键(primary key)、外键(foreign key)、候选键(Candidate key)、超键(super key)、references的总结 观念: 主键:用户选择元组标识的一个候选键,主键不应承为空 外键:来描写两个表的相关,外键可为空 超键:能独一的标识元组
副问题[/!--empirenews.page--]

首要先容一下小我私人对主键(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);

?

1.primary key

☆假如一个table有primary key,那么这个primary key 的value就不能为null,并且每条record就不能一再(完全沟通),不然会产生如下错误

A.当primary key置为null时:ERROR 1048 (23000): Column ‘id‘ cannot be null

B.当primary key 一再时:ERROR 1062 (23000): Duplicate entry ‘1‘ for key ‘PRIMARY‘

例子:create table t2 (?id int(4) not null primary key,--auto_increment,?name char(20) not null,?sex int(4) not null default ‘0‘,?degree double(16,2));

A.的sql语句:insert t2 values(1,‘www‘,1,99.8);当执行两遍时报错

B.的sql语句:insert t2 values(null,99.8);

功效:select * from t2;

+----+--------+------+----------+
| id ? | name | sex | degree |
+----+--------+------+----------+
| ?1 ? | www ?| ? 1 ? ?| ?99.80 ?|
+----+--------+------+----------+

不然,当table无primary key时

语句:create table t1 (?id int(4),?name char(20));

C.insert t1 values (1,‘www‘);可以执行n遍

D.insert t1 values (null,‘www‘);也可以执行n遍

功效:select * from t1;

+--------+---------+
| id ? ? ? ?| name |
+--------+---------+
| ? ?1 ? ? ? | www ?|
| ? ?1 ? ? ? | www ?|
| NULL | www ?|
+--------+--------+

2.foreign key

外键的行使前提有三个

① 两个表必需是InnoDB表,MyISAM表暂且不支持外键
? ? ?② 外键列必需成立了索引,MySQL 4.1.2往后的版本在成立外键时会自动建设索引,但假如在较早的版本则必要显式成立;
? ? ?③ 外键相关的两个表的列必需是数据范例相似,也就是可以彼此转换范例的列,好比int和tinyint可以,而int和char则不行以;

我们成立一个table:create table t1 (?id int(4),?name char(20))type=innodb;而且成立索引create index t1_index on t1(name);

然后再建一个table:create table t3( id int(4),name char(20),foreign key(name) references t1(name)type=innodb );

那么?insert t3 values(1,‘aaa‘);就会报错:ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`anwei`.`t3`,CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`name`) REFERENCES `t1` (`name`))

可是在完成insert t1 values(1,‘aaa‘);后就可以了,也就是values(1,‘aaa‘);就不报错了,可是其他那么的值就又会报错的了。

声名:name是允许null值的,以是null值不受限定。
---------------------

建设一个字段,起首是字段名,然后是字段数据范例,后头的就是对此字段的束缚,一样平常有not null ,unsigned,auto_increment,default 等等。

然后挨个表明标题中的五个观念

unique key:在一张表中可以标识一个列或多个列,因此它既可以看做列级束缚、也可以看做表级束缚。 用来限定一个列中全部元素都不一再,列元素可觉得null。
primary key:在一张表中只能有一个,来束缚该列中全部元素不一再,且不能为null。
以上两者接洽与区别:primary = unique + not null 是一个可取的说法;primary是一个索引,当体系要查询表中某笔记录时,可以通过primary key标志的列,快速查询到所需的记录,以是这里primary修饰的列一样平常都是事先划定好,就是用来举办索引的列,好比:id int unsigned not null auto_increment; 这种列。 而unique key 只是用来限定列元素不一再,并不必然用来快速检索,好比:我们想担保一个表中“身份证”字段的每一笔记录不一再? or? ?“电话号”字段每一笔记录不一再,就可以用unique举办束缚,而此时这样的列跟索引查找并没有相关。
foreign key:一个表中的 foreign key 指向另一个表中的 unique key (虽然也可所以primary key,并且一样平常就应该是primary key吧,事实primary key没null的值)。就是一张表中,foreign标识的列,个中每一个元素,都能对应到另一张表中unique标识的列元素。
index / key :两者一样平常可以划等号,平等对待。其成果:假如要像primary key标识的字段一样,可以或许操作索引快速的查询获得功效怎么办?primary key一张表只能配置一个,此时就可以用index / key 来给一个字段列加索引,从而大大加速该列中元素的查找速率。
---------------------

?

trunc 函数可用于截取日期时刻

用法:trunc(字段名,精度)

详细实例:

在表table1中,有一个字段名为sysdate,该行id=123,日期表现:2016/10/28 15:11:58

1、截取时刻到年时,sql语句如下:

select trunc(sysdate,‘yyyy‘) from table1 where id=123;? --yyyy也可用year替代

表现:2016/1/1

2、截取时刻到月时,sql语句:

select trunc(sysdate,‘mm‘) from table1 where id=123;

表现:2016/10/1

3、截取时刻到日时,sql语句:

select trunc(sysdate,‘dd‘) from table1 where id=123;

表现:2016/10/28

4、截取时刻到小时时,sql语句:

select trunc(sysdate,‘hh‘) from table1 where id=123;

表现:2016/10/28 15:00:00

5、截取时刻到分钟时,sql语句:

select trunc(sysdate,‘mi‘) from table1 where id=123;

表现:2016/10/28 15:11:00

6、截取时刻到秒暂且不知道怎么操纵

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读