I have been trying to get the number of constraints associated with a column using SQL command.
I would like to know if there exists something like
column.number_of_constraints
How can I get this information?
I'm working with an Oracle database.
The answer is in regard to Oracle database. Thanks
ALL_CONS_COLUMNS describes columns that are accessible to the current user and that are specified in constraints.
https://docs.oracle.com/cd/B28359_01/server.111/b28320/statviews_1042.htm
You need to query the data dictionary to see the table columns and the corresponding constraints.
You can try below query:
SELECT *
FROM user_cons_columns
WHERE table_name = '<your table name>';
Related
Is there a query I can write to search all the column names for a particular database in Netezza?
Within the same database you can use the following query:
select *
from _v_odbc_columns1
where column_name like '%columnname%'
or a less Netezza specific query
select *
from information_schema.columns
where column_name like '%columnname%'
The important catalog views in netezza system are listed below
_V_USER: the user view gives information about the users in the netezza system.
_V_TABLE: the table view contains the list of tables created in the netezza performance system.
_V_RELATION_COLUMN: the relation column system catalog view contains the columns available in a table.
_V_TABLE_INDEX: this system catalog contains the information about the
indexes created on table. netezza does not support creating indexes on a table as of now.
_V_OBJECTS: lists the different objects like tables, view, functions etc. available in the netezza.
Example:
SELECT *
FROM _V_RELATION_COLUMN
WHERE
ATTNAME like '%GEO%' --SPECIFY COLUMN NAME
AND type = 'TABLE'
You would access something similar to an information_schema.
Column Name, %COW%', would use % as a wildcard...gathering any column that has 'COW' in the name
SELECT *
FROM _V_SYS_COLUMNS
WHERE
COLUMN_NAME like '%COW%'
AND TABLE_SCHEMA = 'DEV'
ORDER BY TABLE_NAME
;
I've got table A with column X. Column X SHOULD exist within table B too, however, the name of table B is unknown to me.
Is there a way of finding table B?
I am using an Oracle database and SQL Developer. I cannot see anything of help in the contraints/dependencies sections of table A. I suspect the relationship between column X and table B is taken care of by the application interacting with the database.
Try with the following:
select *
from dba_tab_columns
where column_name = 'COLUMN_X'
and table_name != 'TABLE_A'
You could even study all procedures, package, triggers, etc using your column, to understand the way they manipulate the data in your column; to find these objects, try:
select NAME, TYPE, OWNER
from dba_source
where upper(text) like '%COLUMN_X%'
DBA_TAB_COLUMNS Table describes columns of all tables,view and clusters in the database. Refer Oracle Documentation. Both DBA_TAB_COLUMNS and ALL_TAB_COLUMNS provide similar information while ALL_TAB_COLUMNS provide info as accessible to the current user
Refer Oracle Documentation -
https://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_4146.htm#REFRN23277
https://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2094.htm#I1020277
Is there a query I can write to search all the column names for a particular database in Netezza?
Within the same database you can use the following query:
select *
from _v_odbc_columns1
where column_name like '%columnname%'
or a less Netezza specific query
select *
from information_schema.columns
where column_name like '%columnname%'
The important catalog views in netezza system are listed below
_V_USER: the user view gives information about the users in the netezza system.
_V_TABLE: the table view contains the list of tables created in the netezza performance system.
_V_RELATION_COLUMN: the relation column system catalog view contains the columns available in a table.
_V_TABLE_INDEX: this system catalog contains the information about the
indexes created on table. netezza does not support creating indexes on a table as of now.
_V_OBJECTS: lists the different objects like tables, view, functions etc. available in the netezza.
Example:
SELECT *
FROM _V_RELATION_COLUMN
WHERE
ATTNAME like '%GEO%' --SPECIFY COLUMN NAME
AND type = 'TABLE'
You would access something similar to an information_schema.
Column Name, %COW%', would use % as a wildcard...gathering any column that has 'COW' in the name
SELECT *
FROM _V_SYS_COLUMNS
WHERE
COLUMN_NAME like '%COW%'
AND TABLE_SCHEMA = 'DEV'
ORDER BY TABLE_NAME
;
I have a column name "CustomerIDClass" and I need to find the table it's associated with within an entire Oracle database.
I've run this to determine the owner and name of the table where this column name appears:
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CustomerIDClass%';
and I'm getting this response:
I don't have enough reputation to post the image, so here's the link: http://i.imgur.com/a7rcKoA.png
I have no idea how to access this (BIN$Csew==) table. When I try to use it as a table name I get errors or messages saying that no rows were returned.
My main goal here is to write a simple statement that lets me search the database for the "CustomerIDClass" and view the table that contains this column name.
This table is in the recycle bin. You have to issue FLASHBACK TABLE "Customer1"."BIN$Csew==$0" TO BEFORE DROP command, given you have the appropriate privileges.
Doc: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9012.htm
Do note that in oracle the column names are stored in capital but you are using mixed case in your like statement therefore the select clause will not return any result
Try the below
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CUSTOMERIDCLASS%';
I am doing some analysis work with some old code that uses Oracle database (11g). I've got a code Table and would like to find all the tables, Triggers, etc that calls/uses this table (like foreign key reference). I use Oracle SQL Developer currently and not sure if I can write a query to find this. Is there a way that I can find all references to my current table in the same Schema? Thanks in advance...
You can use the data dictionaries to do this, but which data dictionary to use depends on the type of object in question. Because you want to find several different types of objects, you'll need to use several dictionaries, and, if you want the result all in one query, you will have to connect a few queries together via unions.
The following query (2 queries connected into one by a union) will show you tables with keys referencing table xyz in schema xyz, and also the names of triggers using table xyz in schema xyz (replace table and schema name with whatever you're searching on:
select 'Table has key referencing this table' as match_type, table_name
from all_constraints
where constraint_type = 'R'
and r_constraint_name in
(select constraint_name
from all_constraints
where constraint_type in ('P', 'U')
and table_name = 'TABLE_XYZ'
and owner = 'SCHEMA_XYZ')
union all
select distinct 'Table is used by this trigger', trigger_name
from all_triggers
where table_name = 'TABLE_XYZ'
and owner = 'SCHEMA_XYZ'
For other types of objects, use the same method with the next appropriate dictionary, and connect by a union. You will find this helpful:
http://www.oracle.com/pls/tahiti/tahiti.catalog_views
Note that I use the all_ dictionaries in the query above. Each has an equivalent dba_ and user_ dictionary. The all_ ones act as a dictionary for all objects to which the currently logged in user (you) has access. The dba_ one acts as a dictionary for all objects in the database, but you need to have the privilege to use the dba_ dictionaries, so I didn't use them in the above query (if you do have access, replace all_ with dba_). The user_ dictionaries act as a dictionary for all objects owned by the current user/schema.
On top of the tables and triggers, you may also want to check if there are any code objects referencing your table. ie. procedures, packages.
So you can add a third query:
select NAME,TYPE from all_dependencies where REFERENCED_OWNER='&your_table_owner' and REFERENCED_NAME='&your_table_name';