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:
Step 1 - Create a new user in Oracle
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:
CREATE USER smithj
IDENTIFIED BY pwd4smithj
DEFAULT TABLESPACE tbs_perm_01
TEMPORARY TABLESPACE tbs_temp_01
QUOTA 20M on tbs_perm_01;
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.
Step 2 - Assign privileges to new user in Oracle
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:
GRANT create session TO smithj;
GRANT create table TO smithj;
GRANT create view TO smithj;
GRANT create any trigger TO smithj;
GRANT create any procedure TO smithj;
GRANT create sequence TO smithj;
GRANT create synonym TO smithj;
These new privileges are now granted to the user called smithj.
Step 3 - Create objects in the schema
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:
- Executing individual SQL statements to create each object. This would be done through CREATE TABLE statements and CREATE VIEW statements.
- Executing a CREATE SCHEMA statement to create multiple objects in a single SQL statement.