sqlplus system/system@orcl --连接
sql>ed a --创建sql文本
sql>get a --把a.sql载入缓存
sql>/
create temporary tablespace sa_temp --临时表空间
tempfile 'e:\dbf\sa_temp.dbf'
size 10m
autoextend on;
create tablespace sa_space --表空间
logging
datafile 'e:\dbf\sa_space.dbf'
size 20m --20m
autoextend on; --自动增长
create user sa identified by sa --创建用户 使用对应的表空间
default tablespace sa_space
temporary tablespace sa_temp;
grant connect,resource,dba to sa; --授予连接 、dba权限给用户
conn sa/sa --角色sa
--创建学员信息表
create table studentinfo(
stuid number primary key not null,
tel nvarchar2(15),
sex char(2) not null,
schooltime date not null,
email nvarchar2(50) not null,
remark nvarchar2(500) not null
);
--创建课程表
create table course(
courseid number primary key not null,
coursecode nvarchar2(15), --课程代码
coursename nvarchar2(50)
);
--创建学员与课程关系表(多对多)
create table stdent_course(
courseid number not null,
stuid number not null
);
--创建序列
create sequence seq_studentinfo_stuid --学员序列
increment by 1 -- 每次加1
start with 1 -- 从1开始计数
nomaxvalue -- 不设置最大值
nocycle -- 一直累加,,不循环
nocache -- 不建缓冲区
create sequence seq_course_courseid --课程序列
increment by 1 -- 每次加1
start with 1 -- 从1开始计数
nomaxvalue -- 不设置最大值
nocycle -- 一直累加,不循环
nocache -- 不建缓冲区
--创建触发器
create or replace trigger tri_studentinfo_stuid --学员主键自增
before
insert on studentinfo for each row
begin
select seq_studentinfo_stuid.nextval into :new.stuid from dual;
end;
create or replace trigger tri_course_courseid --课程主键自增
before
insert on course for each row
begin
select seq_course_courseid.nextval into :new.courseid from dual;
end;
--建立课程表主外建关系
alter table stdent_course add constraint fk_stdentcourse_courseid
foreign key(courseid) references course(courseid);
--建立学员主外建关系
alter table stdent_course add constraint fk_stdentcourse_courseid
foreign key(stuid) references studentid(stuid);
--sql测试
insert into studentinfo(tel,sex,schooltime,email,remark)
values('123456','男',to_date('2011-01-12','yyyy-mm-dd'),'ss@ww.com','爱是刚');
insert into studentinfo(tel,sex,schooltime,email,remark)
values('111111','男',to_date('2011-02-12','yyyy-mm-dd'),'ss1@ww.com','爱是刚111');
insert into course(coursecode,coursename)values('001','语文');
insert into course(coursecode,coursename)values('002','数学');
insert into stdent_course (stuid,courseid)values(1,1);
insert into stdent_course (stuid,courseid)values(1,2);
insert into stdent_course (stuid,courseid)values(2,1);
select * from studentinfo;
select * from course;
select * from stdent_course;
