<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>
        <title>Obtaining Database Information in CUBRID</title>
        <link>http://www.cubrid.org/?mid=cubrid_database_information</link>
        <description>Obtaining Database Information in CUBRID</description>
        <language>en</language>
        <pubDate>Thu, 23 Jun 2011 08:22:58 -0800</pubDate>
        <lastBuildDate>Fri, 24 Jun 2011 04:31:08 -0800</lastBuildDate>
        <generator>XpressEngine 1.4.4.1</generator>
                        										        <item>
            <title>Obtaining Database Information in CUBRID</title>
            <dc:creator>admin</dc:creator>
            <link>http://www.cubrid.org/cubrid_database_information</link>
            <guid isPermaLink="true">http://www.cubrid.org/cubrid_database_information</guid>
                                    <description><![CDATA[<h1>Obtaining Database Information in CUBRID</h1>

<p>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.</p><h2>Users List</h2><p>The list of database users is stored in a system table called <a href="/manual/840/en/db_user" target="_self"><b>db_user</b></a>. Follow this link to see the manual where the table columns are explained.</p><p>To get the list of users for a database being used, execute the following query.</p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">SELECT name FROM db_user;</div>

<p>You will see the results similar to the following.</p>

<p><img src="http://www.cubrid.org/files/attach/images/49/005/197/cubrid-db-users.png" alt="List of Users" width="129" height="114" editor_component="image_link"/></p>

<p>Also to retrieve the user name that is currently logged in to the database, one can retrieve <b><a href="/manual/840/en/CURRENT_USER" target="_self">CURRENT_USER</a></b> or <b>USER</b> which can be used interchangeably.</p><p></p><div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">SELECT CURRENT_USER;</div>

<p>The result will show the current user name.</p>

<p><img src="http://www.cubrid.org/files/attach/images/49/005/197/cubrid-current-user.png" alt="Current User" width="148" height="96" editor_component="image_link"/></p>

<h2>Tables List</h2>

<p>The list of database tables is stored in a system table called&nbsp;<a href="/manual/840/en/DB_CLASS" target="_self"><b>db_class</b></a>. In CUBRID, whenever you meet the term&nbsp;<b>class</b>, you can refer to it as to a <i>table</i>. There are two types of tables in CUBRID:</p>

<ol><li><b>System Tables</b><br />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&nbsp;do. If you see a table which starts with underscore like <b>_db_class</b>, then they are accessible by database administrators only. The system tables without underscore like <b>db_class</b> can be accessed by non-admin users.</li><li><b>User Tables</b><br />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.</li></ol>

<p>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.</p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">SELECT class_name FROM db_class;</div>

<p>To get the list of only user defined tables, i.e. those explicitly created by the user, one should filter out by&nbsp;<b>is_system_class</b> column of the <b>db_class</b> table as shown below.</p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">SELECT class_name FROM db_class WHERE is_system_class = 'NO';</div>

<p>For other possible attributes users can use, see the <b>db_class</b> documentation.</p>

<h2>Table Columns List</h2>

<p>The list of columns defined for database tables is stored in a system table called&nbsp;<b><a href="/manual/840/en/DB_ATTRIBUTE" target="_self">db_attribute</a></b>. In CUBRID, whenever you meet the term&nbsp;<b>attribute</b>, you can refer to it as to a <i>column</i>.&nbsp;There is also&nbsp;<b>_db_attribute</b>&nbsp;table, but as mentioned above, this class is accessible by only administrators.</p>

<p>Thus, to get a list of all columns created in a particular table correctly ordered, one must execute the following query.</p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">
SELECT * FROM db_attribute
WHERE class_name = 'game'
ORDER BY def_order
</div>

<p>For other possible attributes, see the <b>db_attribute</b> documentation.</p>

<h2>Index List</h2>

<p>The list of indexes created in the database is stored in a system table called&nbsp;<b><a href="/manual/840/en/DB_INDEX" target="_self">db_index</a></b>. There is also <b>_db_index</b> 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.</p><p>Thus, to get a list of all indexes created for all table in the current database, one must execute the following query.</p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">SELECT *&nbsp;FROM db_index;</div>

<p>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.</p><p>If you want to see indexes for only those table which were created by you, then you need to join <b>db_index</b> and <b>db_class</b> tables just like you would do normal SQL query.</p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">
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';
</div>

<p>For learn more about other index attributes, see the <b>db_index</b> documentation.</p><h3>Index Keys List</h3>

<p>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 <b><a href="/manual/840/en/DB_INDEX_KEY" target="_self">db_index_key</a></b>.</p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">SELECT * FROM db_index_key;</div>

<p>If you see the documentation, you can understand different attributes that an index can have in CUBRID.</p><p>There are many other information we can retrieve for a database in CUBRID. Later, we will update this article and add some more examples.</p>]]></description>
                        <pubDate>Thu, 23 Jun 2011 07:23:24 -0800</pubDate>
                        <category>sql</category>
                        <category>index</category>
                        <category>index key</category>
                        <category>table</category>
                        <category>user</category>
                        <category>system table</category>
                        <category>user table</category>
                                </item>
            </channel>
</rss>
