Question

MonetDB seems to support a fairly comprehensive set of system catalog views in order to discover the schema structure of the database. Unfortunately, I can't seem to find a SQL query that will obtain the set of columns for a given key or index. Here are the system tables/views are reported by the Tables table:

schemas types functions args sequences dependencies connections _tables _columns keys idxs triggers objects tables columns db_user_info users user_role auths privileges queryhistory callhistory querylog systemfunctions

I tried dependencies, but the IDs don't seem to match up. BTW, I did try looking in the source code, but I haven't yet found where the system views are created and maintained.

Was it helpful?

Solution

Two and a half years later, because I was intrigued by the question: You can indeed find the columns for a given key using the poorly named "objects" table.

For example, consider the following table

CREATE TABLE indextest (a INT, b INT);
ALTER TABLE indextest ADD CONSTRAINT indextest_pk PRIMARY KEY (a);
ALTER TABLE indextest ADD CONSTRAINT indextest_uq UNIQUE (a, b);                                                                           

Now let's find out which columns belong to indextest_uq:

SELECT idxs.id AS index_id, columns.id AS column_id, tables.name AS table_name, columns.name AS column_name, columns.type AS column_type 
FROM idxs JOIN objects ON idxs.id=objects.id JOIN tables ON idxs.table_id=tables.id JOIN columns ON idxs.table_id=columns.table_id AND objects.name=columns.name 
WHERE idxs.name='indextest_uq';

The result of this query looks like this:

+----------+-----------+------------+-------------+-------------+
| index_id | column_id | table_name | column_name | column_type |
+==========+===========+============+=============+=============+
|     6446 |      6438 | indextest  | a           | int         |
|     6446 |      6439 | indextest  | b           | int         |
+----------+-----------+------------+-------------+-------------+

Obviously, more information from the columns and tables tables could be included by extending the SELECT part of the query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top