0
点赞
收藏
分享

微信扫一扫

14.Oracle表管理和维护总结

小黑Neo 2022-01-07 阅读 30

文章目录

前言

一、数据的存储类型

1.标量数据类型

  varchar2(size)、nvarchar2(size):变长字符型数据。
  char(size)、nchar(size):定长字符型数据。
  date:日期型数据。
  number(p,s):数字型数据。

2.集合数据类型

嵌套表和VARRY数据类型。

3.关系数据类型

  REF指向一个对象,REF类型的对象就是cursor。

4.行ID(rowid)

(1).rowid是数据库中每一行的唯一标识符。
(2).rowid作为列值是隐式存储的。
(3).rowid不直接给出行的物理地址,但是可以用rowid来定位行。
(4).rowid提供了最快速地访问表中行的方法。

查询rowid
SQL> select rowid from emp t where empno = '7369';
 
ROWID
------------------
AAAVREAAEAAAACXAAA

AAAVRE:为数据对象号,在数据库中每个对象都是唯一的。
AAE:为相对文件号,它和表空间中的一个数据文件对应。
AAAACX:为块号,块号为相对文件中存放的块的位置。
AAA:为行号,行号标识块头中行目录的位置。

二、创建表

1.创建表的规则

1.命名尽量简单,表名要具有一定的意义。
2.每个表都有一个表空间,这样易于管理和维护,对于一个表空间的维护不影响其他的表,并且该表空间是本地管理的。
3.使用标准EXTENT尺寸减少表空间碎片。
4.oracle数据库允许表名的最大长度为30个字符。

2.创建普通的表

create table emp_temp(
empid number(4),
empname varchar2(30),
empsex char,
department varchar2(30))
tablespace users;

select owner, table_name, tablespace_name
  from dba_tables
 where owner = 'SCOTT'
   and table_name = 'EMP_TEMP';

在这里插入图片描述

3.将表创建在一个本地的管理表空间里

创建一个本地的管理表空间
create tablespace lhc_local01
datafile '/oradata01/mlbdb1/lhc_local01.dbf' size 50m
extent management local
uniform size 1m;

创建表
create table test_emp(
empid number(4),
empname varchar2(30),
empsex char,
department varchar2(30))
storage (initial 100k next 100k pctincrease 0 minextents 1 maxextents 8)
tablespace lhc_local01;

验证是否成功建立表test_emp
select owner, table_name, tablespace_name, initial_extent, next_extent
  from dba_tables
 where owner = 'SCOTT'
   and table_name = 'TEST_EMP';

在这里插入图片描述

4.创建临时表

临时表是非常特殊的表,该表只对当前用户的当前会话有效。
使用create global temporary指令来创建临时表
create global temporary table scott.emp_test on commit preserve rows
as select * from emp;

select * from scott.emp_test;

在这里插入图片描述

验证是否成功创建该临时表
select owner,table_name,tablespace_name from dba_tables
where table_name = 'EMP_TEST';

在这里插入图片描述

临时表是存储在临时段中,临时段是一个磁盘区。当用户使用SQL语句执行查询时,如果需要对返回的数据进行排序,
oracle首先需要在内存中完成排序工作,如果内存容量不够,就需要把计算的中间结果放在临时段中。
查询emp_test是否是临时表。
select table_name, tablespace_name, temporary
  from dba_tables
 where owner = 'SCOTT'
   and table_name = 'EMP_TEST';

在这里插入图片描述

删除临时表
drop table scott.emp_test;

5.创建索引组织表

索引组织表是将数据和索引存储在一起的,按照索引的结构来组织和存储表中的数据。
索引组织表对于经常使用主键字段来实现查询的事务非常高效,索引组织表的主键约束不能被删除、延期、禁止。
节约磁盘空间的占用,大幅度降低了I/O。

IOT适用的场合有:
1.完全由主键组成的表。
2.如果你只会通过一个主键来访问一个表,这个表就非常适合实现为IOT。
3.数据以某种特定的顺序物理存储。

创建一张索引组织表:
create table tiot
(
   x int primary key,
   y number,
   z varchar2(20)
)
organization index;

查询表的定义:
查看dbms_metadat定义,发现get_ddl返回值为clob类型,原来如此!可以通过下面命令进行设置:
set long 9999
该环境变量用于设置long和lob类型列的显示长度。

SQL> set long 500    
SQL> select dbms_metadata.get_ddl('TABLE','TIOT') FROM DUAL;

DBMS_METADATA.GET_DDL('TABLE','TIOT')
--------------------------------------------------------------------------------

  CREATE TABLE "SCOTT"."TIOT"
   (	"X" NUMBER(*,0),
	"Y" NUMBER,
	"Z" VARCHAR2(20),
	 PRIMARY KEY ("X") ENABLE
   ) ORGANIZATION INDEX NOCOMPRESS PCTFREE 10 INITRANS 2 MAXTRANS 255 LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"

DBMS_METADATA.GET_DDL('TABLE','TIOT')
--------------------------------------------------------------------------------
 PCTTHRESHOLD 50

在创建一张索引组织表:
create table iot_test
(
  x int primary key,
  y number,
  z varchar2(20)
)
organization index
pctthreshold 10
including y
overflow tablespace users;

注解:从第二列(包含第二列)开始的所有列数据都存放到溢出段中,
而对于第一列的数据只要不违反pctthreshold的值就存储在索引块上,
而一旦违反了这个规则就存储到溢出段中。

6.查询表的参数设置

select table_name,
       tablespace_name,
       pct_free,
       pct_used,
       ini_trans,max_trans
  from dba_tables
 where owner = 'SCOTT'
   and table_name = 'TEST_EMP';

在这里插入图片描述

动态修改表的参数pctfree和pctused
alter table test_emp pctused 50 pctfree 30;

查询是否修改成功

在这里插入图片描述

7.维护列

(1).增加列
alter table scott.test_emp add (sex char);

(2).修改列
alter table scott.test_emp modify (degree varchar2(10) not null);

(3).删除列
alter table scott.test_emp drop column degree cascade contraints

(4).设置某列值无效
alter table scott.test_emp set unused column ephone;

然后在删除
alter table scott.test_emp drop unused columns;


(4).更改列的名字
alter table scott.emp rename column sal to salary;

8.删除和截断表

truncate table scott.test_emp

truncate的特性:
1.删除表的数据但都保留了表结构
2.数据一旦删除就释放了数据占用的磁盘空间,并且数据不可恢复。
3.如果表被外键引用,则无法使用truncate删除表中的数据。
4.所有和表相关的索引也被截断。
5.不会触发删除表的删除触发器。

9.drop表

drop table scott.emp_temp;
drop table scott.emp_temp cascade constraints;
举报

相关推荐

0 条评论