Find foreign key details
Hi,
i want to have the details like reference tablename,reference volumn name of foreign key for a particular table.
SELECT a.column_name,
c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
WHERE c.constraint_type = 'R'
AND a.table_name = :TableNameUsing this query I am getting everything except the reference column name.
Can somebody please help?
Thanx
You need to add another join to ALL_CONS_COLUMNS:
SELECT a.column_name,
c_pk.table_name r_table_name, c_pk.constraint_name r_pk,
c_pk_c.column_name
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
JOIN all_cons_columns c_pk_c ON c_pk_c.owner = c_pk.owner
AND c_pk_c.constraint_name = c_pk.constraint_name
WHERE c.constraint_type = 'R'
AND a.table_name = :TableName
Similar Messages
-
How to find foreign key in pl/sql
Error starting at line 1 in command:
DELETE FROM ZKET_SOLE WHERE IID_SOLA = 1
Error report:
SQL Error: ORA-02292: integrity constraint (IRCKIS.ZKETSTIP_ZKET_FK) violated - child record found
02292. 00000 - "integrity constraint (%s.%s) violated - child record found"
*Cause: attempted to delete a parent key value that had a foreign
dependency.
*Action: delete dependencies first then parent or disable constraint.
i need to find foreign key but i do not know how. I know that it must be in dictionary but i can not find it. I do not have access to ER model.
ThxYou could use a query similar to this:
SELECT ucc.constraint_name,ucc.table_name,ucc.column_name
FROM user_cons_columns ucc
JOIN user_constraints uc on uc.constraint_name=ucc.constraint_name
WHERE CONSTRAINT_TYPE='R'You could also use the ALL_CONSTRAINTS / ALL_CONS_COLUMNS views.
Hope this helps! -
Hai all,
Can I get all the details of foreign key used in a database..is it possible ?let it be MS Access ,SQL,DB2
Kindly clarify..See the javadocs for DatabaseMetaData class under java.sql
-
Find foreign key which are able or not
Same as the topic ~ how can I find all the able/unable foreign key/contraints inside a database ? Using TSQL
Hello,
Please try the scripts provided on the following resources:
http://blog.sqlauthority.com/2009/07/17/sql-server-two-methods-to-retrieve-list-of-primary-keys-and-foreign-keys-of-database/
http://blog.sqlauthority.com/2006/11/01/sql-server-query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/
http://blog.sqlauthority.com/2007/09/16/sql-server-2005-list-all-the-constraint-of-database-find-primary-key-and-foreign-key-constraint-in-database/
Hope this helps.
Regards,
Alberto Morillo
SQLCoffee.com -
hi,
anyone got sql that would tell which tables reference a primary key in a another table e.g. the link from primary key to foreign key in a schema?
thanksquery DBA_CONSTRAINTS
SQL> desc dba_constraints
Name Null? Type
OWNER NOT NULL VARCHAR2(30)
CONSTRAINT_NAME NOT NULL VARCHAR2(30)
CONSTRAINT_TYPE VARCHAR2(1)
TABLE_NAME NOT NULL VARCHAR2(30)
SEARCH_CONDITION LONG
R_OWNER VARCHAR2(30)
R_CONSTRAINT_NAME VARCHAR2(30)
DELETE_RULE VARCHAR2(9)
STATUS VARCHAR2(8)
DEFERRABLE VARCHAR2(14)
DEFERRED VARCHAR2(9)
VALIDATED VARCHAR2(13)
GENERATED VARCHAR2(14)
BAD VARCHAR2(3)
RELY VARCHAR2(4)
LAST_CHANGE DATE
INDEX_OWNER VARCHAR2(30)
INDEX_NAME VARCHAR2(30)
INVALID VARCHAR2(7)
VIEW_RELATED VARCHAR2(14) -
Finding foreign keys of a child table,grandchild and all dependendents
Hi All,
I just came up with this sceanrio
I have to delete a record in a parent table with a condition provided will have to find all the child tables depends on it and should recursively find all the dependents of the child table.
How to achieve this.Please help with a query or guide.Thanks in advanceThanks visakh it gave me an idea and this script helped me
DECLARE @mytable VARCHAR(30) SET @mytable = 'test'
DECLARE @RecordID VARCHAR(8)
SET @RecordID = '00000001'
DECLARE @Order INT SET @Order = 0
DECLARE @Count INT
declare @tblname varchar(100),@query NVARCHAR(max)
if OBJECT_ID('tempdb..#OrderList','U') is not null
DROP TABLE #OrderList
CREATE TABLE #TEMP
OrderNo INT,
TableName VARCHAR(50)
CREATE TABLE #OrderList
RecordNo INT,
OrderNo INT,
TableName VARCHAR(50)
INSERT INTO #TEMP
VALUES ( @Order,
@mytable )
WHILE ( EXISTS(SELECT TableName
FROM #TEMP
WHERE OrderNo = @Order) )
BEGIN
SET @Order= @Order + 1;
INSERT INTO #TEMP
SELECT @Order,
t1.TABLE_NAME AS PointsFrom
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS r
JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t1
ON t1.CONSTRAINT_NAME = r.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t2
ON t2.CONSTRAINT_NAME = r.UNIQUE_CONSTRAINT_NAME
WHERE t2.table_name IN (SELECT TableName
FROM #TEMP
WHERE OrderNo = ( @Order - 1 ))
END
INSERT INTO #OrderList
SELECT Row_number() OVER (ORDER BY OrderNo, TableName),
#TEMP.OrderNo,
#TEMP.TableName
FROM #TEMP
DROP TABLE #TEMP
--SELECT * FROM #OrderList
SET @Count = (SELECT Max(RecordNo)
FROM #OrderList)
WHILE (@Count <> 0 )
BEGIN
select @tblname = TableName FROM #OrderList WHERE RecordNo = @Count
select @query = 'DELETE from '+ @tblname
select @query
exec sp_executesql @query
SET @Count = @Count-1
END -
Hi All,
Let's picture that I have a GW service called Search Helps and within that service I have a PersonalAreas Entity Set which acts as a search help for drop down fields effectively that need to select a Personal Area. I have 3 properties, with Personal Area Id and Company Code being the keys, and the text for Personal Area being the 3rd and last property. You really need to filter PersonalAreas by Company Code otherwise it's a fairly useless result set.
Now within a different GW service, I have an entity called Position which has a single key of PositionId. There is also a a property of company code on this Position.
Now rather than asking the UI developer to get the Company code value and filter PersonalAreas with it directly, I thought it would be nicer for the UI developer just to use the navigation from a specific Position to PossiblePersonalAreas. e.g. Positions('123')/PossiblePersonalAreas returns the entity set of PersonalAreas filtered by the Position's Company Code.
So with that in mind, I add the external model reference pointing at the search helps service; then create an external association via the sneaky External Associations Editor button (that doesn't appear on the Wizard just to confuse you), and set up my Dependent entity to point at the Search Helps PersonalAreas entity.
At this point I really want to tie the Position's Company Code to the PersonalAreas Company Code, but all I can do is set up a referential constraint of PositionId to CompanyCode or have no referential constraint at all (note - in my example I added the referential constraint as there is an issue with navigation keys being provided to external associations from what I can tell - more about this below).
So, without any other option, I go into my GetEntitySet method of PersonalAreas and write something like the following:
CASE iv_source_name.
WHEN 'Position'.
READ TABLE it_filter_select_options ASSIGNING <filter> INDEX 1.
CHECK sy-subrc = 0.
READ TABLE <filter>-select_options assigning <select_option> INDEX 1.
CHECK sy-subrc = 0.
position_id = <select_option>-low.
* Get company code for position
TRY.
CREATE OBJECT o_position
EXPORTING
i_position_id = position_id.
o_position->get_cost_centre_information(
IMPORTING
e_company_code = company_code
CATCH zcx_hr_object.
ENDTRY.
endcase.
* Continue with retrieving PersonalAreas for given Company Code
<Remaining code not shown>
e.g. I have to assume CompanyCode is actually "Position Id" when the source of this call is from Position (I could also look at the navigation to find this has come from Position but if you're wondering, unfortunately for external associations, the key is not included in this for some reason but the same followup logic of reloading the object would be required).
So the question I'm hoping the answer to is Yes is: Am I missing something in my understanding here?
Thanks,
Matt
Edit since posting and playing around a bit more: Thinking about this further - it's unfortunate, but the navigation property in odata really implies the dependent entity has knowledge of the principal entity and realistically must know how to instantiate it if it needs to get data based on one of the properties.
Now while I'm fine with that (sort of) I think it's wrong that the dependent entity needs to have the required key properties to hold the principal entities as it makes something like a generic search help impossible to do with navigation - Maybe I just need to be done with that and expect filters to be used by UI programmers to find foreign key'ed entities....Hi Matt,
I'm not surprised that you have an issue with this. One bugbear I have with Gateway is that for some reason the designers don't think any context beyond the immediate preceding navigation node is important. They don't support this within the same service,so it's hardly likley to work in a service reference.
I may be wrong but I don't think GW is OData compliant in this respect. If I have the navigation path /blindservice/FromHereSet(1)/ToAPoints(22)/ToBPoints, it's rather restrictive to say I cannot see the initial key of '1' in the path when I am trying to resolve 'ToBPoints'. If that value - where I started - is key to the context, I have to know it.
Currently the only way to access this context is to embed the 'key history' in the intermediate entities, i.e. /blindservice/FromHereSet(1)/ToAPoints(1,22)/ToBPoints. In my view that is corrupting the URl to suit the limitations of the SAP OData implementation. What's even more confusing/frustrating is that the technical request context appears to be able to store the stacked navigation keys but doesn't make use of this design.
I feel your pain
Ron. -
ADF-Master detail-Foreign key generated By Trigger won't propaget to Detail
Hi All,
We have Master-Detail Table where Master table key is generated by DB.
And this key act as Foreign key in detail table.When we Creates Master Table records and commit it .It gets saved correctly But when we navigate to Create Detail record (Cretae button is taken from Detail) It opens in Create mode but does n't poppulate Foreign Key.
Can anybody plz explain..
Regards
YogeshHi Frank,
Thanks for your reply.I don't know "the ADF Business Components tester "
what is this ?
I was able to solve my problem by making "Refresh after insert " Check box selected in Entity object.Now it is generating Master Key and also i am getting it in Detail.
But now problem is, Can we make details record in insert mode by 'Create opration' under Master View.My requirment is insert two records in detail table in background while inserting record in master.Where relationship is maintained by foreign key which generated by trigger.
I want master and detail create form on same page where i will make details from Rendering False i.e both should be in Create mode (Accesser should not retrive record).
In Short
ADF-BC Master Detail - Detail Record insert in Back ground on the same page
Thanks in Adv
RHY -
Foreign key validation while creating master/detail record in document mode
I am creating master/detail records in the document mode. And I am generating the master primary key in the create() method of the master Entity Object. Now I valdate the foreign key in the detail Entity Object's validateEntity() method by doing a query to the master table through some View Object.
But since the master record has not yet been posted (since it hasn't been committed) to the database, this query does not return any record and the validation fails in detail object.
And thus the create fails.
Please let me know if this is the right approach for doing this. If so where I am going wrong? Else please let me know if there are any better approach to do this.
KunalKunal:
Use a code snippet like the following:
--- Assuming you're in the detail EO's create method ---
oracle.jbo.server.EntityDefImpl eDef = oracle.jbo.server.EntityDefImpl.findDefObject(<package-qualified-name-of-the-master-Entity-Object>);
oracle.jbo.Row masterRow = eDef.findByPrimaryKey(this.getDBTransaction(), <foreign-key>);
If the row does not exist, masterRow will be null.
findByPrimaryKey first checks the entity cache (which will include your new master row(s)) and then go out to database (if it's not in cache).
Thanks.
Sung -
Master-Detail Forms (Populating a Foreign Key Automatically)
Master-Detail Form
When inserting a MD Form, the Master ID (e.g. Deptno,the Primary Key) is generated.
How is it possible for this also to be generated in the Detail Form (as the Foreign Key)? The requirement is for the foreign key to be populated automatically.
Any help would be much appreciated.
Thanks.This item is discussed earlier in this group.
It's solved in version 3.0.8
Regards,
Anna -
Master detail without foreign key
Hi.
I would like to know whether it is possible in JDeveloper to have a master-detail form for two tables that do not have a phisical foreign key constraint (they do have a logical one, though)?
I tried creating an Association between the two Entities, but I do not see any effect.This should work without dependency on the database having the relation defined.
Are you using ADF Business Components?
If so can you detail what you did and what is the behavior you see?
Also remember that you'll need to set the link at the view level also. -
Find which tables are linked to this one via foreign keys
Hi, I need to drop & recreate a table, but this table is referenced by other tables via foreign key constraints.
1. Is there a query I can run to find those tables (the ones referencing this one) ?
2. What is the standard way to drop & recreate this table being referenced by others?1) You can try the following query:
select r.owner, r.table_name
from user_constraints r, user_constraints o
where r.r_owner = o.owner and r.r_constraint_name = o.constraint_name
and o.constraint_type in ('P','U') and r.constraint_type = 'R'
and o.table_name = 'DEPT'
2) To drop tables with references you can also use drop table cascade constraints,
which will also drop constraints from tables:
[email protected]> drop table dept;
drop table dept
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys
[email protected]> drop table dept cascade constraints;
Table dropped.
Another way is to prepare a script which will remove the tables in proper order.
Best Regards
Krystian Zieja / mob -
How can I have multiple instances of a column that itself holds a foreign key reference?
I am new to Visual Studio so to start learning it I first of all downloaded a sample available at https://code.msdn.microsoft.com/ADPNET-Entity-Framework-2d1160cb and started working around it. Since I have fairly good knowledge of VB6 and SQL it did
not take much time for me to understand the whole pattern the sample is based on. Had Microsoft given a detail explanation or a walk through of the sample it would have been much easier to understand the basics. However, I somehow managed to work around it
and have build a small desktop application in wpf using Entity Framework and MVVM. But a point has come where I have got completely stuck up finding no way out. The problem is as under:
I have two tables. 1 Advocate and 2 Party. Table Advocate would contain names of advocates and would have a primary key. Similarly Party would have names and their respective primary keys.
Then I have another two tables 1. Case and 2 CaseDetail. Table Case would simply hold three columns: 1. CaseId 2. CaseNo and 3. Year. Table CaseDetail would have CaseDetailId as a primary key the CaseId as a Foreign Key. Now what I need is that a particular
case could have multiple advocates and multiple petitioners. So the table CaseDetail would have two columns to hold advocateId and PartyId as a Foreign Keys.
If you look at the sample referred above you would not find how to deal with such a case. When I follow the pattern of the sample I get host of design time and runtime errors.
Therefore, please suggest what strategy should be adopted in a scenario described above while developing WPF application using Entity Framework and MVVM.I am new to Visual Studio so to start learning it I first of all downloaded a sample available at https://code.msdn.microsoft.com/ADPNET-Entity-Framework-2d1160cb and started working around it. Since I have fairly good knowledge of VB6 and SQL it did not
take much time for me to understand the whole pattern the sample is based on. Had Microsoft given a detail explanation or a walk through of the sample it would have been much easier to understand the basics. However, I somehow managed to work around it and
have build a small desktop application in wpf using Entity Framework and MVVM. But a point has come where I have got completely stuck up finding no way out. The problem is as under:
I have two tables. 1 Advocate and 2 Party. Table Advocate would contain names of advocates and would have a primary key. Similarly Party would have names and their respective primary keys.
Then I have another two tables 1. Case and 2 CaseDetail. Table Case would simply hold three columns: 1. CaseId 2. CaseNo and 3. Year. Table CaseDetail would have CaseDetailId as a primary key the CaseId as a Foreign Key. Now what I need is that a particular
case could have multiple advocates and multiple petitioners. So the table CaseDetail would have two columns to hold advocateId and PartyId as a Foreign Keys.
If you look at the sample referred above you would not find how to deal with such a case. When I follow the pattern of the sample I get host of design time and runtime errors.
Therefore, please suggest what strategy should be adopted in a scenario described above while developing WPF application using Entity Framework and MVVM. -
How to change a massive amount of foreign keys?
Hi to all!
I'm a newbie here, and I hope I'll find a solution to my problem...
Well, I have to create a copy of row within the same table, with same values except of primary key. That part is ok. Then, I have to change values of that primary key (from old value, to new value) in all foreign keys which references to that column/primary key in parent table. Let's say that isn't problem too, using system tables and informations about constraints and indexes...
But, tricky part comes. Some of those foreign keys are primary keys for their tables, and there are other foreign keys which references to these primary keys, including a column which is primary key in very first (parent) table. And also, that line of primary key-foreign key can go on...
I don't know how deep to check for those keys. Any help with some piece of code (in PL/SQL)? Also, how to start to change value of that, particular column, in line of primary key-foreign key?Sounds like you need to add a copy function to your application. This will allow you to create a copy of an existing import/export but each will be unique. This would provide you with the ability to change the import/export and retain the old and new details.
A small example using a header and detail table;
select * from orders;
ORDER_ID ORDER_DATE
1 27/05/2010
select * from order_lines;
LINE_ID ORDER_ID
1 1
2 1
3 1A function that makes a copy of a certain order;
create or replace function copy_order(p_order_id orders.order_id%type)
return number is
l_new_order_id orders.order_id%type;
begin
select orders_seq.nextval
into l_new_order_id
from dual;
insert into orders
(order_id, order_date)
select l_new_order_id, sysdate
from orders
where order_id = p_order_id;
insert into order_lines
(line_id, order_id)
select order_lines_seq.nextval, l_new_order_id
from order_lines
where order_id = p_order_id;
return l_new_order_id;
end;
Function createdCall the function with order_id 1;
var n number
exec :n := copy_order(1);
PL/SQL procedure successfully completed
print n
n
2There are now 2 similar orders;
select * from orders;
ORDER_ID ORDER_DATE
1 27/05/2010
2 28/05/2010
select * from order_lines;
LINE_ID ORDER_ID
1 1
2 1
3 1
4 2
5 2
6 2
6 rows selectedThe first order can be canceled (using a status column or similar) and the second order can be updated, ie add additional lines, etc. or left as is. -
Can I create a foreign key when creating Business Components from Tables
Hi,
I would like to use ADF and JSF to create an application on our database. My problem is our database does not "conform". i.e. there are no foreign key constraints.
When I create a "Business Component from Tables" view, can I put in my own joins between tables so enabling the use of the generated views?
Jon L.View object instances show up in the data control palette once you've added them to your application module's data model.
See these sections in the ADF Developer's Guide for more information:
Section 2.6.3, "Using View Objects in the Application Module's Data Model" talks about using view object instance in the data model as part of the overview chapter.
Section 4.5, "Understanding the Active Data Model" explains a little more about this
Section 5.3, "Using a View Object in an Application Module's Data Model" shows how to do this.
Chapter 10, "Overview of Application Module Data Binding" gives details on how the application and its view objects are exposed in the Data Control palette.
You can find the guide on the ADF Learning Center on OTN.
http://www.oracle.com/technology/products/adf/learnadf.html
Look for the ADF Developer's Guide for Forms/4GL Developers.
Maybe you are looking for
-
Win 8.1 and HP wireless printer
I can print using USB cable but it will not recognize it as a wireless device HELP
-
Change my IP addres in Exchange 2007
Last week we changed Internet provider in my company, I had a problem because I received various mails and It's says my IP address public was inside in BLOCKLIST1. My Internet provider will give me another IP address public for change in my Exchange
-
hi, i am making report in alv in which i have to display the sub totals and den full total. is it possible to display sub total in ALV acccording to material type wise or itemid wise. plzz help me with some example. If find useful he or she will defi
-
Sample management with in-process inspection
i need to know why must we assign inspection points when we use sample management in an in-process inspection ?
-
How to deploy webcenter application
Hi, How to deploy webcenter application? I mean not in the Integrated Server provided by Jdev. Can anyone specify the steps whcih I can follow?