Query to display all tables and views - sql

I've come up with a query that displays all tables and views of a specific owner. What I would like to do now, but am having an issue with, is that I would like to have a second column which line by line will indicate whether the field is a "table" or "view". Is this possible? if so, how might I go about this?
select table_name
from all_tables
where owner = '<owner>'
UNION
select view_name
from all_views
where owner = '<owner>'
order by table_name;

I'd prefer the xxx_objects views myself for this purpose (as Justin says), but if you specifically need other data from the table and view views, you can add extra info thus:
select 'Table' AS object_type, table_name
from all_tables
where owner = '<owner>'
UNION ALL
select 'View' AS object_type, view_name
from all_views
where owner = '<owner>'
order by table_name;
Note I've changed it to use UNION ALL because there will be no collisions between the two result sets.

I'd use all_objects instead
select object_name, object_type
from all_objects
where object_type in ('TABLE', 'VIEW')
and owner = <<schema name>>
order by object_name

Related

An equivalence of ".schema sqlite_master" in Oracle

I'm trying to do the same exercise for both SQLite and Oracle. In SQLite, there is a table sqlite_master containing a description of all of the other tables, indexes, triggers, and views that are contained within the database. I can see the query to generate sqlite_master with .schema sqlite_master.
In Oracle, the data dictionary is presented to us in the form of a number of views (DBA, ALL or USER). Let's take the table USER_TABLES as an example. We can do query on USER_TABLES, for example
SELECT table_name
FROM USER_TABLES;
Is there anyway to get the query used to create the table USER_TABLES in Oracle? I tried
SELECT dbms_metadata.get_ddl('TABLE', 'USER_TABLES')
FROM dual;
but it does not work.
I am not sure what are you looking for but if you want the definition of any table in oracle you can use :
Describe TableName
Or if you want to have list of tables, views or columns you can use below queries:
For Tables :
select * from select * from all_tables
For Columns :
select * from all_tab_columns
For Views :
select * from select * from all_views
To get all the column information of a table:
select *
from all_tab_columns
where upper(table_name) = upper('Test')
order by column_id
Typically oracle stores table_name in uppercase so I used upper() or you can just type 'TEST'
You could use this one:
DECLARE
DDL CLOB;
BEGIN
FOR aTab IN (SELECT TABLE_NAME FROM USER_TABLES) LOOP
DDL := DBMS_METADATA.GET_DDL('TABLE', aTab.TABLE_NAME);
DBMS_OUTPUT.PUT_LINE(DDL);
END LOOP;
END;
You may customize the output with DBMS_METADATA.SET_TRANSFORM_PARAM() before you run the query.

Oracle SQL Developer report to make DDL of DB object

I've wrote a report for SQL Developer to generate the DDL of a DB object.
SELECT DBMS_METADATA.GET_DDL(UPPER(:OBJECT_TYPE),UPPER(:OBJECT_NAME),UPPER(:OBJECT_SCHEMA)) DDL FROM DUAL
UNION ALL
select DBMS_METADATA.GET_DDL (UPPER('CONSTRAINT'),UPPER(CONSTRAINT_NAME),UPPER(:OBJECT_SCHEMA)) AS "DDL OGGETTI DIPENDENTI" from DBA_CONSTRAINTS where OWNER like UPPER(:OBJECT_SCHEMA) and TABLE_NAME like UPPER(:OBJECT_NAME) and CONSTRAINT_TYPE not like 'R'
UNION ALL
select DBMS_METADATA.GET_DDL (UPPER('REF_CONSTRAINT'),UPPER(CONSTRAINT_NAME),UPPER(:OBJECT_SCHEMA)) from DBA_CONSTRAINTS where OWNER like UPPER(:OBJECT_SCHEMA) and TABLE_NAME like UPPER(:OBJECT_NAME) and CONSTRAINT_TYPE like 'R'
UNION ALL
select DBMS_METADATA.GET_DDL (UPPER('INDEX'),UPPER(INDEX_NAME),UPPER(:OBJECT_SCHEMA)) from DBA_INDEXES where OWNER like UPPER(:OBJECT_SCHEMA) and TABLE_NAME like UPPER(:OBJECT_NAME)
UNION ALL
select DBMS_METADATA.GET_DEPENDENT_DDL (UPPER('COMMENT'),UPPER(TABLE_NAME),UPPER(:OBJECT_SCHEMA)) from DBA_COL_COMMENTS where OWNER like UPPER(:OBJECT_SCHEMA) and TABLE_NAME like UPPER(:OBJECT_NAME) AND COMMENTS is not null GROUP BY TABLE_NAME
UNION ALL
select DBMS_METADATA.GET_DEPENDENT_DDL (UPPER('OBJECT_GRANT'),UPPER(TABLE_NAME),UPPER(:OBJECT_SCHEMA)) from DBA_TAB_PRIVS where OWNER like UPPER(:OBJECT_SCHEMA) and TABLE_NAME like UPPER(:OBJECT_NAME) GROUP BY TABLE_NAME
UNION ALL
select DBMS_METADATA.GET_DDL (UPPER('SYNONYM'),UPPER(TABLE_NAME),UPPER('PUBLIC')) from DBA_SYNONYMS where TABLE_OWNER like UPPER(:OBJECT_SCHEMA) and TABLE_NAME like UPPER(:OBJECT_NAME) GROUP BY TABLE_NAME
UNION ALL
select DBMS_METADATA.GET_DEPENDENT_DDL (UPPER('TRIGGER'),UPPER(TABLE_NAME),UPPER(:OBJECT_SCHEMA)) from DBA_TRIGGERS where TABLE_OWNER like UPPER(:OBJECT_SCHEMA) and TABLE_NAME like UPPER(:OBJECT_NAME) GROUP BY TABLE_NAME;
The result type of function DBMS_METADATA.GET_DDL is CLOB.
The problem is when i copy the strings output in notepad or word:
If I choose TABLE for report style every row of the output is quoted with " ". for example: " CREATE TABLE "MWPROD"."ORDINI" ... TABLESPACE "MWPROD_TBSDAT" ;"
If I choose SCRIPT for report style some rows are truncate.
I want found a solution for generate a clean output with complete rows without quotes.
the DDL is quoted by the database - it's to account for idiot developers - sorry, developers who name tables with reserved words, like "TABLE", or for folks that need case sensitive object names
For the truncated script output, use the SET LONG command in your script.
SET LONG {80 | n}
Sets maximum width (in bytes) for displaying CLOB, LONG, NCLOB and XMLType values; and for copying LONG values.

