首页 > 代码库 > 『ORACLE』 约束实验(11g)

『ORACLE』 约束实验(11g)

create table student 
(
   sno                  NUMBER(5)            not null,
   sname                VARCHAR2(20),
   idcard               VARCHAR2(18),
   createtime           DATE                 default SYSDATE,
   nno                  NUMBER(2),
   constraint PK_STUDENT primary key (sno)
);

create table course 
(
   cno                  NUMBER(5)            not null,
   cname                VARCHAR2(20),
   constraint PK_COURSE primary key (cno)
);

create table nativeplace 
(
   nno                  NUMBER(2)            not null,
   np_name              VARCHAR2(20),
   constraint PK_NATIVEPLACE primary key (nno)
);

create table sc 
(
   sno                  NUMBER(5)            not null,
   cno                  NUMBER(5)            not null,
   grade                NUMBER(4,1),
   constraint PK_SC primary key (sno, cno)
);

alter table sc add constraint fk_sc_sno foreign key (sno) references student (sno) on delete cascade;
alter table sc add constraint fk_sc_cno foreign key (cno) references course (cno);
alter table student add constraint fk_student foreign key (nno) references nativeplace (nno) on delete set null;

insert into nativeplace values(1,‘heilongjiang‘);
insert into nativeplace values(2,‘jilin‘);
insert into nativeplace values(3,‘liaoning‘);
insert into nativeplace values(4,‘beijing‘);

insert into course VALUES(1,‘Oracle‘);
insert into course VALUES(2,‘Java‘);
insert into course VALUES(3,‘C++‘);

insert into student values(1,‘zhangsan‘,‘123456789123456780‘,default,1);
insert into student values(2,‘lisi‘,‘123456789123456781‘,default,2);
insert into student values(3,‘wangwu‘,‘123456789123456782‘,default,3);
insert into student values(4,‘zhaoliu‘,‘123456789123456783‘,default,4);

insert into sc values(1,1,91);
insert into sc values(1,2,92);
insert into sc values(1,3,93);
insert into sc values(2,1,88);
insert into sc values(2,2,77);
insert into sc values(2,3,99);
insert into sc values(3,1,65);
insert into sc values(3,2,75);
insert into sc values(3,3,85);
insert into sc values(4,1,80);
insert into sc values(4,2,88);
insert into sc values(4,3,89);
commit;

alter table sc drop constraint fk_sc_sno;
alter table sc drop constraint fk_sc_cno;
alter table student drop constraint fk_student;
drop table course cascade constraints;
drop table nativeplace cascade constraints;
drop table sc cascade constraints;
drop table student cascade constraints;

『ORACLE』 约束实验(11g)