How we relate two tables using foreign key(fk)?

hi to all,
    what are the conditions has to follow to relate two tables.I.e.,
the two tables have same primary keys(pk). if we relate these two tables in one table the pk and fk will be the same then how that table in active.

Hi
To relate two tables..we have foreign key relationship.
In one table v have primary key and in the second table, the same key is foreign key for that table..
To relate two tables, we can use JOINS
If there is already a suitable foreign key between two tables used in the view, these tables can be linked with a join condition from this foreign key.
Create a view on tables TAB1 and TAB2. TAB1 is the primary table of the view. TAB2 is the secondary table of the view. TAB1 is the check table for TAB2. The foreign key fields are assigned to the check table fields as follows:
TAB1-FIELD_A assigned to TAB2-FIELD_1
TAB1-FIELD_A assigned to TAB2-FIELD_1
The join condition of the view generated from the foreign key is then:
CREATE VIEW ... AS SELECT ... WHERE TAB2-FIELD_1 = TAB1-FIELD_A AND TAB2-FIELD_2 = TAB1-FIELD_B.
Join conditions can also be copied from generic and constant foreign keys. If a constant is assigned to a field in the foreign key, it is also assigned to the field in the join condition. There is no join condition for a generic relationship in the foreign key.
The foreign key between tables TAB1 (check table) and TAB2 (foreign key table) is defined as follows:
TAB1-FIELD_A assigned to TAB2-FIELD_1
TAB1-FIELD_B generic
TAB1-FIELD_C assigned to constant ‘C’
The join condition for the view generated from the foreign key is in this case:
CREATE VIEW ... AS SELECT ... WHERE TAB2-FIELD_1 = TAB1-FIELD_A AND TAB2-FIELD_2 = ‘C’.
Hope it helps
Reward if useful.

