安装数据库
基本信息
版本:OracleXE184_Win64
系统管理员:system/123456
SQL Plus命令
创建表空间、用户
Oracle12c 中,增加了可插接数据库的概念,即PDB,允许一个数据库容器(CDB)承载多个可插拔数据库(PDB)。CDB全称为 ContainerDatabase,中文翻译为数据库容器,PDB全称为PluggableDatabase,即可插拔数据库。在ORACLE 12C之前,实例与数据库是一对一或多对一关系(RAC):即一个实例只能与一个数据库相关联,数据库可以被多个实例所加载。而实例与数据库不可能是一对 多的关系。当进入ORACLE 12C后,实例与数据库可以是一对多的关系。下面是官方文档关于CDB与PDB的关系图。
create temporary tablespace my_temp
tempfile 'D:\database\oracle space\pdmis_temp.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
create tablespace my
datafile 'D:\database\oracle space\my.dbf'
size 200m
autoextend on
next 100m maxsize unlimited;
create user C##my identified by my;
create role c##myroles container = all;
grant dba to c##myroles container = all;
grant c##myroles to c##my container = all;
-- 查询表空间
select * from dba_tablespaces;
-- 查询当前数据库是CDB?yes: CDB, no PDB
select CDB from v$database;
-- 修改成PDB
alter session set container = pdb1;
-- 修改成CDB
alter session set container = cdb$root;
-- 删除表空间
DROP TABLESPACE my INCLUDING CONTENTS AND DATAFILES;
创建表
create table basic_student_info(
student_no varchar2(32),
student_name varchar2(64),
class_no varchar2(32),
major_no varchar2(32),
major_name varchar(32),
brithday varchar(32),
create_at varchar2(32) default TO_CHAR(SYSDATE,'YY/MM/DD HH24:MI:SS'),
primary key(student_no)
);
create index stu_class_major on basic_student_info(class_no, major_no);
declare
c number(8) := 100000;
i number(8) := 1;
BEGIN
<<insert_loop>>
loop
insert into basic_student_info(student_no, student_name, class_no, major_no, major_name, brithday)
values(to_char(i), 'test', to_char(mod(i, 100)), to_char(mod(i,10)), concat('major', mod(i, 10)), '2020-10-01');
i := i+1;
exit insert_loop when i > c;
end loop;
dbms_output.put_line('insert_loop end!');
END;
-- ORA-01950: 对表空间 'USERS' 无权限, 使用system账户登录授权
alter user C##my quota unlimited on users;
优化
联合索引
-- range scan
explain plan for
select * from basic_student_info where class_no = '1';
-- skip scan
-- 在前导列唯一值较少的情况下,才会用到index skip can
-- skip scan没有直接索引查询快,但可以这样说,相比于整个表扫描(table scan),索引跳跃式扫描的速度要快得多
explain plan for
select * from basic_student_info where major_no = '1';
-- range scan
explain plan for
select * from basic_student_info where class_no = '1' and major_no = '1';
-- range scan
-- diffrent from MySQL
explain plan for
select * from basic_student_info where major_no = '1' and class_no = '1';