In Oracle, how do you change the 'default' database?
linux, oracle, sqlplus
Solution
To expand on kerchingo's answer: Oracle has multiple ways to identify a database.
The best way -- the one that you should always use -- is `USER/PASSWORD@SERVER`. This will use the Oracle naming lookup (tnsnames.ora) to find the actual server, which might be on a different physical host every time you connect to it. You can also specify an Oracle connection string as `SERVER`, but pretend you can't.
There are also two ways to specify a default server via environment variables. The first is `TWO_TASK`, which uses the naming lookup, and the second is `ORACLE_SID`, which assumes that the server is running on the current machine. `ORACLE_SID` takes precedence over `TWO_TASK`.
The reason that you should always use an explicit connect string is that you have no idea whether the user has set `TWO_TASK`, `ORACLE_SID`, both, or neither; nor do you know what they might be set to. Setting both to different values is a particularly painful problem to diagnose, particularly over the phone with a person who doesn't really understand how Oracle works (been there, done that).
Problem
I really should know this, but would someone tell me how to change the default database on Linux? For example: I have a database `test1` on `server1` with `ORACLE_SID=test1`. So, to connect to `test1` I can use: ``` sqlplus myuser/password ``` Connects to the default database, `test1` I would now like the default sqlplus connection to go to database `test2` on server `server2`. So, I've updated tnsnames so that the old `test1` entry now points to `test2@server2`. I've also added a separate entry for `test2` that points to the same place. However, the default connection still seems to go to `test1@server1`. The following both work fine and go to database `test2` on `server2`: ``` sqlplus myuser/password@test1 sqlplus myuser/password@test2 ``` But the default connection, `sqlplus myuser/password`, goes to `test1@server1`. Any ideas? Thanks.