`

Oracle 创建表空间和用户

阅读更多

Oracle安装完后,其中有一个缺省的数据库,除了这个缺省的数据库外,我们还可以创建自己的数据库。

    对于初学者来说,为了避免麻烦,可以用'Database Configuration Assistant'向导来创建数据库。

    创建完数据库后,并不能立即在数据库中建表,必须先创建该数据库的用户,并且为该用户指定表空间。

    下面是创建数据库用户的具体过程:

 

    1.假如现在已经建好名为'news'的数据库,此时在F:\oracle\product\10.1.0\oradata\目录下已经存在news目录(注意:我的Oracle10g安装在F:\oracle下,若你的Oracle安装在别的目录,那么你新建的数据库目录就在*\product\10.1.0\oradata\目录下)。

 

    2.在创建用户之前,先要创建表空间:

    其格式为:格式:  create tablespace 表间名 datafile '数据文件名' size 表空间大小;

    如:

    SQL> create tablespace news_tablespace datafile 'F:\oracle\product\10.1.0\oradata\news\news_data.dbf' size 500M autoextend on next 50M;

    其中'news_tablespace'是你自定义的表空间名称,可以任意取名;'F:\oracle\product\10.1.0\oradata\news\news_data.dbf'是数据文件的存放位置,'news_data.dbf'文件名也是任意取;'size 500M'是指定该数据文件的大小,也就是表空间的大小;'autoextend on next 50M'是指不够的话会自动增长50M;

    实例SQL如下>create tablespace ts_frame_dat datafile 'D:\oracle\product\10.2.0\oradata\orcl\ts_frame_dat.dbf' size 500M autoextend on next 50M;

 

    3.现在建好了名为'news_tablespace'的表空间,下面就可以创建用户了:

    其格式为:格式:  create user  用户名 identified by 密码  default tablespace 表空间表;

    如:

    SQL> create user news identified by news default tablespace news_tablespace;

    默认表空间'default tablespace'使用上面创建的表空间。

 

    4.接着授权给新建的用户:

    SQL> grant connect,resource to news;  --表示把 connect,resource权限授予news用户

    SQL> grant dba to news;  --表示把 dba权限授予给news用户

    授权成功。

 

    ok! 数据库用户创建完成,现在你就可以使用该用户创建数据表了!

 

    5.查询表空间文件所在路径

    SQL> select * from dba_data_files

 

    6.查询表空间使用情况

    SQL> select dbf.tablespace_name,

              dbf.totalspace "总量(M)",

              dbf.totalblocks as 总块数,

              dfs.freespace "剩余总量(M)",

              dfs.freeblocks "剩余块数",

              (dfs.freespace / dbf.totalspace) * 100 "空闲比例" 

              from (select t.tablespace_name,

              sum(t.bytes) / 1024 / 1024 totalspace,

              sum(t.blocks) totalblocks

              from dba_data_files t

              group by t.tablespace_name) dbf,

              (select tt.tablespace_name,

                sum(tt.bytes) / 1024 / 1024 freespace,

                sum(tt.blocks) freeblocks

                from dba_free_space tt

              group by tt.tablespace_name) dfs

              where trim(dbf.tablespace_name) = trim(dfs.tablespace_name)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics