Oracle has a query that selects the existing indexes of a table.
For example:
SELECT * FROM user_indexes WHERE table_name = 'CM_WCEL';
But I need to recreate the index creation statement.
How can I get the remaining information like the affected columns, etc?
To get the complete DDL for each index, use dbms_metadata.get_ddl():
select index_name, dbms_metadata.get_ddl('INDEX', index_name) as ddl
from user_indexes
where table_name = CM_WCEL';
The DDL is returned as a CLOB. Depending on the SQL client you are using you might need some configuration changes to be able to see the complete code. e.g. in SQL*Plus you need something set long 60000 before you run the select statement.
As per the creation of table, the below tables will have the requested information.
SELECT *
FROM user_ind_columns
WHERE table_name = 'CM_WCEL';
or
SELECT *
FROM dba_ind_columns
WHERE table_name = 'CM_WCEL';
this should provide needed information:
select index_name, column_name
from user_ind_columns
where table_name = 'CM_WCEL';
Related
In Snowflake, I have a table of the same name (MY_TABLE ) across multiple databases. Within a single database, I can find the number of rows using the following code:
select table_catalog, table_name, row_count
from information_schema.tables
where table_name = 'MY_TABLE';
Is there a way to find the number of rows for MY_TABLE across all databases (or across a specified list)?
Edit: I currently do not have ACCOUNT_USAGE schema authorization.
Thank you for your help.
Just use the SNOWFLAKE.ACCOUNT_USAGE.TABLES view
select table_catalog, table_schema, table_name, row_count
from SNOWFLAKE.ACCOUNT_USAGE.TABLES
where table_name = 'MY_TABLE';
N.B. Latency for the view may be up to 90 minutes
If you don't have access to the ACCOUNT_USAGE schema then an alternative is the "show" command e.g.
show tables like 'MY_TABLE' in account;
I am doing a procedure in the oracle database that has the function of performing a kind of inventory of the objects of the database.
Basically I must get the DDL of objects of type table.
For this, I am using queries from the bank itself as:
select * from user_objects;
select * from user_constraints;
select * from user_source;
My inventory must contain the following information:
Inventory information here.
How do I get the DDL command from objects without using the function:
dbms_metadata.get_ddl();
and no other ready functions from the metadata library.
I have also tried this:
SELECT
(CASE WHEN line = 1 THEN 'create or replace ' || text ELSE text END) texto
FROM user_source
WHERE NAME = '....'
ORDER BY line
but this command does not get the ddl of table objects.
For getting the DDL of views, it's very easy:
SELECT VIEW_NAME, TEXT FROM ALL_VIEWS;
If you want it to return just the text of a particular view, then do:
SELECT TEXT FROM ALL_VIEWS
WHERE VIEW_NAME LIKE '[name_of_view]';
Getting the DDL for tables is more cumbersome, but can be done by querying the data from several system views:
ALL_TABLES ALL_TAB_COLUMNS ALL_COL_COMMENTS
ALL_CONSTRAINTS ALL_CONS_COLUMNS ALL_INDEXES
ALL_IND_COMMENTS
For example, if you wanted to get all column names and their data types for TABLE1, you would do:
SELECT COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME LIKE 'TABLE1';
To get a list of all constraints on a table, the query is:
SELECT * FROM ALL_CONSTRAINTS
WHERE TABLE_NAME LIKE 'TABLE1';
To get a full table definition takes a fairly good understanding of how to use these system views. A very helpful page for this can be found here: 6 Useful Oracle Data Dictionary Queries Every DBA Should Have
Is it possible to select the first 20 columns from my table, without naming out each column in the select?
The columns are always ordered in the same way when I do a select, so there must be some underlying order.
The below query forms the SQL for you. It uses the dictionary table all_tab_columns to fetch the column names for the table.
SELECT ' SELECT '
|| REPLACE(LISTAGG(column_name,',') WITHIN GROUP( ORDER BY column_id),',',','
||CHR(10))
|| ' FROM YOUR_TABLE'
FROM all_tab_columns
WHERE owner ='YOUR_SCHEMA_NAME'
AND table_name='YOUR_TABLE_NAME'
AND column_id <= 20;
you can use column index instead of column name like select 0,1,2,.....
There is a table in a SQL Server database called sysColumns that records all the columns in every table. I think it is a SQL standard and should be in Oracle too.
EDIT: thanks to comment from #davegreen100, this table is in Oracle but is named DBA_TAB_COLUMNS.
try running Select * from DBA_TAB_COLUMNS and see what the results are, and work from there.
If it's there (in Oracle), you will eventually end up with something like
Select name from DBA_TAB_COLUMNS
Where id = #tableId -- <--- the id of the table
and colOrder <= 20
Your final SQL will be probably have to be generated dynamically using the output from the above
Is there an HSQLDB equivalent for the MYSQL SHOW COLUMNS from TABLE command?
HSQLDB does not have separate commands for showing tables, columns or other database objects.
You use SELECT * FROM INFORMATION_SCHEMA.COLUMNS and various other tables in the INFORMATION_SCHEMA for such purposes.
http://hsqldb.org/doc/2.0/guide/databaseobjects-chapt.html#dbc_information_schema
This is the query that you need.
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '<your schema>' AND TABLE_NAME = '<name table or view>';
I want to write a program that can show the user a list of tables in the database and also show the descriptions of those tables. So can I do a "select * from system_table" or something like that?
This will give you a list of tables:
show tables;
To describe each table:
describe table_name;
To get both at the same time try:
SELECT * FROM DOMAIN.TABLES WHERE TYPE = 'TABLE'
SELECT * FROM DOMAIN.COLUMNS WHERE TABLETYPE = 'TABLE'
The results are similar to MySql show and describe statements
In addition to show tables, MySQL 5.0+ also supports the INFORMATION_SCHEMA meta-database:
SELECT table_name, table_comment FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name';
information_schema.tables also has other information in it, if you're curious.
Note that if you didn't provide a comment when you created the table and use InnoDB, it will fill the table_comment column with unnecessary data, such as the InnoDB space reserved for this table or the foreign key constraints.