与一般的关系数据库产品相比,使用Oracle建立一张数据表的步骤要相对复杂一些,一般要先做一些准备工作,这些工作包括分配表空间(普通表和临时表)、新建用户(模式)及用户授权等,这些都涉及到Oracle的管理知识。下面介绍一些比较常用的管理命令:
一、表空间管理
1.查看表空间信息
select * from user_tables;
2. 建立普通表空间
create (smallfile|bigfile) tablespace lychee --默认是"smallfile"
datafile 'D:\app\hp\oradata\orcl\lychee.bdf' --数据文件的路径
size --数据文件初始大小为
autoextend on|off --数据文件是否自动增长
next --指定数据文件自动增长时每次增长的大小
maxsize ; --数据文件的最大值,可用unlimited表示无限增长;
3. 建立临时表空间
create (smallfile|bigfile) temporary tablespace temp_lychee
tempfile 'D:\app\hp\oradata\orcl\temp_lychee.bdf'
size
autoextend on
maxsize unlimited;
4.改变表空间的可用性
alter tablespace lychee
offline normal|temporary|immediate|for recovery;
说明:使表空间以normal|temporary|immediate|for recovery方式脱机,脱机即使用户暂时无法访问数据库的某部分。
alter tablespace lychee online;
说明:使表空间联机。
5.改变表空间的读写状态
alter tablespace lychee read only|write;
6.改变表空间名称
alter tablespace lychee rename to new_name;
7.设置数据库的默认表空间(新建用户时默认所使用的表空间)
alter database default tablespace users; --默认普通表空间
alter database default temporary tablespace temp; --默认临时表空间
8.删除表空间
drop tablespace lychee including contents and datafiles;
说明:该语句仅删除控制文件和数据字典中的表空间和数据文件的信息。最后还需要手动删除操作系统上的数据文件。
二、数据文件管理
1.添加表空间的数据文件
alter tablespace lychee
add datafile 'D:\app\hp\oradata\orcl\temp_lychee 02.bdf'
size 10;
alter tablespace lychee
add tempfile 'D:\app\hp\oradata\orcl\lychee02.bdf'
size 10
reuse;
注:如果同名文件已存在,则添加数据文件会失败。可使用reuse子句来强制覆盖同名文件。
2.修改表空间的数据文件
alter database
datafile 'D:\app\hp\oradata\orcl\lychee.bdf'
resize
autoextend on
next 512k
maxsize ;
3.改变数据文件的可用性
alter database
datafile 'D:\app\hp\oradata\orcl\lychee.bdf'
offline|online;
注:数据文件脱机不影响表空间的可用性,但表空间脱机则导致其所有的数据文件脱机。
4.改变数据文件的名称和位置
4.1.当数据文件属于单个表空间时
(1)将表空间设为脱机状态
(2)在操作系统上重新命名或移动数据文件
(3)在数据库内部修改数据文件的名称,即使用alter tablespace语句中的rename file子句,如:
alter database
rename file
'D:\app\hp\oradata\orcl\lychee01.bdf'
'D:\app\hp\oradata\orcl\lychee02.bdf'
to
'E:\app\hp\oradata\orcl\lychee.bdf'
'E:\app\hp\oradata\orcl\lychee.bdf';
(4)将表空间重新设置为联机状态
4.2.当数据文件属于多个表空间时
(1)关闭数据库
(2)在操作系统中,将要改动的数据文件复制到新的位置,或改变它们的名称
(3)启动实例,加载数据库
(4)在alter database语句中使用rename file子句,修改数据库内部数据文件的名称
(5)使用如下语句打开数据库:
alter database open;
三、用户管理
1. 建立用户
create user lychee --用户帐号
identified by lychee --用户密码(以后密码要区分大小写)
default tablespace lychee --默认使用表空间"USERS"
quota on lychee --默认的配额是0
temporary tablespace temp_lychee --默认使用临时表空间"TEMP"
account unlock; --帐号解锁
2.修改用户密码
alter user lychee identified by new_password;
3.修改表空间配额
Alter user lychee quota 0 on lychee; --第一个lychee是帐号,第二个lychee是表空间名
小技巧:要回收某用户创建数据库对象的权限,可将其相应的表空间配额修改为0,这样该用户已经创建的数据库对象依然被保留,但无法再创建新的数据库对象。
4.锁定/解锁用户账户
alter user lychee account lock|unlock;
5.修改用户的默认表空间
alter user lychee default tablespace new_table_space;
6.修改用户的临时表空间
alter user lychee temporary tablespace new_temp_tablespace;
7.删除用户账户
drop user lychee;
注意:Oracle不允许修改用户的账户名,如要更改只能通过删除帐号后再创建的方法间接实现。
8.使用PROFILE管理密码
create profile password_profile limit
failed_login_attempts 3
password_lock_time 5
password_life_time 10
password_grace_time 2;
alter user lychee profile password_profile;
说明:新建一个配置文件password_profile,配置用户如果登录失败超过3次,则将帐号锁定5天。用户密码有效期为10天,宽限期为2天。该配置文件给用户lychee使用。
alter profile password_profile limit
failed_login_attempts 5;
说明:修改配置文件password_profile,设定用户如果登录失败超过3次,则将帐号锁定。
drop profile password_profile;
说明:删除配置文件password_profile。
select profile from DBA_USERS where username=’lychee’;
说明:从数据字典视图DBA_USERS中查询用户lychee所使用的配置文件。
四、用户授权
grant
create session,
create table,
create sequence,
create view,
create trigger,
create procedure,
create cluster,
create database link
to lychee;
五、建表准备工作的小总结
最后总结一下在Oracle中建立一个数据表的一般过程(使用全局数据库orcl):
--以管理员身份登录
disconnect;
--建立普通表空间
create tablespace lychee
datafile ' D:\app\hp\oradata\orcl\lychee.bdf '
size
autoextend on
maxsize unlimited;
--建立临时表空间
create temporary tablespace temp_lychee
tempfile ' D:\app\hp\oradata\orcl\temp_lychee.bdf '
size
autoextend on
maxsize unlimited;
--新建用户(模式)
create user lychee
identified by lychee
default tablespace lychee
quota on lychee
temporary tablespace temp_lychee
account unlock;
--用户授权
grant
create session,
create table,
create sequence,
create view,
create trigger,
create procedure,
to lychee;
--以lychee帐号登录
disconnect;
connect lychee/lychee;
--建立数据表
create table t_lychee(
……
);