创建学生表
create table student
(s_id varchar2(10) primary key, --学生学号
s_name varchar2(10) not null, -- 学生姓名
s_birth date, -- 出生年月
s_sex char(2) check(s_sex='男' or s_sex='女')) --学生性别;
插入记录
insert into student(s_id,s_name,s_birth,s_sex)
select '001','张三',to_date('1985-01-01','YYYY-MM-DD'),'男' from dual
union
select '002','李四',to_date('1985-01-30','YYYY-MM-DD'),'男' from dual;
使用序列插入记录
create sequence stu_sequence
start with 003
increment by 1;
insert into student(s_id,s_name,s_birth,s_sex)
values(stu_sequence.nextval,'李美','','');
创建课程表
create table course
(c_id varchar2(10),
c_name varchar2(10)) -- 课程名
;
alter table course
add constraint pk_course primary key(c_id);--添加主键
创建选修表
create table xiu
(xiu_s_id varchar2(10), --选修的学生号
xiu_c_id varchar2(10))--选修的课程号;
添加外键
alter table xiu
add constraint
fk_student_xiu
foreign key(xiu_s_id) references student(s_id);
alter table xiu
add constraint fk_course_xiu
foreign key(xiu_c_id) references course(c_id);
删除外键方法一:
alter table student
drop primary key cascade;
删除外键方法二:
drop table course cascade constraints;