Sub topic 8
Verifying your environment with a few queries
You now have at least one practice database: a FreeSQL session, a 26ai Free container, or an Always Free Autonomous Database. This chapter shows how to confirm the database works before you build on it. You will run a short set of queries that answer four questions: where am I connected, who am I, what can I do, and does data survive a write.
This chapter assumes you can already run SELECT, INSERT and CREATE TABLE, and that you have a development user (or a FreeSQL session) from the previous chapters. Use any SQL client you like: SQL*Plus, SQLcl, the Database Actions SQL worksheet, or the FreeSQL editor. The queries are the same everywhere.
Why verify before you build
A connection that succeeds does not prove you are in the right place. Three mistakes are common:
- You connected to the container database root (
CDB$ROOT) instead of the pluggable databaseFREEPDB1. - You connected as
SYSorADMINwhen you meant to use your development user. - You connected correctly but lack the quota or privileges to create tables.
Each mistake produces confusing errors later, such as a table you cannot find or a quota error halfway through a lesson. A one-minute check now avoids that.
Check 1: where am I and who am I
The quickest check uses the SYS_CONTEXT function with the USERENV namespace. It returns facts about your current session.
SELECT sys_context('USERENV', 'CON_NAME') AS container_name,
sys_context('USERENV', 'SESSION_USER') AS session_user
FROM dual;DUAL is a built-in one-row, one-column table. You select from it when you need a value that does not come from a real table, such as a function result.
Here is what you should see in each environment:
| Environment | `container_name` | `session_user` |
|---|---|---|
| 26ai Free container, as your development user | `FREEPDB1` | `APP_USER` |
| 26ai Free container, as `SYS` in the root | `CDB$ROOT` | `SYS` |
| Autonomous Database, as your development user | A generated database name, not `CDB$ROOT` | Your user name in upper case |
| FreeSQL | A name assigned by the service | The schema FreeSQL gives you |
Unquoted user names are stored in upper case. If you created app_user without double quotes, the session shows APP_USER.
If the container check shows CDB$ROOT, stop. The root holds Oracle's system metadata, and ordinary practice work belongs in the pluggable database. Reconnect with the service name FREEPDB1:
sqlplus app_user/ChangeMe_123@localhost:1521/FREEPDB1The part after the slash is the service name. It decides which container you land in, which is why a wrong service name is the usual cause of landing in the wrong place.
More session facts
A few other USERENV attributes are useful when something looks off:
SELECT sys_context('USERENV', 'CURRENT_SCHEMA') AS current_schema,
sys_context('USERENV', 'SERVICE_NAME') AS service_name,
sys_context('USERENV', 'DB_NAME') AS db_name
FROM dual;CURRENT_SCHEMA is the schema Oracle uses to resolve unqualified table names. It normally equals SESSION_USER. The two differ only if someone ran ALTER SESSION SET CURRENT_SCHEMA, which is rare in practice work.
The shorthand SELECT USER FROM dual; also returns your session user. Use it when you only need that one value.
Check 2: which version am I running
Knowing the version matters because features differ between releases. For example, the GENERATED ALWAYS AS IDENTITY syntax used below needs Oracle Database 12c or later. Any current Free, Autonomous or FreeSQL environment supports it.
SELECT product, version_full, status
FROM product_component_version;PRODUCT_COMPONENT_VERSION is readable by every user, so this query works without special grants. You will see a row for the Oracle Database product with the full release number. A row for NLSRTL (the globalization library) usually appears too. Expect the database row to show a 23-series release for 26ai Free.
You may find advice online to query v$version. That view requires an explicit grant on V_$VERSION. A non-administrative user may get ORA-00942: table or view does not exist, which looks like a broken database but is only a missing privilege.
Check 3: what can I do
Your development user received DB_DEVELOPER_ROLE. Confirm that your session sees it:
SELECT role
FROM session_roles
ORDER BY role;SESSION_ROLES lists the roles enabled for your current session. You should see DB_DEVELOPER_ROLE, plus roles it contains, such as CONNECT and RESOURCE, depending on the release.
Next, check what your account owns and what privileges it holds directly:
SELECT username, default_tablespace, account_status
FROM user_users;
SELECT privilege
FROM user_sys_privs
ORDER BY privilege;USER_USERS describes only your own account. Look at DEFAULT_TABLESPACE. In the container created in an earlier chapter it is normally USERS. In Autonomous Database the default tablespace is named DATA. This explains a difference in the user-creation statement: the quota clause in the container example says ON users, while on Autonomous you would write ON data.
The USER_SYS_PRIVS output may be short or empty, because most privileges arrive through roles rather than direct grants. An empty result does not mean you lack privileges. The role check above is the better signal.
Finally, check for existing objects:
SELECT table_name
FROM user_tables
ORDER BY table_name;On a fresh development user this returns no rows. That is the expected result, not an error.
Check 4: the full write and read cycle
The checks so far only read. The most reliable proof that the environment works is a complete cycle of data definition, data change, commit and query. Run this script:
CREATE TABLE practice_note (
id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
body VARCHAR2(200) NOT NULL
);
INSERT INTO practice_note (body) VALUES ('Hello from my practice database');
COMMIT;
SELECT id, body FROM practice_note;Expected result:
| ID | BODY |
|---|---|
| 1 | Hello from my practice database |
Each statement tests something different:
CREATE TABLEtests that you hold theCREATE TABLEprivilege and have a usable tablespace with quota.- The identity column tests that sequences, which Oracle uses internally for
GENERATED ... AS IDENTITY, work for your user. INSERTtests that you can write data.COMMITmakes the change permanent and visible to other sessions. SQL*Plus and SQLcl do not commit for you, so leaving it out is a classic cause of "my data disappeared after I reconnected".SELECTtests the read path and shows that the identity value was generated automatically.
Prove the commit worked
To see why COMMIT matters, open a second session as the same user, for example a second terminal or a second worksheet, and run the SELECT. If the first session had not committed, the second would show no rows. Doing this once makes the transaction model concrete.
Read your own table's metadata
You can confirm the table exists and look at its columns through the data dictionary:
SELECT column_name, data_type, data_length, nullable
FROM user_tab_columns
WHERE table_name = 'PRACTICE_NOTE'
ORDER BY column_id;Note that the table name in the WHERE clause is upper case. The dictionary stores unquoted names in upper case, and string comparisons are case-sensitive. Writing 'practice_note' returns no rows and often leads people to think the table is missing.
Reading the failures
Each failure points to a specific cause. This table maps the errors you are most likely to see during verification:
| Error | Likely cause | Fix |
|---|---|---|
| `ORA-01017: invalid username/password` | Wrong password, or the password has special characters your shell changed | Quote the password, or let the client prompt for it |
| `ORA-12514` or `ORA-12541` | Wrong service name, or the listener is not ready | Use `FREEPDB1`, and wait for `DATABASE IS READY TO USE!` in the container log |
| `ORA-01950: no privileges on tablespace` | The user has no quota | Run `ALTER USER app_user QUOTA UNLIMITED ON users;` as an administrator |
| `ORA-01031: insufficient privileges` | Missing `CREATE TABLE` or a role is not enabled | Grant `DB_DEVELOPER_ROLE`, then reconnect |
| `ORA-00955: name is already used by an existing object` | You ran the create script twice | Drop the table first, or skip the create |
| `ORA-32795: cannot insert into a generated always identity column` | You supplied your own `id` value | Omit `id` from the insert, as the script does |
| `ORA-00942: table or view does not exist` | Wrong schema, a missing privilege, or a case mismatch | Check `current_schema` and the table name |
A role granted after you connected is not active in your current session. If you see ORA-01031 right after a GRANT, disconnect and reconnect.
Environment-specific notes
The same script runs everywhere, but a few details differ:
- FreeSQL: The session is managed for you, so you usually skip user creation. Some administrative views are restricted. If
session_rolesoruser_sys_privsreturns less than you expect, rely on the write and read cycle as your proof. - Autonomous Database: Use the development user, not
ADMIN. Change the tablespace in the quota clause todatawhen you create the user. Database Actions runs your script in a worksheet, and you can run statements one at a time or as a script. - 26ai Free container: If the container was restarted, repeat check 1. Reconnecting after a restart sometimes lands you in a different service than you intended if you changed connect strings.
Clean up
The practice table has done its job. Remove it so your next lesson starts from a known state:
DROP TABLE practice_note PURGE;Without PURGE, Oracle moves the table to the recycle bin and keeps its space reserved. With PURGE, the table is removed outright. For a throwaway table, PURGE is the tidy choice.
Exercise
You connect to the 26ai Free container and run SELECT sys_context('USERENV', 'CON_NAME') FROM dual;. The result is CDB$ROOT. What went wrong, and how do you fix it?
Show answer
You connected to the container database root, almost certainly by using a service name that points to the root instead of the pluggable database. Reconnect with the service name FREEPDB1, for example sqlplus app_user/ChangeMe_123@localhost:1521/FREEPDB1, and run the check again. It should now return FREEPDB1.
Exercise
You run the verification script, and then the query SELECT table_name FROM user_tables WHERE table_name = 'practice_note'; returns no rows, although SELECT id, body FROM practice_note; works. Why?
Show answer
The data dictionary stores unquoted object names in upper case, and the string comparison is case-sensitive. The correct filter is WHERE table_name = 'PRACTICE_NOTE'. The table exists, and only the lower-case literal fails to match.
Exercise
A colleague runs the INSERT and SELECT in SQLcl, then closes the window. The next day the table is empty. What step did they leave out, and how can you demonstrate its effect with two sessions?
Show answer
They did not run COMMIT. SQLcl does not commit automatically, so the uncommitted insert was rolled back when the session ended. To demonstrate, insert a row in session one without committing, then run a SELECT in session two. Session two shows no row until session one runs COMMIT.