In an Oracle Database, what are the differences between the following:
- user()
- sys_context('USERENV', 'CURRENT_USER')
- sys_context('USERENV', 'SESSION_USER')
Are these also possible related values to whatever 'the current user' is?
- sys_context('USERENV', 'CURRENT_SCHEMA')
- sys_context('USERENV', 'AUTHENTICATED_IDENTITY')
I am specifically interested in which ones can change, what can change them, which ones can not change value, which ones have different values based on connection type, and which one(s) is(are) always the schema used to log into the database.
In most of my testing the values are always the same. The only exception would be when running the following to alter 'CURRENT_SCHEMA':
alter session set current_schema=<SCHEMA>
Doing following results in an error:
alter session set current_user=<USER> --even as sys/system, which is good I suppose
So there is some kind of security/rules around all of this. However there must be some reason behind having a SESSION_USER and a CURRENT_USER. I also suppose user() could be a shortcut to sys_context('USERENV', 'CURRENT_USER'), but I could find no documentation on the matter.
sys_context('USERENV', 'CURRENT_SCHEMA')
- The schema that is currently being used and as you already found out can be changed withalter session
sys_context('USERENV', 'SESSION_USER')
- The user that was used for authentication during the creation of the session and cannot be changedsys_context('USERENV', 'CURRENT_USER')
-Pretty much like "session_user"deprecated (at least according to the 10g documentation)(editted according to @a_horse_with_no_name's answer and the reference he gave to the 11g docs)
sys_context('USERENV', 'AUTHENTICATED_IDENTITY')
- The identity used for authentication, depends on the "AUTHENTICATION_METHOD".from documentation:
user pseudo column
- I'm not sure, according to documentation I'd think it's likeCURRENT_SCHEMA
but apparently it's likeCURRENT_USER
There is also a performance difference between USER and using sys_context
Also see https://svenweller.wordpress.com/2016/02/24/sequence-and-audit-columns-with-apex-5-and-12c/ and http://www.grassroots-oracle.com/2019/01/oracle-user-vs-sys-context.html
CURRENT_SCHEMA
is the schema that will be assumed if you name an object without specifying its owner. For instance, if myCURRENT_SCHEMA
isSCOTT
, thenSELECT * FROM EMP
is the same asSELECT * FROM SCOTT.EMP
. By default, when I first connect to Oracle, theCURRENT_SCHEMA
is the same as CURRENT_USER.However, if I am connected as
SCOTT
, I can issueALTER SESSION SET CURRENT_SCHEMA=JOE
and then when I doSELECT * FROM EMP
, it is interpreted asJOE.EMP
rather thanSCOTT.EMP
. Of course, if I don't have theSELECT
privilege onJOE.EMP
, orJOE
doesn't have an object namedEMP
, theSELECT
will fail.From the manual at: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions184.htm#SQLRF51825
CURRENT_USER
SESSION_USER
So there is a difference between SESSION_USER and CURRENT_USER especially when CURRENT_USER is used in a stored procedure or function.
I have to admit that I don't know what the term "enterprise user" means though.
Btw: there is a third one:
SESSION_USERID