SQL Statement to Find Out Which Schema Owns an Oracle Table?

What is a SQL statement to find out which Schema owns an Oracle table?
To see information about any object in the database, in your case USER_TABLES use:
select * from all_objects where object_name = 'USER_TABLES';
OWNER OBJECT_NAME OBJECT_ID OBJECT_TYPE CREATED LAST_DDL_TIME
SYS USER_TABLES 3922 VIEW 24-MAY-13 24-MAY-13
USER_TABLES is a dictionary view. All dictionary views are owned by SYS.
SELECT OWNER FROM DBA_TABLES WHERE TABLE_NAME = '<your table>'
If you don't have privilege to DBA_TABLES use ALL_TABLES.
Here are the queries:
SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME='<TABLE_NAME>'; -- There is no owner column in this view
SELECT OWNER, TABLE_NAME FROM ALL_TABLES WHERE OWNER='<OWNER_NAME>' AND TABLE_NAME='<TABLE_NAME>';
SELECT OWNER, TABLE_NAME FROM DBA_TABLES WHERE OWNER='<OWNER_NAME>' AND TABLE_NAME='<TABLE_NAME>';
Also you can query USER_OBJECTS, ALL_OBJECTS, DBA_OBJECTS using OBJECT_TYPE='TABLE' and OWNER= '<OWNER_NAME>' and OBJECT_NAME='<TABLE_NAME>'
There are 3 views, which can give that information:
USER_TABLES, if the login schema is the owner of the table
ALL_TABLES, if the login schema have permission on the table or owner of the table or have SELECT_CATALOG role
DBA_TABLES, if the login schema have SELECT_CATALOG role or any other role which have SELECT_CATALOG role

How to check if a database object in Oracle is a table or view

I have list of object names from which i have to find out whether the object is a table or view. For this reason i have to query and check in all_tables and all_views and confirm whether the object is table or view. I am using below queries and its working. But as i have huge list of object names I want to perform this in single query and check whether the object is table or view and also the owner of the object.
select * from ALL_views where view_name like '%INSTANCE%'
select * from all_tables where table_name like '%INSTANCE%'
select *
from all_objects
where object_name like '%INSTANCE%'
There is an OBJECT_TYPE column in there.
How about using all_objects instead?
E.g.:
select owner,
object_name,
object_type
from all_objects
where object_type in ('TABLE', 'VIEW')
and object_name in (....);

Get Columns Names of Table

How I can Get for a specific Table its columns Names ?
I tried this :
SELECT column_name
FROM USER_TAB_COLUMNS
WHERE table_name = 'x' ;
But it doesn't work.
Try this :
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TABLENAME'
Hope this helps.
EDIT :
The Oracle equivalent for information_schema.COLUMNS is USER_TAB_COLS for tables owned by the current user, ALL_TAB_COLS or DBA_TAB_COLS for tables owned by all users.
Tablespace is not equivalent to a schema, neither do you have to provide the tablespace name.
Providing the schema/username would be of use if you want to query ALL_TAB_COLS or DBA_TAB_COLS for columns OF tables owned by a specific user. in your case, I'd imagine the query would look something like:
String sqlStr= "
SELECT column_name
FROM all_tab_cols
WHERE table_name = 'users'
AND owner = ' || +_db+ || '
AND column_name NOT IN ( 'password', 'version', 'id' )
"
Note that with this approach, you risk SQL injection.
Source : Oracle query to fetch column names
String comparisons in Oracle are case sensitive by default, and table and column names are upper case by default, so you need to make sure that the capitalization of the table name you are searching for matches the the way it's stored in the database, so unless your table was named with mixed case or all lower case try making sure your string is all upper case.
SELECT column_name from USER_TAB_COLUMNS
WHERE table_name = 'X'; -- not 'x'
Additionally, if you don't own the table, then you need to use either ALL_TAB_COLUMNS or DBA_TAB_COLUMNS instead of USER_TAB_COLUMNS since USER_TAB_COLUMNS only lists details for tables owned by your current schema.
You can just describe the table:
desc x;
Try to queryUSER_TAB_COLUMNS view
SELECT column_name
FROM USER_TAB_COLUMNS
WHERE table_name = 'TABLE_NAME'