oracle创建表,设置主外键约束以及插入多条语句

描述:创建以下表和一个教师表

Oracle语句如下:

--学生表
create table student(
StudentNo number primary key,
LoginPwd nvarchar2(20) not null,
StudentName nvarchar2(20) not null,
Sex nvarchar2(2) not null,
GradeId number,
Phone nvarchar2(50),
Address nvarchar2(255),
BornDate date,
Email nvarchar2(50),
IdentityCard nvarchar2(18),
constraint fk_garde_student foreign key(GradeId) references student(StudentNo)
);

--年级表
create table grade(
GradeId number primary key,
SubjectNo number not null,
GradeName nvarchar2(50) not null,
constraint fk_subject_garde foreign key(SubjectNo) references grade(GradeId)
);

--教师表
create table teacher(
TeacherNo number primary key,
LoginPwd nvarchar2(20) not null,
TeacherName nvarchar2(20) not null,
Sex nvarchar2(2) not null,
GradeId number,
Phone nvarchar2(50),
Address nvarchar2(255),
BornDate date,
Email nvarchar2(50),
IdentityCard nvarchar2(18),
constraint fk_garde_teacher foreign key(GradeId) references teacher(TeacherNo)
);

--课程表
create table subject(
SubjectNo number primary key,
SubjectName nvarchar2(50) not null,
ClassHour number not null,
GradeId number not null,
constraint fk_garde_subject foreign key(GradeId) references subject(SubjectNo)
);

--成绩表
create table result(
StudentNo number primary key,
SubjectNo number not null,
ExamDate date,
StudentResult number,
constraint fk_student_result foreign key(StudentNo) references result(StudentNo),
constraint fk_subject_student foreign key(SubjectNo) references result(StudentNo)
);

--为学生表插入三条语句
insert all
into student(studentno, loginpwd, studentname, sex, gradeid, phone, address, borndate, email, identitycard)
values (001,'123','张三','男',1,'110','北京市',to_date('2023-2-27','yyyy-mm-dd'),'123@qq.com','1001')
into student(studentno, loginpwd, studentname, sex, gradeid, phone, address, borndate, email, identitycard)
values (002,'456','李四','女',1,'112','上海市',to_date('2023-2-26','yyyy-mm-dd'),'456@qq.com','1002')
into student(studentno, loginpwd, studentname, sex, gradeid, phone, address, borndate, email, identitycard)
values (003,'789','王五','男',1,'119','深圳市',to_date('2023-2-25','yyyy-mm-dd'),'789@qq.com','1003')
select * from dual;
commit;
select * from student;

--为年级表插入三条语句
insert into grade(gradeid, subjectno, gradename)
select 1,0001,'大一' from dual
union all select 2,0002,'大二' from dual
union all select 3,0003,'大三' from dual;
commit;
select * from grade;

--为教师表插入三条语句
insert all
into teacher(teacherno, loginpwd, teachername, sex, gradeid, phone, address, borndate, email, identitycard)
values(001,'1000','Oracle','男',3,'123','武汉市',to_date('2023-2-1','yyyy-mm-dd'),'1000@163.com','2001')
into teacher(teacherno, loginpwd, teachername, sex, gradeid, phone, address, borndate, email, identitycard)
values(002,'2000','Mysql','女',2,'456','厦门市',to_date('2023-2-2','yyyy-mm-dd'),'2000@163.com','2002')
into teacher(teacherno, loginpwd, teachername, sex, gradeid, phone, address, borndate, email, identitycard)
values(003,'3000','SqlServer','男',3,'789','西安市',to_date('2023-2-3','yyyy-mm-dd'),'3000@163.com','2003')
select * from dual;
commit;
select * from teacher;

--为成绩表插入三条语句
insert into result(studentno, subjectno, examdate, studentresult)
select 001,001,to_date('2023-2-27','yyyy-mm-dd'),100 from dual
union all select 002,002,to_date('2023-2-27','yyyy-mm-dd'),99 from dual
union all select 003,003,to_date('2023-2-27','yyyy-mm-dd'),60 from dual
commit;
select * from result;

--为课程表插入三条语句
insert into subject(subjectno, subjectname, classhour, gradeid)
select 001,'SqlServer',32,1 from dual
union all select 002,'Mysql',45,2 from dual
union all select 003,'Oracle',32,3 from dual
commit;
select * from subject;

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值