5.3. SCHEMA
Introduced in Firebird 6.0, schemas (schemata) allow you to group related database objects like tables, functions, procedures, etc. into a namespace (the schema). Within a schema, object names must be unique. Between schemas, object can have duplicate names.
This grouping has several purposes
logical organisation (keeping related things together, separate unrelated things)
avoid naming conflicts for applications sharing a database (by putting the application-specific objects in separate schemas)
data-isolation. For example, in a multi-tenant database, each schema can have the same objects, but differing data.
The following object types are always contained in a schema (or, schema-bound):
Tables
Views
Triggers
Procedures
Exceptions
Domains
Indexes
Character sets
Sequences / Generators
Functions
Collations
Packages
The following object types are not contained in a schema (or, schemaless):
Users
Roles
Blob filters
Schemas
Mappings and global mappings
Initially, a Firebird database has two schemas:
|
|
The schema for system objects (the The |
|
|
A schema for user objects. The
|
When describing the syntax in various chapters and sections of this Language Reference, we sometimes take shortcuts.
For example, character sets have a schema, but that schema is always SYSTEM, so in some syntax production we will use [SYSTEM <period>] charset instead of the formally more correct [cs-schema <period>] charset and declaring what cs-schema is.
5.3.1. CREATE SCHEMA
Creates a schema.
Available inDSQL
Syntax
CREATE SCHEMA [IF NOT EXISTS] schema-name
[DEFAULT CHARACTER SET [SYSTEM <period>] default_charset]
[DEFAULT SQL SECURITY {DEFINER | INVOKER}]
CREATE SCHEMA Statement Parameters| Parameter | Description |
|---|---|
schema-name | Schema name. The maximum length is 63 characters. |
default_charset | Specifies the default character set for character data types created inside this schema.
If not specified, it inherits the default database character set.
The character set can optionally be qualified with a schema;
only |
The CREATE SCHEMA statement creates a new schema.
When IF NOT EXISTS is specified, the statement will not raise an error if the schema already exists.
Initially, only the schema owner receives the USAGE permission.
For other users or roles, it must be explicitly granted.
5.3.1.1. Optional Clauses for CREATE SCHEMA
IF NOT EXISTSIf the schema already exists, no changes will be performed.
DEFAULT CHARACTER SETThe default character set for character data types. If not specified, the default character set of the database is inherited. The default will be used for objects created in the schema except where an alternative character set is used explicitly for a field, domain, variable, cast expression, etc.
The character set name can optionally be qualified with the
SYSTEMschema1.SET DEFAULT SQL SECURITYSpecifies the default
SQL SECURITYoption to apply at runtime for objects in the schema without the SQL Security property set. If not specified, theSQL SECURITYoption of the database is inherited. See also SQL Security in chapter Security.
5.3.1.2. Who Can Create a Schema
The CREATE SCHEMA statement can be executed by:
Users with the
CREATE SCHEMAprivilege
See alsoSection 5.3.2, “ALTER SCHEMA”, Section 5.3.3, “DROP SCHEMA”, Section 5.3.4, “CREATE OR ALTER SCHEMA”, Section 5.3.5, “RECREATE SCHEMA”
5.3.2. ALTER SCHEMA
Alters a schema.
Available inDSQL
Syntax
ALTER SCHEMA schema-name
<alter_schema_option>...
<alter_schema_option> ::=
SET DEFAULT CHARACTER SET [SYSTEM <period>] default-charset
| SET DEFAULT SQL SECURITY {DEFINER | INVOKER}
| DROP DEFAULT CHARACTER SET
| DROP DEFAULT SQL SECURITY
ALTER SCHEMA Statement Parameters| Parameter | Description |
|---|---|
schema-name | Schema name |
default-charset | The default character set for character data types created inside this schema.
The character set can optionally be qualified with a schema;
only |
The ALTER SCHEMA statement changes the configuration of a schema.
5.3.2.1. Optional Clauses of ALTER SCHEMA
An ALTER SCHEMA statement needs one or more of these clauses:
SET DEFAULT CHARACTER SETAlters the default character set of the schema.
The character set name can optionally be qualified with the
SYSTEMschema1.DROP DEFAULT CHARACTER SETDrops the default character set of the schema, inheriting the default character set of the database.
SET DEFAULT SQL SECURITYAlters the default
SQL SECURITYoption. See also SQL Security in chapter Security.DROP DEFAULT SQL SECURITYDrops the default
SQL SECURITYoption of the schema, inheriting the defaultSQL SECURITYoption of the database.
5.3.2.2. Who Can Alter a Schema
The ALTER SCHEMA statement can be executed by:
The schema owner
Users with the
ALTER ANY SCHEMAprivilege
See alsoSection 5.3.1, “CREATE SCHEMA”, Section 5.3.4, “CREATE OR ALTER SCHEMA”
5.3.3. DROP SCHEMA
Drops a schema.
Available inDSQL
Syntax
DROP SCHEMA [IF EXISTS] schema-name
DROP SCHEMA Statement Parameters| Parameter | Description |
|---|---|
schema-name | Schema name |
Drops the schema.
When IF EXISTS is specified, the statement will not raise an error if the schema does not exist.
Currently, only empty schemas can be dropped.
In the future, we expect a CASCADE sub-clause to be introduced, allowing schemas to be dropped along with all their contained objects.
5.3.3.1. Optional Clauses for DROP SCHEMA
IF EXISTSIf the schema does not exist, no changes will be performed. If this clause is not specified, an error is raised if the schema does not exist.
5.3.3.2. Who Can Alter a Schema
The DROP SCHEMA statement can be executed by:
The schema owner
Users with the
DROP ANY SCHEMAprivilege
See alsoSection 5.3.1, “CREATE SCHEMA”, Section 5.3.5, “RECREATE SCHEMA”
5.3.4. CREATE OR ALTER SCHEMA
Creates or alters a schema.
Available inDSQL
Syntax
CREATE OR ALTER SCHEMA schema-name
[DEFAULT CHARACTER SET [SYSTEM <period>] default-charset]
[DEFAULT SQL SECURITY {DEFINER | INVOKER}]
If the schema does not exist, creates it as if executing the equivalent Section 5.3.1, “CREATE SCHEMA”.
Otherwise, alters the schema as if executing Section 5.3.2, “ALTER SCHEMA” with equivalent SET options.
For further details, see Section 5.3.1, “CREATE SCHEMA”.
See alsoSection 5.3.2, “ALTER SCHEMA”, Section 5.3.1, “CREATE SCHEMA”, Section 5.3.5, “RECREATE SCHEMA”
5.3.5. RECREATE SCHEMA
Drops and creates a schema.
Available inDSQL
Syntax
RECREATE SCHEMA schema-name
[DEFAULT CHARACTER SET [SYSTEM <period>] default-charset]
[DEFAULT SQL SECURITY {DEFINER | INVOKER}]
If the schema already exists, drops it as if executing Section 5.3.3, “DROP SCHEMA”.
Next, it will create the schema as if executing Section 5.3.1, “CREATE SCHEMA”.
For further details, see Section 5.3.1, “CREATE SCHEMA” and Section 5.3.3, “DROP SCHEMA”.
See alsoSection 5.3.1, “CREATE SCHEMA”, Section 5.3.3, “DROP SCHEMA”