Find all views and tables that depend on an Oracle database table - database

Hi I have a database with a couple of schemas one of which has a Table TABLE_NAME. I need to make a change to the table and to do that I could do with knowing which tables and views in all the different schemas depend on it.
Its an oracle database, is there some SQL I can run that would help?
I've seen a couple of posts that suggest:
select OWNER, Name
from dba_dependencies
where type = 'VIEW'
and REFERENCED_TYPE = 'TABLE'
AND REFERENCED_NAME IN ('table_name', 'TABLE_NAME');
and
select *
from all_constraints
where r_constraint_name in (select constraint_name
from all_constraints
where table_name='TABLE_NAME')
Neither of which appear to be working, the first gives me a "table or view does not exist" on dba_dependencies and the second returns no results.

Related

Snowflake ACCOUNT_USAGE.TABLES doesn't show me transient table details

I use the query below to get the information of transient tables on my whole account and unable to fetch any result for that.
There are two transients table already created and I can access them. All are done from the highest role accountadmin only.
select * from "SNOWFLAKE"."ACCOUNT_USAGE"."TABLES" WHERE TABLE_TYPE <> 'BASE TABLE';
Gives me empty.
If I do with use DATABASE TRANSIENT_DB;
show tables history;
I get both transient and temporary table I created, I would like to like all the tables that are transients/temporary across the account and not under database alone. Any help on this will be appreciated.
as per Snowflake there is a latency From 45 minutes to 3 hours (varies by view) for the information to be available in the Schema.
You can test this by yourself and will see the objects are not avialbe as sson as you create but are available in information_schema
https://docs.snowflake.com/en/sql-reference/account-usage.html
create table Nor_tab(fld1 varchar2(100));
create transient table tran_tab(fld1 varchar2(100));
select * from snowflake.account_usage.tables where table_name like 'NOR%';
select * from information_schema.tables where table_name like 'NOR%';

Sybase databases, schemas, users and tables

Can someone explain the difference in semantics between the following:
select count(*) from dbo.SomeTable;
select count(*) from SomeTable;
select count(*) from ..SomeTable;
select count(*) from somedb..SomeTable;
select count(*) from somedb.dbo.SomeTable;
They all seem to yield the same results in my system.
More specifically I have this theory that somedb..SomeTable actually means:
the object SomeTable owned by the user I am currently connected as
in the database somedb.
Is this correct?
If so, given then that usernames appear to serve to partition table names into different namespaces, can't it be said that Sybase conflates the concept of a user to that of a schema? (since schemas are what other RDBMSs use to namespace tables?)

Find dependencies between tables in sql database

I have a Sql database with data. I have been asked to populate a fresh identical database with all the required master data so that the application is able to up and run for a new customer.
First approach
Delete all the data from database, run the application, sure i won't be even able to login. Observe errors, identify tables which need master data(sure User table at least), insert data. Then assume i am going to access a module. But without some master data it'll give me errors. Observe errors, identify tables which need master data, insert data.
But this seems not practical.
Second approach
While keeping the data in database, take one table at a time, using queries or sql server management studio tools, find all dependent tables. Keep the parent table data and delete child table data. Do this for all tables. In second round consider the remaining parent tables. Some table's data are inserted from application. Identify those and delete them. This way i can have relevant master data at the end. But i don't know how to approach this.
All these are my thoughts. Sure there might be many more approaches which are more precise and easier than these.I am confused with what to do. Please guide me. Thanks!
Here's a few queries you could use to figure out which table and column is referencing which table and column...
select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE
select * from INFORMATION_SCHEMA.columns
select * from INFORMATION_SCHEMA.tables
select * from sys.foreign_keys
select * from sys.foreign_key_columns
select * from [sys].[objects] where [name] = 'your_tablename'
For more, open Object Explorer (View Menu) and expand:
Databases/System Databases/Master/Views/System Views.
Also, check out any database diagrams there might be in Object Explorer:
Databases/Your_db_name/Database Diagrams.
How big is the database ?
No matter what you have to make proper documentation ?So better start with documentation.
You have to list all table one by one and identity if it is master table.
Remember the diff. between Delete or Truncate.
While doucmenting above query will come in handy.
Save the query and document for future need.
Most importantly,there should not be any error,even if any of the table is empty.
To find foreign key dependecies between tables you can use
SELECT FKT.name 'Parent table', CHT.name 'Child table' FROM sys.foreign_keys FK
JOIN sys.tables CHT ON FK.parent_object_id = CHT.object_id
JOIN sys.tables FKT ON FK.referenced_object_id = FKT.object_id
There is also ways to find dependencies in database views using system views.

how to select values from two tables

I have two tables arts and artsdetails.. artsId is the primary key in arts table foreign key in artdetails table. I am selecting values from both table but my query is giving error as
"Invalid object name 'artdetails'"
My query is:
SELECT arts.artsId, artdetails.mainImage
FROM artdetails
INNER JOIN arts ON artdetails.artsId = arts.artsId;
Please help.
You're probably not running the query in the database where these tables live.
If you're using SQL Server Management studio, look in the top left for a drop down containing the database names. It probably says 'master' (as that's the default). Select the one containing the tables you're using and re-run your query.
Failing that, check they're both running in the same schema as Tom suggests.
You can fully quality table names in your query like this:
SELECT a.artsId, ads.mainImage
FROM [DBNAME].[SCHEMA].artdetails ad
INNER JOIN [DBNAME].[SCHEMA].arts a ON ad.artsId = a.artsId;
Also using table abbreviations tidies things up a bit.

sql server: looking for table usage through out database

How would I figure out what type of sql code such as procs, functions, views etc. are interacting with my table called TABLE1 through out a given database. Sample code would be very helpful for me.
thanks
select so.name, so.xtype
from sysobjects so (nolock)
inner join syscomments sc (nolock) on sc.id = so.id
where sc.text like '%tablename%'
This code will search all SQL Server objects for a reference to your table. You have to run this query for each database.
If a stored procedure uses your table it will appear in this query. The same is true of functions, views, and triggers.
xtype tells you the type of object.
Here are the possible xtype values:
D = Field names
F = Foreign Key
FN = Function
P = Stored Procedures
PK = Primary Key
S = System Tables
U = User tables
V = Hidden tables
Not enough info in your question, but one thing you can do is use SQL Profiler to profile where INSERTs, UPDATEs, and DELETEs are coming from.
I assume you are talking about how an app is interacting with data and what name (of say a sproc) is doing the insert / update / delete.
Look at SQL Profiler, it comes with your client tools install. Filter it to only show connections to your database (either db name or ID).
If you've been good and created your SPs/views/functions after your table was created, sp_depends will tell you evertyhing referencing the table. Exept for dynamic sql that is.

Resources