Oracle: SQL query to find all the triggers belonging to the tables? - sql

how can i find all the triggers that belong to a table?

The following will work independent of your database privileges:
select * from all_triggers
where table_name = 'YOUR_TABLE'
The following alternate options may or may not work depending on your assigned database privileges:
select * from DBA_TRIGGERS
or
select * from USER_TRIGGERS

Check out ALL_TRIGGERS:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#i1592586

Another table that is useful is:
SELECT * FROM user_objects WHERE object_type='TRIGGER';
You can also use this to query views, indexes etc etc

Use the Oracle documentation and search for keyword "trigger" in your browser.
This approach should work with other metadata type questions.

Related

Choosing table from the right owner in Oracle SQL

I am quite new to SQL, and my first "job" is to get something out of an Oracle SQL database.
Just to see what's actually found in my connection I use the following:
SELECT owner, table_name FROM dba_tables
This gives me a bunch of tuples with an owner name and table_name. However, some table names are the same for different owners.
So when I run a command like:
SELECT * FROM MyTableName
How do I ensure that this table is coming from owner1 and not owner2, where both of them actually have a table called MyTableName ?
You can do:
SELECT * FROM <owner>.MyTableName
You can specify exactly which table with
select * from some_owner.my_table;
If you do this:
select * from my_table;
You will be selecting from the version of MY_TABLE that belongs to the owner/user with which you you are connected to the database. So, if you connected as user SCOTT, then
select * from my_table;
effectively becomes
select * from scott.my_table;
Note that this behavior can be overridden by the use of synonyms. Suppose user SCOTT has a synonym like this:
create synonym scott.my_table for fred.my_table;
Then SCOTT issues
select * from my_table;
Then oracle will check first for a synonym, and finding it will effectively transform the query to
select * from fred.my_table;
So in the end, the precedence is this. When you reference a table without qualifying it with the owner, oracle will look for it in this sequence:
is there a user synonym by that name? if so, use what it references if the user has been granted necessary privileges.
is there a global synonym by that name? if so, use what it references if the user has been granted necessary privileges.
does the user itself own a table by that name? if so, use that table.
return error "ORA-00942: table or view does not exist"

How do I write the table structure in Oracle from an existing schema?

I have so far figured out that to describe a table I can use the below:
select dbms_metadata.get_ddl('TABLE','<my table name>','<table owner>') from dual;
I also found that I can get a list of tables from the current user using the below statement:
select table_name from user_tables;
However I need to find a way to combine these two so I get a (preferably SQL file) output which basically describes all the tables in the current schema. How can I go about that?
Call dbms_metadata in your query on user_tables:
select dbms_metadata.get_ddl('TABLE',table_name,user)
from user_tables;

How to list the user created table name in sql?

I created like 20 tables in sql 11g and lost the record of them. Is there any way I can list the table names I created.
SELECT table_name FROM user_tables is not the solution.
First, log on as sys before you can view the tables in the entire database.
Second, run this query using the sys
SELECT owner, table_name FROM dba_tables WHERE owner='HR';
This should display all tables owned by the HR schema. Remember to use uppercase for the owner (HR) else you will receive error.
Hope this helps.
The Best approach to solve this issue is considering the attribute of ALL_ALL_TABLES named
LAST_ANALYZED
It gives Date on which the table was most recently analyzed.
So you could easily query the database with the help of DATE Functions.
The Oracle Database Reference helps you.
SELECT TABLE_NAME FROM USER_ALL_TABLES
Well, if user_all_tables is not your desired result, you will have to rely on dba_tables via:
SELECT owner, table_name
FROM dba_tables
But for this you need more privs than for user_tables obviously

SQL/JDBC : select query on variable tablenames

I'm using Oracle DB and I would like to write a SQL query that I could then call with JDBC. I'm not very familiar with SQL so if someone can help me, that could be great ! Here is the problem. I have a table MY_TABLE wich contains a list of another tables, and I would like to keep only the nonempty tables and those that their names start by a particular string.
The query I wrote is the following :
select TABLE_NAME
from MY_TABLE
where TABLE_NAME like '%myString%'
and (select count(*) from TABLE_NAME where rownum=1)<>0
order by TABLE_NAME;`
The problem comes from the second SELECT, but I don't know how can I do to use the TABLE_NAME value.
Does someone have an idea ?
Thanks.
[Added from comments]
Actually, I need to test the V$ views contained in the ALL_CATALOG table. But if I can find another table where all these views are contained too and with a NUM_ROWS column too, it would be perfect !
Standard versions of SQL do not allow you to replace 'structural elements' of the query, such as table name or column name, with variable values or place-holders.
There are a few ways to approach this.
Generate a separate SQL statement for each table name listed in MY_TABLE, and execute each in turn. Brute force, but effective.
Interrogate the system catalog directly.
Investigate whether there are JDBC metadata operations that allow you to find out about the number of rows in a table without being tied to the system catalog of the specific DBMS you are using.
Can you use oracle view USER_TABLES? then query will be much easier
select TABLE_NAME
from USER_TABLES
where TABLE_NAME like '%myString%'
and Num_ROWS > 0
order by TABLE_NAME;`

What is Mysql select statement to show a list of tables?

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.