1. 程式人生 > >Oracle資料庫基礎--建表語法+操作

Oracle資料庫基礎--建表語法+操作

複製程式碼
 1 --student表
 2 create table student(
 3   stu_id varchar2(10) primary key,
 4   stu_name varchar2(10) not null,
 5   stu_sex varchar2(2) not null,
 6   stu_birthday date,
 7   class_id number
 8 );
 9 --新增表註釋
10 comment on table student is '學生資訊表';
11 --欄位添加註釋
12 comment on column student.stu_id is '
學號(主鍵)'; 13 comment on column student.stu_name is '學生姓名'; 14 comment on column student.stu_sex is '學生性別'; 15 comment on column student.stu_birthday is '學生出生年月'; 16 comment on column student.class_id is '學生所在班級'; 17 18 --sclass表 19 create table sclass( 20   class_id number primary key, 21   class_name varchar2
(10) not null 22 ); 23 comment on table sclass is '班級資訊表'; 24 comment on column sclass.class_id is '班級編號'; 25 comment on column sclass.class_name is '班級名稱'; 26 27 --新增外來鍵 28 alter table student add constraint fk_class_id foreign key(class_id) references sclass(class_id); 29 30 --新增資料 31 insert into
sclass(class_id, class_name)values(1,'計應1401'); 32 insert into sclass(class_id, class_name)values(2,'計網1401'); 33 insert into sclass(class_id, class_name)values(3,'軟體1401'); 34 insert into student(stu_id, stu_name, stu_sex, stu_birthday, class_id)values('A001','張珊','',to_date('1995-10-02','yyyy-mm-dd'),1) ; 35 insert into student(stu_id, stu_name, stu_sex, stu_birthday, class_id)values('A002','李思','',to_date('1995-10-02','yyyy-mm-dd'),1) ; 36 insert into student(stu_id, stu_name, stu_sex, stu_birthday, class_id)values('A003','王武','',to_date('1996-10-02','yyyy-mm-dd'),2) ; 37 insert into student(stu_id, stu_name, stu_sex, stu_birthday, class_id)values('A004','趙柳','',to_date('1996-12-02','yyyy-mm-dd'),3) ; 38 insert into student(stu_id, stu_name, stu_sex, stu_birthday, class_id)values('A005','趙柳','',sysdate,3) ;