Namespace Management

A namespace defines a group of tables, within which all of the table names must be uniquely identified. Namespaces permit you to do table privilege management as a group operation.

Namespace Resolution

You can grant authorization permissions to a namespace to determine who can access both the namespace and the tables within it.

To resolve a table from a table_name that appears in an SQL statement, the following rules apply:
    • If the table_name contains a namespace name, no resolution is needed, because a qualified table name uniquely identifies a table.
    • If you don't specify a namespace name explicitly, the namespace used is the one contained in the ExecuteOptions instance that is given as input to the executeSync(), execute(), or prepare() methods of TableAPI.
    • If ExecuteOptions doesn't specify a namespace, the default sysdefault namespace is used.

Using different namespaces in ExecuteOptions allows executing the same queries on separate but similar tables.

Manage Namespaces

SHOW NAMESPACES

The SHOW NAMESPACES statement provides the list of namespaces in the system. You can specify AS JSON if you want the output to be in JSON format.

Example 1: The following statement lists the namespaces present in the system.
SHOW NAMESPACES
Output:
namespaces
  sysdefault
Example 2: The following statement lists the namespaces present in the system in JSON format.
SHOW AS JSON NAMESPACES
Output:
{"namespaces" : ["sysdefault"]}

DROP NAMESPACE

You can remove a namespace by using the DROP NAMESPACE statement.

IF EXISTS is an optional clause. If you specify this clause, and if a namespace with the same name does not exist, no error is generated. If you don't specify this clause, and if a namespace with the same name does not exist, an error is generated indicating that the namespace does not exist.

CASCADE is an optional clause that enables you to specify whether to drop the tables and their indexes in this namespace. If you specify this clause, and if the namespace contains any tables, then the namespace together with all the tables in this namespace will be deleted. If you don't specify this clause, and if the namespace contains any tables, then an error is generated indicating that the namespace is not empty.

The following statement removes the namespace named ns1.
DROP NAMESPACE IF EXISTS ns1 CASCADE

Namespace scoped privileges

You can add one or more namespaces to your store, create tables within them, and grant permission for users to access namespaces and tables. You can grant the following permissions to users.

System-scoped Privileges:
  • CREATE_ANY_NAMESPACE
  • DROP_ANY_NAMESPACE
Namespace-scoped privileges:
  • CREATE_TABLE_IN_NAMESPACE
  • DROP_TABLE_IN_NAMESPACE
  • EVOLVE_TABLE_IN_NAMESPACE
  • CREATE_INDEX_IN_NAMESPACE
  • DROP_INDEX_IN_NAMESPACE
The following example creates a namespace, creates a table in the namespace, adds data to the table, and drops the namespace,. You also see the various privilege on the namespace being assigned to a role/user and revoked later.
CREATE NAMESPACE IF NOT EXISTS ns; 
GRANT MODIFY_IN_NAMESPACE ON NAMESPACE ns TO usersRole; 
CREATE TABLE ns:t (id INTEGER, name STRING, primary key (id));
INSERT INTO ns:t VALUES (1, 'Smith'); 
SELECT * FROM ns:t; 
REVOKE CREATE_TABLE_IN_NAMESPACE ON NAMESPACE ns FROM usersRole; 
DROP NAMESPACE ns CASCADE;

Note:

You can save all of the above commands as a sql script and execute it in a single command. If you want to execute any of the above commands outside of a SQL prompt, remove the semi colon at then end.