Oracle建庫流程例子

來源:互聯網
上載者:User

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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.