Oracle 创建数据库、表空间、用户和授权操作

在 Oracle 数据库中,创建数据库、表空间、用户和授权是常见的数据库管理任务。下面,十一张(www.11zhang.com)将逐步介绍如何完成这些操作。

创建数据库

创建数据库移步之前的文章,此处省略!

创建表空间

1、创建表空间和数据文件

例如:创建一个名为 yunying8 的表空间,该表空间将使用一个名为 yunying01.dbf 的数据文件,文件路径为E:\oradata\yunying8\,初始大小为20MB,并且在该表空间中,每个段的分配单位为128KB。

create tablespace yunying8 datafile 'E:\oradata\yunying8\yunying01.dbf' size 20M uniform size 128k;

2、增加表空间数据文件

在表空间 yunying8 中添加 yunying02.dbf 等多个新的数据文件,文件的初始大小都为500MB,并且可以在需要时自动扩展,每次扩展100MB,最大扩展到31744MB

alter tablespace yunying8 add datafile 'E:\oradata\yunying8\yunying02.dbf' size 500m autoextend on next 100m maxsize 31744m;
alter tablespace yunying8 add datafile 'E:\oradata\yunying8\yunying03.dbf' size 500m autoextend on next 100m maxsize 31744m;
alter tablespace yunying8 add datafile 'E:\oradata\yunying8\yunying04.dbf' size 500m autoextend on next 100m maxsize 31744m;
alter tablespace yunying8 add datafile 'E:\oradata\yunying8\yunying05.dbf' size 500m autoextend on next 100m maxsize 31744m;

3、查看表空间路径

select * from dba_data_files order by file_name desc;
select * from dba_tables  where owner = 'YUNYING8';
select * from dba_objects where owner = 'YUNYING8' and object_type='TABLE';
select a.table_name      "表名",
       a.owner           "用户",
       a.tablespace_name "表空间",
       num_rows          "表行数",
       b.bytes           "所占空间大小(MB)"
  from dba_tables a,
       (select segment_name, sum(bytes) / 1024 / 1024 || 'MB' bytes
          from user_segments
         group by segment_name) b
 where a.table_name = b.segment_name
 order by num_rows desc nulls last;

4、删除表空间和数据文件

删除名为 yunying8 的表空间,同时删除该表空间中的所有数据和内容,以及与之关联的所有数据文件。

drop tablespace yunying8 including contents and datafiles

PS:这是一个不可逆的操作,执行后将无法恢复被删除的数据,因此在执行这样的命令之前,通常需要确保备份了重要数据。

创建用户

创建一个名为 yunying8 的新用户,密码为:yunying8,其默认表空间为:yunying8,并且在 users 表空间中的存储配额限制为500MB。

create user yunying8 identified by yunying8 default tablespace yunying8 quota 500m on users;

用户授权

1、给用户授权

grant all privileges to yunying8;          --授予所有可用的数据库权限
grant connect to yunying8;                 --给用户登录权限
grant resource to yunying8;                --给用户登录权限
grant create table to yunying8;            --给用户创建表权限
grant dba to yunying8;                     --授管理员权限
grant select on DBA_TABLES to yunying8;    --授予查看DBA_TABLES视图的权限
grant select any dictionary to yunying8;   --授予所有数据字典视图的查询权限

2、收回dba权限

撤销 yunying8 用户的DBA角色权限

revoke dba from yunying8;

创建directory目录对象

我们可以使用 SQL*Plus 或 PL/SQL Developer 工具来创建 directory 目录对象,用于存储和访问与数据库相关的文件,例如导入或导出数据时使用的数据文件。

1、impdp导入之前,创建一个名为 impdata 的目录对象(同时本地磁盘新建 F:\impdata 文件夹)

create directory impdata as 'F:\impdata'; 

2、expdp导出之前,创建一个名为 expdata 的目录对象(同时本地磁盘新建 F:\expdata 文件夹)

create directory expdata as 'F:\expdata'; 

3、从 Oracle 数据库中删除 directory 目录对象

drop directory impdata;
drop directory expdata;

4、授予用户 yunying8 对目录对象的读取和写入权限(如果用sys/system用户,则不需要授权了)

Grant read,write on directory impdata to yunying8;
Grant read,write on directory expdata to yunying8;
✅来源:十一张
温馨提示:本文最后更新于2025-02-08 11:04:01,某些文章具有时效性,若有错误或已失效,请在文末评论留言
© 版权声明
THE END
如果觉得这篇文章对您有帮助,可以收藏本网址,方便下次访问!
点赞3 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容