This Oracle tutorial explains how to create a schema in Oracle with syntax and examples.
Creating a schema in Oracle, can at first, appear to be a little confusing. You might think that the CREATE SCHEMA statement would create your schema, but that is not the case. The CREATE SCHEMA statement is used only to create objects (ie: tables, views, etc) in your schema in a single SQL statement, but does not actually create the schema itself.
To create a schema in Oracle, you need to do the following steps:
In essence, a schema is created in Oracle when a user is created. (Learn the syntax for the CREATE USER statement).
We can create a new user with the CREATE USER statement as follows:
This CREATE USER statement would create a new user called smithj in the Oracle database whose password is pwd4smithj, the default tablespace would be tbs_perm_01 with a quota of 20MB, and the temporary tablespace would be tbs_temp_01.
If you don't have tablespaces yet, learn how to create default and temporary tablespaces.
The next step in setting up your schema is to assign "system privileges" to the new user smithj.
These "system privileges" will allow our new user to create a session in Oracle as well as create tables, views, triggers, procedures, sequences, and synonyms in the new schema. Here is an example of how we might grant those system privileges:
These new privileges are now granted to the user called smithj.
Now that the schema (called smithj) has been created with the necessary privileges, you can create objects in the schema. This can be done one of 2 ways:
After you have created your objects in the schema, you will need to grant privileges so that other schemas/users can access your database objects (ie: tables).
As a last step, you may want to create synonyms so that other schemas can access the new database objects (ie: tables) without having to prefix the object names with the schema name.
For example, if you were another user named smithj and wanted to select from the suppliers table in new_schema, you would have to run the following SELECT statement (before any synonyms are created):
If you then created a synonym for the suppliers table as follows:
You could run the SELECT statement as follows:
No longer needing to prefix the table name with the schema name.