什么是数据库对象
数据库对象包括 表 索引 分区 视图 序列 同义词
数据库支持对象
存储过程 自定义函数 触发器 表空间 高级包
表 数据库中的数据结构 存储数据以及描述数据间的关系
表由行和列组成 数据库中数据存储数据的基本单元
分类
普通表 数据全局可见 存储在普通表空间 支持分区和LOB类型
会话级全局临时表 数据会话级可见 存储在temp表空间 不支持分区和LOB类型
事务级全局临时表 事务会话级可见 存储在temp表空间 不支持分区和LOB类型
本地临时表 表结构会话级可见 存储在temp表中 不支持分别和LOB类型 表明必须以#为前缀
Nologging表 数据全局可见 存储在temp2空间 支持分区和lob类型
语法 查询表空间 select * from dv_tablespaces
# create table
在表空间 human_resource中创建表staffs
create table staffs ( staff_id not null primary key auto_increment , first_name varchar(20), last_name varchar(20), graduated_name char(50) ) tablespace human_resource NOLOGGING
穿件会话级|| 事务级 全局临时表
create global temporary table staff_session ( id int, owner char(10) ) On commit preserve || DELETE ROWs
更改表语法 alter table
# 新增列 alter table schema_name.table_name add full_masks int; # 删除列 alter table schema_name.table_name drop source; # 添加约束 alter table schema_name.table_name add constraint ck_training check(staff_id>0); # 删除约束 alter table schema_name.table_name drop constraint ck_training; # 重命名training_New alter table schema_name.table_name rename to training_new; # 删除表 # 直接删除表 dtop table if exists staff; purge;
索引:索引是对表中一列或者多列的值 进行排序的一种结构 可以提高查询性能
普通索引 B-TREE
唯一索引 unique
函数索引 简历在函数基础上的索引
分区索引 在表的分区上创建的索引 在删除时不影响其他分区索引的使用
创建索引 # CLOB BLOB IMAGE对象不能有索引 create index staffs_ind on staffs(staff_id) tablespace human_resource; 更改索引 # 在线重建索引 alter index staff_ind rebuild online; # index重命名 alter inedx staffs_ind rename to staffs_new # 删除索引 drop index if exists staffs_ind staffs_new on table_name;
分区
把逻辑上的一张表根据某种方案分为几张 物理块进行存储 这逻辑的表称为分区表 物理块称之为分区
分区表是一张逻辑表 不存储数据 数据实际存储在分区上
范围分区 range 适用于流水 日志等大表 需要根据时间删除历史日志
间隔分区 interval 相对range更加方便 设置分区间隔 和初始分区键指
列表分区 list 适用于离散值场景
哈希分区 hash 适用于把数据分离开 而不是为了删除数据
# 创建list分区 create table table_name ( user_id int primary key auto_increment, username char(10) ) PARTITION by LIST(user_id) ( partition part1 values values (1), partition part2 values values (5), partition part3 values values (10) ); # 创建分区 RANGE create table table_range ( user_id int primary key auto_increment, username char(10) ) PARTITION by RANGE(user_id) ( partition R_0 values less than (50), partition R_1 values less than (100), partition R_2 values less than (150) ); # 更改分区 添加分区R_MAX alter table table_range add partition R_MAX values less than(maxvalues); # 删除分区 R_1 alter table table_range Drop partition R_1 ; # 分裂分区 将R_0 分成R_0_0 R_0_1 其中00的边界为25 alter table table_range SPLITE PARTITION R_0 at(25) into (partition R_0_0 ,partition R_0_1);
分区索引
分区索引是在分区上独立创建索引 在删除某个分区时 不影响其他分区索引的使用
create index index_edu on education(staff_id ,higest_degree) # 创建分区索引 drop index if exists index_edu on education: 删除分区表索引 alter table education drop partition doctor ; 删除分区 doctor drop table if exists education ; 删除分区表education
视图:
视图从一个或几个表导出的虚拟表 用于数据访问
# 创建视图 create or replace view privilege_view(staff , privilege ,description,appover) as select * from privilege ; # 删除视图 drop view if exists privilige_view ;
序列:
# 创建序列 create sequence seq_auto_extend start with 10 maxvalue 200 increament by 2 cycle ; # 修改序列 修改步长为4 最大值为400 alter sequence seq_auto_extend increment by 4 cycle maxvalue 400 ; # 删除序列 drop sequement if exists seq_auto_extend;