SQL Reference

CREATE SCHEMA

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

schema-name
Names the schema. The name must not identify a schema already described in the catalog (SQLSTATE 42710). The name cannot begin with "SYS" (SQLSTATE 42939). The owner of the schema is the authorization ID that issued the statement.

AUTHORIZATION authorization-name
Identifies the user that is the owner of the schema. The value authorization-name is also used to name the schema. The authorization-name must not identify a schema already described in the catalog (SQLSTATE 42710).

schema-name AUTHORIZATION authorization-name
Identifies a schema called schema-name with the user called authorization-name as the schema owner. The schema-name must not identify a schema-name for a schema already described in the catalog (SQLSTATE 42710). The schema-name cannot begin with "SYS" (SQLSTATE 42939).

schema-SQL-statement
SQL statements that can be included as part of the CREATE SCHEMA statement are:

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) )
 


[ Top of Page | Previous Page | Next Page ]