How to list schemas of a specific database in Informix using SQL?

♀尐吖头ヾ 提交于 2020-01-05 17:41:09

问题


What is the query to get a list of schemas names in a specific database in Informix?


回答1:


A schema belongs to a user. You can list all available users from the sysusers system catalog :

SELECT username FROM "informix".sysusers;

Since only DBAs and Resource privilieges allow a user to issue a CREATE SCHEMA statement, we could restrict the query like :

SELECT username FROM "informix".sysusers WHERE usertype IN ('D', 'R');

Another solution is to list only users that actually have created tables ; for that, you can query the systables system catalog and list distinct owners.

SELECT DISTINCT owner FROM FROM "informix".systables

As commented by @JonathanLeffler, a user could have been granted RESOURCE privileges and have created a table, and then be 'demoted' to CONNECT privileges. The user would still own the table. Hence the second solution is the most accurate.




回答2:


Schemas are not commonly used in Informix databases and have very little trackability within a database. The CREATE SCHEMA notation is supported because it was part of SQL-89. The AUTHORIZATION clause is used to determine the (default) 'owner' of the objects created with the CREATE SCHEMA statement. There is nothing to stop a single user running the CREATE SCHEMA statement multiple times, either consecutively or at widely different times (in any given database within an Informix instance).

CREATE SCHEMA AUTHORIZATION "pokemon"
    CREATE TABLE gizmo (s SERIAL NOT NULL PRIMARY KEY, v VARCHAR(20) NOT NULL)
    CREATE TABLE widget(t SERIAL NOT NULL PRIMARY KEY, d DATETIME YEAR TO SECOND NOT NULL)
    ;

CREATE SCHEMA AUTHORIZATION "pokemon"
    CREATE TABLE object   (u SERIAL NOT NULL PRIMARY KEY, i INTEGER NOT NULL)
    CREATE TABLE "pikachu".complain (C SERIAL NOT NULL PRIMARY KEY, v VARCHAR(255) NOT NULL)
    ;

After the CREATE SCHEMA statement executes, there is no way of tracking that either pair of these tables were created together as part of the same schema; there's no way to know that "pikachu".complain was part of a CREATE SCHEMA statement executed on behalf of "pokemon". There is no DROP SCHEMA statement that would necessitate such support.



来源:https://stackoverflow.com/questions/54507381/how-to-list-schemas-of-a-specific-database-in-informix-using-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!