Oracle数据库-primary key/foreign 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; +----+--------+------+----------+ 不然,当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; +--------+---------+ 2.foreign key 外键的行使前提有三个 ① 两个表必需是InnoDB表,MyISAM表暂且不支持外键 我们成立一个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。 ? 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、截取时刻到秒暂且不知道怎么操纵 (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |