Obtaining Database Information in CUBRID
This guide explains how to retrieve database information from CUBRID. This includes: table, index, and column names, if certain columns are indexes or not, which one is primary key, etc. In simple words, all this information can be retrieved from CUBRID's system tables. Every database in CUBRID has a table for users, another table for list of tables in this database, and the other one for list of columns in all tables, etc.
The list of database users is stored in a system table called db_user. Follow this link to see the manual where the table columns are explained.
To get the list of users for a database being used, execute the following query.
SELECT name FROM db_user;
You will see the results similar to the following.
Also to retrieve the user name that is currently logged in to the database, one can retrieve CURRENT_USER or USER which can be used interchangeably.
The result will show the current user name.
The list of database tables is stored in a system table called db_class. In CUBRID, whenever you meet the term class, you can refer to it as to a table. There are two types of tables in CUBRID:
- System Tables
These are the table used primarily by CUBRID server itself to keep track of various meta data of the database. Normally, ordinary database users do not have access to system tables, but users with administrative privileges do. If you see a table which starts with underscore like _db_class, then they are accessible by database administrators only. The system tables without underscore like db_class can be accessed by non-admin users.
- User Tables
These are the table which the database users have created by themselves. Normally they are use in some applications to store application data. So all users with granted rights can access them. As mentioned above, normal users can access to some of the system tables.
Thus, to get a list of all tables a user has access to (this includes both system and user defined), one must execute the following query.
SELECT class_name FROM db_class;
To get the list of only user defined tables, i.e. those explicitly created by the user, one should filter out by is_system_class column of the db_class table as shown below.
SELECT class_name FROM db_class WHERE is_system_class = 'NO';
For other possible attributes users can use, see the db_class documentation.
Table Columns List
The list of columns defined for database tables is stored in a system table called db_attribute. In CUBRID, whenever you meet the term attribute, you can refer to it as to a column. There is also _db_attribute table, but as mentioned above, this class is accessible by only administrators.
Thus, to get a list of all columns created in a particular table correctly ordered, one must execute the following query.
SELECT * FROM db_attribute WHERE class_name = 'game' ORDER BY def_order
For other possible attributes, see the db_attribute documentation.
The list of indexes created in the database is stored in a system table called db_index. There is also _db_index table, but as mentioned above, this class is accessible by only administrators, and in practice you do not want to mess with it, though they have similar structure.
Thus, to get a list of all indexes created for all table in the current database, one must execute the following query.
SELECT * FROM db_index;
The result will include indexes defined for both system table and user created tables. You can also see if a particular index is a UNIQUE, a PK, or an FK.
If you want to see indexes for only those table which were created by you, then you need to join db_index and db_class tables just like you would do normal SQL query.
SELECT di.* FROM db_index as di LEFT JOIN db_class as dc ON di.class_name = dc.class_name WHERE is_system_class = 'NO';
For learn more about other index attributes, see the db_index documentation.
Index Keys List
In the above example we were able to retrieve a list of table the database has, then filtered them down to user defined. However, at this point we cannot understand which column a particular index was created on. This means we need to retrieve the information about index keys. There is a table for this named as db_index_key.
SELECT * FROM db_index_key;
If you see the documentation, you can understand different attributes that an index can have in CUBRID.
There are many other information we can retrieve for a database in CUBRID. Later, we will update this article and add some more examples.
This article has been written by one of the CUBRID core developers to help users improve their application performance by understanding h...
Starting from version 8.4.0 CUBRID supports Covering Index. Wikipedia explains it as:
The scope of this tutorial is to show you how to use one of the most special features implemented in CUBRID – Click Counter.
Sometimes you want to concatenate all column values from differenct rows returned by your SELECT statement in one value. For example, consider the c...