11.2.0.2里新增了一个新特性――用户重命名(Rename User),在这个版本之前要想重命名用户,需要按用户导出,再fromuser touser(imp)或remap_schem(impdp)。还可以通过修改数据字典user$的方式来重命名用户,但这种方法可能会带来风险。有了这个特性之后,可以通过alter user oldname rename to newname identified by "password";一条命令来重命名用户。
下面来看测试过程,使用Oracle版本11.2.0.4
sys@ORCL>select*fromv$version; BANNER ------------------------------------------------------------------------------- OracleDatabase11gEnterpriseEditionRelease11.2.0.4.0-64bitProduction PL/sqlRelease11.2.0.4.0-Production CORE 11.2.0.4.0 Production TNSforLinux:Version11.2.0.4.0-Production NLSRTLVersion11.2.0.4.0-Production
1、创建测试用户及测试表
sys@ORCL>createusersqidentifiedby"sq"; Usercreated. sys@ORCL>grantconnect,resourcetosq; Grantsucceeded sys@ORCL>createtablesq.t1asselect*fromdba_tables; Tablecreated.
2、执行alter user语句
sys@ORCL>alterusersqrenametozlxidentifiedby"zlx"; alterusersqrenametozlxidentifiedby"zlx" * ERRORatline1: ORA-00922:missingorinvalidoption
报错无效的操作,原来是需要修改隐含参数"_enable_rename_user"
3、修改隐含参数"_enable_rename_user",并以restrict模式重启数据库
sys@ORCL>altersystemset"_enable_rename_user"=truescope=spfile; Systemaltered. sys@ORCL>startuprestrictforce; ORACLEinstancestarted. TotalSystemGlobalArea1620115456bytes FixedSize2253704bytes VariableSize956304504bytes DatabaseBuffers654311424bytes RedoBuffers7245824bytes Databasemounted. DatabaSEOpened.
sys@ORCL>alterusersqrenametozlxidentifiedby"zlx"; Useraltered. sys@ORCL>selectcount(*)fromsq.t1; selectcount(*)fromsq.t1 * ERRORatline1: ORA-00942:tableorviewdoesnotexist sys@ORCL>selectcount(*)fromzlx.t1; COUNT(*) ---------- 2864 sys@ORCL>connzlx/zlx ERROR: ORA-01035:ORACLEonlyavailabletouserswithRESTRICTEDSESSIONprivilege Warning:YouarenolongerconnectedtoORACLE. sys@ORCL>conn/assysdba Connected. sys@ORCL>altersystemdisablerestrictedsession; Systemaltered. sys@ORCL>connzlx/zlx Connected. zlx@ORCL>select*fromtab; TNAMetaBTYPECLUSTERID ------------------------------------------------------------------------- T1TABLE
5、reset隐含参数"_enable_rename_user"并重启数据库
sys@ORCL>altersystemreset"_enable_rename_user";
Systemaltered.
sys@ORCL>selectuser#,namefromuser$wherename='ZLX';
USER#NAME
------------------
94 ZLX
sys@ORCL>updateuser$setname='SQ'WHEREUSER#=94;
1rowupdated.
sys@ORCL>commit;
Commitcomplete.
sys@ORCL>connsq/zlx;
Connected.
网上还提到update完后,需要alter system checkpoint;和alter system flush shared_pool;,从上面的操作来看好象更简单一些,但user$作为oracle的核心基表之一,修改数据字典会不会造成系统不稳定产生ora-600错误等等都不好说,所以生产上一定要慎重。
原文链接:https://www.f2er.com/oracle/208598.html