- 按下列表结构利用 SQL 语句创建 class,student 两个表。
Class
列名 | 数据类型 | 约束 | 备注 |
---|---|---|---|
CNO | NUMBER(2) | 主键 | 班号 |
CNAME | VARCHAR(20) | 班名 | |
NUM | NUMBER(3) | 人数 |
Student
列名 | 数据类型 | 约束 | 备注 |
---|---|---|---|
SNO | NUMBER(4) | 主键 | 学号 |
SNAME | VARCHAR(10) | 唯一 | 姓名 |
SAGE | NUMBER | 年龄 | |
SEX | CHAR(2) | 性别 | |
CNO | NUMBER(2) | 班级号 |
create table class(
cno number(2) primary key,
cname varchar(20),
num number(3)
)
create table student(
sno number(4) primary key,
sname varchar(10),
sage number,
sex char(2),
cno number(2),
unique(sname)
)
- 为 student 表添加一个可以延迟的外键约束,其 CNO 列参照 class 表的 CNO 列。
alter table student
add constraint P_FK foreign key(cno) references class(cno) on delete cascade deferrable
- 为 student 表添加一个可以延迟的外键约束,保证该列表取值在 0~100 之间。
alter table student
add check (sage between 0 and 100)
- 为 student 表的 SAGE 列添加一个检查约束,保证该取值为 “M ”或“F”,且默认值为 “M ”。
alter table student modify(sex char(2) default('m'));
alter table student add check(sex in(‘m’,’f’));
- 利用子查询分别创建一个事务级的临时表和会话级的临时表,其结构与student表结构相同
create global temporary table tran_temp_student on commit delete rows as select * from student;
create global temporary table sess_temp_student on commit preserve rows as select * from student;
- 创建一个student_range表(列、类型与student表的列、类型相同),按学生的年龄分为三个区,低于20岁的学生信息放入part1区,存储在EXAMPLE表空间中;20-30岁的放在part2区,存放在ORCLTBS1表空间中;其他数据放在part3区,存放在ORCLTBS2表空间中
create tablespace ORCLTBS1 datafile 'D:\app\Snakewood\oradata\testdba\ORCLTBS1' size 20m;
create tablespace ORCLTBS2 datafile 'D:\app\Snakewood\oradata\testdba\ORCLTBS2' size 20m;
create tablespace EXAMPLE datafile 'D:\app\Snakewood\oradata\testdba\EXAMPLE' size 20m;
create table student_range partition by range(sage)(partition part1 values less than (20) tablespace example,partition part2 values less than(30) tablespace ORCLTBS1,partition part3 values less than(maxvalue) tablespace ORCLTBS2)as select * from student;
- 创建一个student_list表(列、类型与student表的列、类型相同),按学生性别分为两个区
create table student_list
partition by list(sex)
(
partition student_male values('M') tablespace ORCLTBS1,
partition student_female values('F') tablespace ORCLTBS2
) as
select * from student;
- 将一个保存学生信息的.xls文件转换为.txt文件,根据.txt文件中数据的结构创建一个外部表,实现对.txt的读取操作