6

I am developing an Oracle database schema visualizer. So, as a first step, I thought, I will first need to get all the schema details (tables and relationships between tables, constraints also maybe).

To get that information, what is the SQL command that will return the result?

(the DESCRIBE command does not return the information about the all the table)

EDIT #1:

Actually, what I want to do is to get all the information about all tables as I mentioned (columns, rows, foreign keys, constraints) to store them ina MongoDB database, and then create visualizations (diagrams are not included)

1
  • 3
    There is no single statement for this. You need to query several system tables: docs.oracle.com/database/121/REFRN/toc.htm Start with all_tables then you need all_tab_columns probably all_indexes and all_ind_columns and all_ind_expression. If you want constraints you need all_constraints and all_cons_columns. Btw: describe is not a SQL statement. It's a command specific to SQL*Plus and can't be used from inside a programming language anyway
    – user330315
    May 3, 2016 at 21:41

3 Answers 3

7

I do something similar. I read those things from a SQL Server over OPENQUERY statements directly from Oracle DBs and save results into SQL Server tables to allow analysis of historic comparison schema information and changes.

So what you have to do with the resultsets of the following queries is to store them (regulary) somehow and add some kind of unique / primary key or timestamp to it, in order to distinguish between your different scans.

Leaving away the SQL Server specific code stuff, those are the basic oracle sql queries I use so far:

--Tables
SELECT table_name, owner, Tablespace_name, Num_Rows 
FROM all_tables WHERE tablespace_name is not NULL 
AND owner not in ('SYS', 'SYSTEM') 
ORDER BY  owner, table_name;

--Columns
SELECT OWNER, TABLE_NAME, Column_name, Data_type, data_length, data_precision, NULLABLE, character_Set_Name  
From all_tab_cols 
where USER_GENERATED = 'YES' 
AND owner not in ('SYS', 'SYSTEM');

--Indexes
select Owner, index_name, table_name, uniqueness,BLEVEL,STATUS from ALL_INDEXES
WHERE owner not in ('SYS', 'SYSTEM') 

--Constraints
select owner, constraint_name, constraint_type, table_name, search_condition, status, index_name, index_owner 
From all_constraints 
WHERE generated = 'USER NAME'
AND owner not in ('SYS', 'SYSTEM') 

--Role Previleges
select grantee, granted_role, admin_option, delegate_option, default_role, common
From DBA_ROLE_PRIVS

--Sys Privileges
select grantee, privilege, admin_option, common
From DBA_SYS_PRIVS 
1

You could start with:

select DBMS_METADATA.GET_DDL ('TABLE', table_name,user) from user_tables;

That will reverse engineer all the DDL that would create the tables. It wouldn't give you current table/column statistics though.

Some tools (eg Oracle's Data Modeller / SQL Developer) will allow you to point them at a database and it will reverse engineer the database model into a diagram. That would be a good place to start for the relationships between tables.

1
  • Actually, what I want to do is to get all the information about all tables as I mentioned (columns, rows, foreign keys, constraints) to store them ina MongoDB database, and then create visualizations (diagrams are not included).
    – devio
    May 4, 2016 at 7:49
0

MongoDB supports JSON documents, so the best way to solve your issue is to extract database metadata into JSON docuemnts. You can use SchemaCrawler to extract database metadata as JSON.

Sualeh Fatehi, SchemaCrawler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.