System catalog
The system catalog holds the schema of a database: its tables, columns, indexes, users, permissions, partitions, triggers and stored procedures. CoraDB keeps it up to date automatically whenever a DDL statement or a GRANT / REVOKE runs, and it can be read with ordinary SELECT statements.
The catalog has two layers:
- System catalog classes such as
_db_classand_db_attributestore the information, with numeric codes and references between rows. Only DBA can read them. - System catalog views such as
db_classanddb_attributeshow the same information with names and readable values. Every user can read them, and each user sees only the tables they may access.
In most cases, query the views.
Common catalog queries
Section titled “Common catalog queries”The examples use the e-commerce sample database.
The relational tables (vertex and edge tables have the class types 'VERTEX' and 'EDGE', views 'VCLASS'):
SELECT class_name FROM db_classWHERE is_system_class = 'NO' AND class_type = 'CLASS'ORDER BY class_name;| class_name |
|---|
| countries |
| order_items |
| orders |
The columns of a table, in order:
SELECT attr_name, data_type, prec, scale, is_nullableFROM db_attributeWHERE class_name = 'orders'ORDER BY def_order;| attr_name | data_type | prec | scale | is_nullable |
|---|---|---|---|---|
| id | INTEGER | 10 | 0 | NO |
| customer_id | INTEGER | 10 | 0 | NO |
| shipped_date | DATE | 10 | 0 | YES |
| shipping_cost | NUMERIC | 10 | 2 | NO |
| ship_address | STRING | 128 | 0 | NO |
| order_date | DATE | 10 | 0 | NO |
The indexes of a table with their key columns:
SELECT i.index_name, k.key_attr_name, i.is_unique, i.is_primary_key, i.is_foreign_keyFROM db_index i JOIN db_index_key k ON i.class_name = k.class_name AND i.index_name = k.index_nameWHERE i.class_name = 'orders'ORDER BY i.index_name, k.key_order;| index_name | key_attr_name | is_unique | is_primary_key | is_foreign_key |
|---|---|---|---|---|
| fk_orders_customer_id | customer_id | NO | NO | YES |
| idx_orders_order_date | order_date | NO | NO | NO |
| idx_orders_shipped_date | shipped_date | NO | NO | NO |
| pk_orders_id | id | YES | YES | NO |
Other views answer other questions:
| To find | Query |
|---|---|
| Views and their queries | db_vclass |
| Permissions on a table | db_auth |
| Partitions of a table | db_partition |
| Triggers on a table | db_trig |
| Stored functions and procedures | db_stored_procedure |
| Collations and charsets | db_collation, db_charset |
| Superclasses | db_direct_super_class |
In corasql, the session commands ;schema table_name and ;info show the same information without writing a query, and SHOW statements such as SHOW COLUMNS and SHOW INDEX return it as result sets.
Catalog authorization
Section titled “Catalog authorization”- The catalog classes and views are owned by DBA.
- Only DBA and members of the DBA group can read the catalog classes. Other users get an error such as
SELECT is not authorized on _db_class. - Every user can read the catalog views. A user other than DBA sees only the tables the user owns, tables owned by a group the user belongs to, and tables on which the user has SELECT permission. In the e-commerce database, where DBA owns all tables, the user PUBLIC therefore finds no user tables in
db_class. - Nobody, not even DBA, can change the catalog directly. UPDATE, INSERT and DELETE on a catalog class fail with an authorization error (
UPDATE is not authorized on _db_class.), and catalog views are not updatable (Vclass db_class is not updatable.). The catalog changes only through DDL, user and permission statements.
Querying the catalog
Section titled “Querying the catalog”CoraDB stores the names of tables, views, columns, indexes, triggers and methods in lowercase, whatever case they were written in. Compare them with lowercase strings:
CREATE TABLE GiftCard (Code VARCHAR(16));
SELECT class_name FROM db_class WHERE class_name = 'GiftCard'; -- no rowsSELECT class_name FROM db_class WHERE class_name = 'giftcard'; -- giftcardSELECT attr_name FROM db_attribute WHERE class_name = 'giftcard'; -- codeUser names are stored in uppercase:
CREATE USER analyst;
SELECT name FROM db_user WHERE name = 'ANALYST'; -- ANALYSTWith the default collation utf8_bin, string comparisons are case-sensitive, so LOWER() or UPPER() on the search value avoids misses when the case is not known: WHERE class_name = LOWER('GiftCard').