The CREATE SCHEMA statement defines a schema at the current server and optionally creates tables, views, aliases, indexes, and distinct types. Comments and labels may be added in the catalog description of tables, views, aliases, indexes, columns, and distinct types. Table, view, and distinct type privileges can be granted to users.
This statement can be embedded in an application program or issued interactively. It is an executable statement that can be dynamically prepared.
The privileges held by the authorization ID of the statement must include at least one of the following:
The privileges held by the authorization ID of the statement must include at least one of the following:
If the AUTHORIZATION clause is specified, the privileges held by the authorization ID of the statement must also include at least one of the following:
>>-CREATE SCHEMA--+-schema-name-----------------------+---------> '-AUTHORIZATION--authorization-name-' >--+----------------------+-------------------------------------> '-IN ASP--+-integer--+-' '-ASP-name-' >--+----------------------------------------------------+------>< | .------------------------------------------------. | | V | | '---+-COMMENT statement--------------------------+-+-' +-CREATE ALIAS statement---------------------+ +-CREATE DISTINCT TYPE statement-------------+ +-CREATE INDEX statement---------------------+ +-CREATE SEQUENCE statement------------------+ +-CREATE TABLE statement---------------------+ +-CREATE VIEW statement----------------------+ +-GRANT (Distinct Type Privileges) statement-+ +-GRANT (Sequence Privileges) statement------+ +-GRANT (Table Privileges) statement---------+ | (1) | '-LABEL statement----------------------------'
Schema attributes: A schema is created as:
An index created over a distributed table is created on all of the servers across which the table is distributed. For more information about distributed tables, see the DB2® Multisystem book.
Object ownership: The owner of the schema and created objects is determined as follows:
Object authority: If SQL names are used, the schema and any other objects are created with the system authority of *EXCLUDE on *PUBLIC and the library is created with the create authority parameter CRTAUT(*EXCLUDE). The owner is the only user having any authority to the schema. If other users require authority to the schema, the owner can grant authority to the objects created; using the CL command Grant Object Authority (GRTOBJAUT).
If system names are used, the schema and any other objects are created with the system authority given to *PUBLIC is determined by the system value QCRTAUT, and the library is created with CRTAUT(*SYSVAL). For more information about system security, see the books iSeries Security Reference , and the SQL Programming book.
If the owner of the schema is a member of a group profile (GRPPRF keyword) and group authority is specified (GRPAUT keyword), that group profile will also have authority to the schema.
Object names: If a CREATE TABLE, CREATE INDEX, CREATE ALIAS, CREATE DISTINCT TYPE, CREATE SEQUENCE, or CREATE VIEW statement contains a qualified name for the table, index, alias, distinct type, sequence, or view being created, the schema name specified in that qualified name must be the same as the name of the schema being created. Any other object names referenced within the schema definition may be qualified by any schema name. Unqualified table, index, alias, distinct type, sequence, or view names in any SQL statement are implicitly qualified with the name of the created schema.
Delimiters are not used between the SQL statements.
SQL statement length: If the CREATE SCHEMA statement is executed via the RUNSQLSTM command, the maximum length of any individual CREATE TABLE, CREATE INDEX, CREATE DISTINCT TYPE, CREATE ALIAS, CREATE SEQUENCE, CREATE VIEW, COMMENT, LABEL, or GRANT statements within the CREATE SCHEMA statement is 2 097 152. Otherwise, the entire CREATE SCHEMA statement is limited to 2 097 152.
Syntax alternatives: The COLLECTION keyword can be used as a synonym for SCHEMA for compatibility to prior releases. This keyword is non-standard and should not be used.
Deprecated features: The WITH DATA DICTIONARY clause causes an IDDU data dictionary to be created in the schema. While the clause can still be specified at the end of the CREATE SCHEMA statement and is still supported; it is not recommended.
A schema created with a data dictionary cannot contain tables with LOB or DATALINK columns. The clause has no effect on the creation of catalog views.
Example 1: Create a schema that has an inventory part table and an index over the part number. Give authority to the schema to the user profile JONES.
CREATE SCHEMA INVENTORY CREATE TABLE PART (PARTNO SMALLINT NOT NULL, DESCR VARCHAR(24), QUANTITY INT) CREATE INDEX PARTIND ON PART (PARTNO) GRANT ALL ON PART TO JONES
Example 2: Create a schema using the authorization ID of SMITH. Create a student table that has a comment on the student number column.
CREATE SCHEMA AUTHORIZATION SMITH CREATE TABLE SMITH.STUDENT (STUDNBR SMALLINT NOT NULL UNIQUE, LASTNAME CHAR(20), FIRSTNAME CHAR(20), ADDRESS CHAR(50)) COMMENT ON STUDENT (STUDNBR IS 'THIS IS A UNIQUE ID#')