oracle學習(建立表)

woo80277038123發表於2010-05-01

我想最初學習資料庫時都是以student表、course表和sc表為例的吧

首先建立這三張表:必要時可重新建立使用者及密碼並登陸

環境:oracle 10g

declare
  vcnt number;
begin
  select count(*) into vcnt from user_tables where table_name='SC';
  If vcnt = 1 Then
    Execute immediate 'drop table sc';
  end if;
  select count(*) into vcnt from user_tables where table_name='STUDENT';
  If vcnt = 1 Then
    Execute immediate 'drop table student';
  end if;
  select count(*) into vcnt from user_tables where table_name='COURSE';
  If vcnt = 1 Then
    Execute immediate 'drop table course';
  end if;
end;
/

--建立表
Create table student
(sno char(10) primary key,
 sname varchar(20) not null,
 sage smallint,
 ssex char(2),
 sdept varchar(20));

Create table course
(cno char(10) primary key,
 cname varchar(20) not null,
 credit smallint);

Create table sc
(sno char(10),
 cno char(10),
 grade smallint,
 primary key(sno,cno));

--向表中插入資料
insert into student values('001','葛靈','19','f','MA');
insert into student values('002','嶽林月','25','f','MA');
insert into student values('003','姬勝俊','16','f','CS');
insert into student values('004','馬源','20','f','MA');
insert into student values('005','翁印','23','m','C');

insert into course values('C01','C語言','4');
insert into course values('C02','數學','4');
insert into course values('C03','英語','2');
insert into course values('C04','計算機','5');
insert into course values('C05','數位電路','3');
insert into course values('C06','資料庫','2');

--001號學生選修課程
insert into sc values('001','C05','75');
insert into sc values('001','C01','68');
insert into sc values('001','C03','86');
insert into sc values('001','C02','40');
insert into sc values('001','C04','97');
--002號學生選修課程
insert into sc values('002','C01','60');
insert into sc values('002','C02','67');
insert into sc values('002','C03','47');
insert into sc values('002','C04','98');
--003號學生選修課程
insert into sc values('003','C05','82');
insert into sc values('003','C01','54');
insert into sc values('003','C02','84');
insert into sc values('003','C03','54');
insert into sc values('003','C04','76');
--004號學生選修課程
insert into sc values('004','C02','87');
insert into sc values('004','C03','69');
insert into sc values('004','C01','40');
--005號學生選修課程
insert into sc values('005','C03','41');
insert into sc values('005','C05','62');
insert into sc values('005','C01','98');

commit;

 

若每次都需要建立這三張表,可以新建txt檔案,如a.txt,將上述內容複製到該檔案中,並將檔案放置在d盤。

在command視窗,執行 @d:/a.txt ; 或者 start d:/a.txt ;

相關文章