一. 分别建立Student,Course,SC基本表。
create table Student
(
sno char(5) primary key,
sname varchar(10) unique,
ssex char(2) not null,
sage number(3) default 0,
sdept varchar(10)
);
create table Course
(
cno char(1) primary key,
cname varchar2(15) not null,
cpno char(1),
ccredit number(1)
);
create table SC
(
sno char(5) not null,
cno char(1) not null,
grade number(3),
primary key(sno, cno),
foreign key(sno) references Student(sno),
foreign key(cno) references Course(cno)
);
综上可看出,定义时公式为:create table 表名称
(
数据名称 类型(限制常量) [限制类型],
[限制类型(名称) references 表名称(数据名称) ]
);
二. 部分操作
- 显示基本结构:DESC 基本表名;
DESC Student;
DESC Course;
DESC SC;
2. 显示用户模式内的基本表和视图:select * from cat;
select * from Student;
select * from Course;
select * from SC;
3. 用alter table语句修改基本表结构:
(1)对Student表增加“籍贯”属性列,varchar(20);
alter table Student add native_place varchar(20);
(2)对Student表增加“入学时间”属性列,日期型;
alter table Student add entrance_time date;
(3)对Course和SC表更新涉及“课程号”,“先行课程”属性列长度为2;
alter table Course modify cno char(2);
alter table Course modify cpno char(2);
alter table SC modify cno char(2);
(4)删除学生姓名必须取唯一值得约束;
alter table Student drop unique(sname);
综上,alter table语句基本格式为:alter table 表名 add 新属性名 限制条件;
alter table 表名 modify 属性名 限制条件;
alter table 表名 drop 限制条件(属性名);
4. 用create index语句建立索引
(1)对Student表按姓名升序建唯一索引;
create unique index Stuname on Student(sname asc);
(2)对Course表按课程名称降序建唯一索引;
create unique index Couname on Course(cname desc);
(3)对SC表按成绩降序和学号升序建立索引;
create unique index Scno on SC(grade desc, sno asc);
综上,create index语句基本格式为:
create 限制类型 index 索引名称 on 表名(属性名称 desc, 属性名称 asc);
其中asc可以省略不写,顺序可以颠倒
5. 用drop语句删除基本表及索引;
drop Student;
drop index Stuname;
6. 用insert语句向以上三个基本表插入元组;
insert into Student values('95001','李永','男',20,'cs','北京','27-2月-02');
7. 用select语句显示各表的内容;
select * from Student;
select * from Course;
select * from SC;
select sysdate from dual; -----显示系统时间