The CREATE SCHEMA statement defines a schema. It is also possible to create some objects and grant privileges on objects within the statement.
Invocation
This statement can be embedded in an application program or issued through the use of dynamic SQL statements. It is an executable statement that can be dynamically prepared. However, if the bind option DYNAMICRULES BIND applies, the statement cannot be dynamically prepared (SQLSTATE 42509).
Authorization
An authorization ID that holds SYSADM or DBADM authority can create a schema with any valid schema-name or authorization-name.
An authorization ID that does not hold SYSADM or DBADM authority can only create a schema with a schema-name or authorization-name that matches the authorization ID of the statement.
If the statement includes any schema-SQL-statements the privileges held by the authorization-name (if not specified, it defaults to the authorization ID of the statement) must include at least one of the following:
Syntax
>>-CREATE SCHEMA------------------------------------------------>
>-----+-schema-name-------------------------------------+------->
+-AUTHORIZATION--authorization-name---------------+
'-schema-name--AUTHORIZATION--authorization-name--'
>-----+------------------------------+-------------------------><
| .------------------------. |
| V | |
'----schema-SQL-statement---+--'
Description
Notes
Examples
Example 1: As a user with DBADM authority, create a schema called RICK with the user RICK as the owner.
CREATE SCHEMA RICK AUTHORIZATION RICK
Example 2: Create a schema that has an inventory part table and an index over the part number. Give authority on the table to user JONES.
CREATE SCHEMA INVENTRY
CREATE TABLE PART (PARTNO SMALLINT NOT NULL,
DESCR VARCHAR(24),
QUANTITY INTEGER)
CREATE INDEX PARTIND ON PART (PARTNO)
GRANT ALL ON PART TO JONES
Example 3: Create a schema called PERS with two tables that each have a foreign key that references the other table. This is an example of a feature of the CREATE SCHEMA statement that allows such a pair of tables to be created without the use of the ALTER TABLE statement.
CREATE SCHEMA PERS
CREATE TABLE ORG (DEPTNUMB SMALLINT NOT NULL,
DEPTNAME VARCHAR(14),
MANAGER SMALLINT,
DIVISION VARCHAR(10),
LOCATION VARCHAR(13),
CONSTRAINT PKEYDNO
PRIMARY KEY (DEPTNUMB),
CONSTRAINT FKEYMGR
FOREIGN KEY (MANAGER)
REFERENCES STAFF (ID) )
CREATE TABLE STAFF (ID SMALLINT NOT NULL,
NAME VARCHAR(9),
DEPT SMALLINT,
JOB VARCHAR(5),
YEARS SMALLINT,
SALARY DECIMAL(7,2),
COMM DECIMAL(7,2),
CONSTRAINT PKEYID
PRIMARY KEY (ID),
CONSTRAINT FKEYDNO
FOREIGN KEY (DEPT)
REFERENCES ORG (DEPTNUMB) )