可以在创建表的时候就建立外键约束
create table student_inf (STUDENTID number not null primary key ,STUDENTNAME nvarchar2(20) ,GENDER nvarchar2(20) ,IDENTIFIED_STU nvarchar2(20) ,ADDRESS nvarchar2(20) ,STUDENTIMG nvarchar2(20) ,CLASSID number,constraint fk_CLASSID foreign key(CLASSID) references class_inf(CLASSID) );
也可以创建表之后建立外键约束
alter table student_inf add constraint fk_CLASSID foreign key(CLASSID) references class_inf(CLASSID);
constraint 外键名【说明:外键名一般fk_开头】
foreign key(要设为外键的字段)
references 表2名【说明:与哪个表关联】(表2中的字段名)