Similar Messages

  • How to fetch column data using foreign key in adf table ?

    I have created a adf table using a view that has a group id and user id column. I want to display the user name ( from user table) and group name from group table. I had created view links to the corresponding tables from the user_group view ( whose iterator is used to render the table).
    It sort of works. I dragged the user name from the embedded user iterator in user_group iterator. It does find the correct user name but if I have more than one row, it displays the user name of the last row user in all rows !
    Is there a solution to this problem ?

    Well, I saw in the view query for the association view ( for Many-Many) that the SQL already had the joins with the two ( master) tables to the association table. So I went ahead and added the descriptive columns to the select clause. Then the selected columns showed up in the attribute list by themselves ( without my intervention). So far so good.
    Then I recreated the adf table attribute by dragging these newly selected attribute to the page. I still see that the value of the attribute is the same in all the rows - this time it is the value for the FIRST row. It doesn't correspond to the foreign key for that particular row.
    IS THIS A BUG ? Has anyone else ever done this ?

  • Binding for table produces list for other tables using foreign key and crea

    Using
    software Jdev 11G, WLS 11G, Oracle DB 11G, Windows Vista platform
    technology EJB 3.0, jspx, backing beans, session bean
    I cannot create a namedquery on my secondary table. The method for the column uses the entity object rather than the name and value of the column.
    For instance,
    (Coketruck) table has inventory records(Products) table
    Coketruck has one to many to the Products table
    Products has a many to one to the Coketruck
    I need to return the products from the product table based on the CokeTruck but I cannot create a namedQuery because the method in the Product table is an entity object type instead of a long that I can use to look up all the products based off the column truck_id.
    This is what I was expecting…
    Private Long truckId;
    public Long getTruckId() {
    return truckId;
    public void setTruckId (Long truckId) {
    this. truckId = truckId;
    Instead this is what I have…
    @ManyToOne
    @JoinColumn(name = "TRUCK_ID")
    private Coketruck coketruck;
    this. coketruck = coketruck
    public Coketruck getCoketruck() {
    return coketruck;
    public void set Coketruck (Coketruck coketruck) {
    this. coketruck = coketruck;
    How do I do a query on the Product table to return all the products that are in the coketruck?
    If I do the following it expects for me to pass the Entity Object which I cannot use as search criteria for my find method.
    @NamedQuery(name = "Products.findById", query = "select o from Products o where o.truckId = :truckId")
    On a different note but the same song…
    I noticed that when I look at my Session Bean Data Contols that the coketruck already has a list of the products. I have created a jsp page with a backing bean and have been able to use the namedquery on the coketruck entity to retrieve the productList. Unfortunately I need to sort the products by type and was also not able to find where to perform the work to be able to iterate through the productList to get my desired display. Therefore I started looking at doing another namedquery that would only retrieve the product_type ordering by the truckId.
    Seems I have come full circle… I don’t care what method I have to use to get the info back.
    Any help is greatly appreciated!

    user9005175 wrote:
    Hi!
    I work on an application wich uses a shopping cart stored in a database. The shopping cart uses two tables:
    CART: Holds information common for one shopping cart: the user it is connected to etc.
    - Primary key: CART_ID
    CART_ROW: One row in the cart, e.g. one new product to buy.
    - Primary key: ROW_ID
    - Foreign key: CART_ROW.CART_ID references CART.CART_ID
    From the code the rows in the cart are collected per cart, as is modelled by the foreign key. There exists one more relationship, which we use in the code, but which is not modelled by a foreign key in the database. One row can be dependent on another row, which makes the other row a parent.
    CART_ROW has a column PARENT_ID which references CART_ROW.ROW_ID.
    Should we add a foreign key for PARENT_ID? Or are there any questions to consider when it is a foreign key to the same table?
    I suggest to add foreign key it wont harm the performance (except while on insert when there would be validation for the foreign key). But it would prevent users to insert wrong/corrupt data either through code or directly by loggin in the database.
    A while ago we added indexes, both on ROW_ID and on PARENT_ID. Could the index on PARENT_ID have been harmful, since there is no foreign key?
    Index on parent_id would only be harmful if you do not make use of index after creating it (i.e. there is no query which make use of this index).
    And if you decide to have a foreign key on parent_id then I suggest to have index too on parent_id as it would be helpful atleast when you delete any record in this table.
    Best regards!

  • How oracle locks child table with foreign keys?

    I have 3 tables. tab1 , tab2,tab3.
    tab2 has FK on tab1.
    tab3 has FK on tab2.All the FKs are indexed.
    One transaction in my code changes tab3.
    One transaction in my code changes tab1,tab2 and tab3.
    I am facing deadlock issues on tab3 when both transactions work concurrently.
    If I lock tab1 with "for update nowait " at the start of both transaction,will the problem be solved?

    Let me elaborate on my problem
    TAB1          
    =====          
    a1 b1 c1     
    a2 b2 c2     
    a3 b3 c3     
    TAB2               
    =====               
    x1 y1 z1 a1          
    x2 y2 z2 a2          
    x3 y3 z3 a3          
    (FK indexed)          
    TAB3
    =====
    p1 q1 r1 y1
    p2 q2 r2 y2
    p3 q3 r3 y3
    (FK indexed)
    Transaction 1 for session1
    select * from TAB1 where col1 = a1 for update nowait;
    doing activities on tab2 and tab3 depending on the foreign key joins in loop.
    Transaction 2 for session 2
    select * from TAB1 where col1 = a2 for update nowait;
    doing activities on tab2 and tab3 depending on the foreign key joins in loop.
    1) Please let me know whether these sessions can head towards deadlock.
    2) If some session try to update TAB3 row of y1 when session 1 is still working , how will system behave?
    will it go for blocked session or go for dead lock or get an exception message?
    Edited by: user9974355 on Dec 17, 2008 11:58 PM
    Edited by: user9974355 on Dec 18, 2008 12:00 AM

  • How to  Join two tables using the Inner Join

    Hi All,
    I have two tables i.e table1 and table2 as i have created two otds and my present requirement is to join this two tables and get the results and using this i need to do some logic and update another table3.
    can some one help me out how to go for the above req.
    Thanks in Advance
    Srikanth

    The best efficient way to use inner join is create two input otds,use there otd's in create a collaboration usinf etl.
    after selecting two input otd's create a inner join statement and map it to out put otd.
    while using the etl the performance of the over all integration is increased 20 time of the normal integration.
    Hopes this will helps,,
    Thanks,
    Papa Rao.

  • How to join two tables using EJB-QL

    Hi There,
    How to join tables using EJB-QL ?
    Thanks.
    Edited by: vamseebobby on Nov 6, 2007 8:12 AM

    You might try
    SELECT b.entity2property FROM Entity1 a JOIN a.entity2 b
    for example, to retrieve players names, from a Players table, that belong to the team 'My Team' in the Teams table
    SELECT b.playerName FROM Teams a JOIN a.players b WHERE a.teamName = 'My Team'

  • How to relate two tables when there is null in join criteria

    Hi,
    I have two tables dept and emp, they both are joined on basis of deptno. few records in emp table have no deptno. i want to join these tables, i want to count those records as well where deptno is null in emp table. Please can anybody help me to get this query.
    Regards,

    Please refer...
    http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm#i2054062
    and search 'Using Outer Joins: Examples'
    http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2066378

  • Find matching records from two tables, useing three key fields, then replace two fields in table1 from table2

    I have two tables - table1 and table2 - that have the exact same schema. There are three fields that can be used to compare the data of the two tables, field1, field2, and field3. When there are matching rows in the two tables (table1.field1/2/3 = table2.field1/2/3)
    I want to replace table1.field4 with table2.field4 and replace table1.field5 with table2.field5.
    I have worked with the query but have come up with goobly goop. I would appreciate any help. Thanks.

    If your field1, field2, and field3 combinations in these tables are unique, you
    can do a join on them.
    Select t1.field4, t2.field4 , t1.field5, t2.field5
    from table1 t1 inner join table2 t2 on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t1.field3=t2.field3
    --You can update your table1 with following code:
    Merge table1 t1
    using table2 t2 on
    on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t3.field3=t2.field3
    When matched then
    Update Set
    t1.field4= t2.field4
    ,t1.field5 = t2.field5 ;

  • How to join two tables using UD Connect ?

    Hi,
    I have 2 tables on which I need to create join & extract the data from Oracle database using UD connect. Is this possible? If yes, please let me know how or suggest an alternative approach.
    Thanks,
    Sunil

    Thanks Tony for the reply..  Actually, the issue is Netweaver Java dictionary doesn't allow you to create a view, right? Also, I dont want to create a view directly on the database.
    I want to handle this on BI side somehow.

  • How to join two tables

    hi
    how to join two tables using inner join  if the first table has two primary keys and second table has 3 primary keys

    Would describe type of joins in ABAP, which might differ with other joins.
    The join syntax represents a recursively nestable join expression. A join expression consists of a left-hand and a right- hand side, which are joined either by means of INNER JOIN or LEFT OUTER JOIN. Depending on the type of join, a join expression can be either an inner (INNER) or an outer (LEFT OUTER) join. Every join expression can be enclosed in round brackets. If a join expression is used, the SELECT command circumvents SAP buffering.
    On the left-hand side, either a single database table, a view dbtab_left, or a join expression join can be specified. On the right-hand side, a single database table or a view dbtab_right as well as join conditions join_cond can be specified after ON. In this way, a maximum of 24 join expressions that join 25 database tables or views with each other can be specified after FROM.
    AS can be used to specify an alternative table name tabalias for each of the specified database table names or for every view. A database table or a view can occur multiple times within a join expression and, in this case, have various alternative names.
    The syntax of the join conditions join_cond is the same as that of the sql_cond conditions after the addition WHERE, with the following differences:
    At least one comparison must be specified after ON.
    Individual comparisons may be joined using AND only.
    All comparisons must contain a column in the database table or the view dbtab_right on the right-hand side as an operand.
    The following additions not be used: NOT, LIKE, IN.
    No sub-queries may be used.
    For outer joins, only equality comparisons (=, EQ) are possible.
    If an outer join occurs after FROM, the join condition of every join expression must contain at least one comparison between columns on the left-hand and the right-hand side.
    In outer joins, all comparisons that contain columns as operands in the database table or the view dbtab_right on the right-hand side must be specified in the corresponding join condition. In the WHERE condition of the same SELECT command, these columns are not allowed as operands.
    Resulting set for inner join
    The inner join joins the columns of every selected line on the left- hand side with the columns of all lines on the right-hand side that jointly fulfil the join_cond condition. A line in the resulting set is created for every such line on the right-hand side. The content of the column on the left-hand side may be duplicated in this case. If none of the lines on the right-hand side fulfils the join_cond condition, no line is created in the resulting set.
    Resulting set for outer join
    The outer join basically creates the same resulting set as the inner join, with the difference that at least one line is created in the resulting set for every selected line on the left-hand side, even if no line on the right-hand side fulfils the join_cond condition. The columns on the right-hand side that do not fulfil the join_cond condition are filled with null values.
    Note
    If the same column name occurs in several database tables in a join expression, they have to be identified in all remaining additions of the SELECT statement by using the column selector ~.
    Example
    Join the columns carrname, connid, fldate of the database tables scarr, spfli and sflight by means of two inner joins. A list is created of the flights from p_cityfr to p_cityto. Alternative names are used for every table.
    PARAMETERS: p_cityfr TYPE spfli-cityfrom,
    p_cityto TYPE spfli-cityto.
    DATA: BEGIN OF wa,
    fldate TYPE sflight-fldate,
    carrname TYPE scarr-carrname,
    connid TYPE spfli-connid,
    END OF wa.
    DATA itab LIKE SORTED TABLE OF wa
    WITH UNIQUE KEY fldate carrname connid.
    SELECT ccarrname pconnid f~fldate
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM ( ( scarr AS c
    INNER JOIN spfli AS p ON pcarrid = ccarrid
    AND p~cityfrom = p_cityfr
    AND p~cityto = p_cityto )
    INNER JOIN sflight AS f ON fcarrid = pcarrid
    AND fconnid = pconnid ).
    LOOP AT itab INTO wa.
    WRITE: / wa-fldate, wa-carrname, wa-connid.
    ENDLOOP.
    Example
    Join the columns carrid, carrname and connid of the database tables scarr and spfli using an outer join. The column connid is set to the null value for all flights that do not fly from p_cityfr. This null value is then converted to the appropriate initial value when it is transferred to the assigned data object. The LOOP returns all airlines that do not fly from p_cityfr.
    PARAMETERS p_cityfr TYPE spfli-cityfrom.
    DATA: BEGIN OF wa,
    carrid TYPE scarr-carrid,
    carrname TYPE scarr-carrname,
    connid TYPE spfli-connid,
    END OF wa,
    itab LIKE SORTED TABLE OF wa
    WITH NON-UNIQUE KEY carrid.
    SELECT scarrid scarrname p~connid
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM scarr AS s
    LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid
    AND p~cityfrom = p_cityfr.
    LOOP AT itab INTO wa.
    IF wa-connid = '0000'.
    WRITE: / wa-carrid, wa-carrname.
    ENDIF.
    ENDLOOP.

  • How to insert record in child table with foreign key

    Hi,
    I am using Jdeveloper 11.1.2.0. I have two master table one child table.
    How to insert and update a record in child table with foreign key ?
    I have created VO based on three EO(one eo is updatable other two eo are references) by using joined query.
    Thanks in Advance
    Edited by: 890233 on Dec 24, 2011 10:40 PM

    ... And here is the example to insert using sequenceimpl by getting the primary key of the master record and insert master and detail together.
    Re: Unable to insert a new row with a sequence generated column id
    -Arun

  • How to update two tables in a single call using JDBC Sender adapter

    Hello All,
    The scenario is, database entries have to be selected from two tables and at the same time those tables have to be udpated with the flag.
    We are using JDBC sender adapter and in Select Query, we are fetching the data by joinin the two tables.
    Update Statemtent: We can only update one table using this statement.
    Is it possible to update two tables using the Update Statement without using Stored Procedures.
    Let me know.
    Regards,
    Sreenivas.

    Hi Sreenivas,
    > Is it possible to update two tables using the Update Statement without using Stored Procedures.
    Yes its possible through join statement
    Check this links
    Update in JDBC Sender adapter for more than one table
    data from 2 tables for jdbc sender adapter
    Regards
    Ramesh

  • Dropdown List in table - multicolumn foreign key

    Hello,
    Steve Muench has a nice screencast how to create a dropdown list in an editable adf table.
    Is there a way how to update this example to work with multicolumn foreign key between edited table and lookup table?
    Let's say lookup table has Code, Line attributes as primary key and edited table has foreign key attributes MasterCode and MasterLine. How populate the FK attributes by dropdown list?
    For editable table it's probably not possible directly from dropdown list. I tried to find a solution but I had problem to identify which lookup row was selected.
    Rado

    ok i have checked that for the past few days but its not what i want.
    i am using wizard to do this and i need to populate the database table records (individual field names) and display in the table control dropdown list.
    what the demo shows is only how to display a dropdown list with reference to the table names.

  • Why can't global temporary tables have foreign keys?

    Hi,
    I have a forms module which shows two blocks. The first block is based on a normal database table EMP and the second one is based on a global temporary table SAL_TEMP. There is a master-detail relation between the two blocks. So far, in Designer I created two normal database tables EMP and SAL_TEMP and created a foreign key with validation level "none" from the table SAL_TEMP to the table EMP. By creating the foreign key, Designer generates code to automatically synchronize the data between the master and the detail block in my form.
    In the latest version of Designer it is finally possible to indicate a table as a global temporary table. But when I try to change my table SAL_TEMP to a global temporary table (session) I get an error that this is not allowed because my table contains foreign keys. So the Designer team finally implemented this feature but it is useless to me.
    Can someone explain to me the reason of this restriction?
    Vincent

    Just out of curiosity - which version of Designer supports the creation of global temp tables? We are running version 9.0.2.80.6 and, as far as I am award, this does not support it (unless I am being really thick!) - is this new 10g Dev. Suite functionality?
    thanks,
    Pete

  • Not use foreign keys.

    I would like to know if is it bad if I don't want to use foreign keys? is it just a constraint? Is it useful? If I wont use delete on cascade for example is it mandatory to use foreign keys? Please help me with this doubt.
    Thanks.

    A Small example:
    DROP TABLE T_TABLES ;
    CREATE TABLE T_TABLES AS
    SELECT ROWNUM AS ID, OWNER, TABLE_NAME
    FROM   ALL_TABLES;
    ALTER TABLE T_TABLES ADD CONSTRAINTS PK_TABLES PRIMARY KEY(ID);
    DROP TABLE T_TABLE_COLUMNS ;
    CREATE TABLE T_TABLE_COLUMNS AS
      SELECT T.ID, TC.OWNER, TC.TABLE_NAME, TC.COLUMN_NAME, TC.DATA_TYPE
      FROM   ALL_TAB_COLS TC
               JOIN T_TABLES T ON (T.OWNER = TC.OWNER AND T.TABLE_NAME = TC.TABLE_NAME)
      WHERE  T.TABLE_NAME NOT IN ('T_TABLES', 'T_TABLE_COLUMNS'); -- EXCEPT THESE TWO TABLES
    EXPLAIN PLAN FOR
    SELECT *
    FROM   T_TABLE_COLUMNS
    WHERE  ID IN (SELECT ID FROM T_TABLES);
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
    | Id  | Operation          | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT   |                 | 27823 |  3586K|    63   (7)| 00:00:01 |
    |   1 |  NESTED LOOPS      |                 | 27823 |  3586K|    63   (7)| 00:00:01 |
    |   2 |   TABLE ACCESS FULL| T_TABLE_COLUMNS | 27823 |  3233K|    60   (2)| 00:00:01 |
    |*  3 |   INDEX UNIQUE SCAN| PK_TABLES       |     1 |    13 |     0   (0)| 00:00:01 |
    --ADD FOREIGN KEY
    ALTER TABLE T_TABLE_COLUMNS ADD CONSTRAINT FK_TABLES FOREIGN KEY(ID) REFERENCES T_TABLES(ID);
    -- SAME QUERY AGAIN
    EXPLAIN PLAN FOR
    SELECT *
    FROM   T_TABLE_COLUMNS
    WHERE  ID IN (SELECT ID FROM T_TABLES);
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
    -- NO JOIN ANYMORE
    | Id  | Operation         | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT  |                 | 27823 |  3233K|    60   (2)| 00:00:01 |
    |*  1 |  TABLE ACCESS FULL| T_TABLE_COLUMNS | 27823 |  3233K|    60   (2)| 00:00:01 |
    -------------------------------------------------------------------------------------FKs can even save you from joins.

Maybe you are looking for

  • Use of open interface tables in R12

    Hello, Can someone tell me the status of using open interface tables in release 12. Is this still a supported mechanism for importing data into the product, or must you use a PL/SQL/Java API, XML gateway etc? iREP for 11.5.10 lists these open interfa

  • Help! Problem Purchasing Music on iTunes iPhone store with Gift Credit

    Hello, i am using iTunes store on my iPhone 4. I redeemed a gift card and that credit money is showing up as available. However, as I try to make a purchase, instead of deducting payment from that redeemed credit, I'm asked to sign in and my visa inf

  • AP - WLC joining issue

    We have 3 WLC's(5500) in our network and about 150 AP's. Only 4 AP's register to 1 controller, over 70 to 2nd and about 50 to 3rd. On checking & comparing few of the AP's this is what i concluded. 1. 4 AP's that registered to the first WLC did not ha

  • Dreamweaver MX Update Links broken

    In the post http://forums.adobe.com/message/5336083#5336083 Nancy O, posts several links to download links for Dreamweaver MX. The actual download link for the updater (http://www.adobe.com/cfusion/resourcecenter/rc_driver.cfm?pageName=dwmx%5Fupdater

  • Field Notice of assessment in Asset Master (Logical Field group 7)

    Dear Team, This is with regard to Field for Screen Layout in Asset Master. Under Logical Field group 7 (Real estate and similar rights), there is a field group with name 'Notice of assessment'. I would like to understand exactly what this field is me