Necessary to have foreign keys ?

Dear All,
We have a oracle database, in which there is no foreign key constraints defined. But we are able to join the tables with the appropriate columns and getting the output of the query perfectly.
Now, is that mandatory to have foreign key columns defined or the current design is fine. Please help me in this, by discussing the advantages and disadvantages.
Thanks in advance.
Anand.

Best practice would absolutely be to have foreign keys in an OLTP system.
Without foreign keys, you are guaranteed to have data that violates referential integrity eventually. Unless you happen to be lucky enough to work on an application that will never have bugs, you need Oracle to enforce referential integrity.
Without foreign keys, your data model is much harder to understand. That means that when new developers come on the project, when people want to point various tools at the database, etc. those people and tools will not be able to use foreign keys to determine how the tables relate. That inevitably means that it will take them far longer to get up to speed and they are far more likely to make errors. If you ever point an automated tool at the database to generate an ERD, that tool will be nearly useless since it can't figure out how the tables relate.
Without foreign keys, you're depriving the optimizer of information that may allow it to generate better plans.
Yes, you need to index foreign keys in the child table, but you almost certainly need these indexes for queries anyway. Other than that, there are really no downsides. Yes, Oracle has to do a minute amount of work to verify the foreign key during DML, but it will be far more efficient for Oracle to do this than for your application to do it.
Justin

Similar Messages

  • In ADF how can i insert data in multiple table if they have foreign key

    I have started working on ADF and can anybody inform me in ADF how can i insert data in multiple table if they have foreign key,please?
    Thnak you very much.

    Hello,
    Still no luck.I am surely doing silly mistakes.Anyway,Here are my workings-
    1> student_mst (id(pk),studentname) and student_guard_mst(id(fk),guardianname)
    2> created EO from both of the tables,made id in both EO as DBSequence and an association was also generated.
    3> i made that association composite by clicking the checkbox
    4> i created 2 VO from 2 EO.
    5> put those VO in Application Module.
    6> dragged and dropped 2 VO on my jspx page and dropped them as ADF Form.
    Now what to do please?

  • Copying tables that have Foreign Keys

    All my tables are using primary/foreign keys. If you look at the example below, I have a table 'stu_school' which has 3 foreign keys (FK_YEAR,FK_SCHOOL,FK_GRADE_LEVEL) pointing to 3 other tables. Every year I need to create a new record in the year table and copy the data from the previous year (with the FK_YEAR value) into the new year. Is there a better approach to moving this data over other than manually writing SQL statements and making sure they are done in the correct order (becuase of the keys).
    stu_school
    PK_ID
    FK_YEAR
    FK_SCHOOL
    FK_GRADE_LEVEL
    year
    PK_ID
    YEAR
    school
    PK_ID
    SCHOOL
    grade_level
    PK_ID
    FK_YEAR
    FK_SCHOOL
    GRADE_LEVEL

    Since this is going to be an annual task and should therefore be part of the application I suggest writing a pl/sql script that performs the necessary actions rather than use sql to generate the sql to perform the action. A pl/sql script would allow better activity logging and error handling appropriate for a repeating production task.
    IMHO -- Mark D Powell --

  • 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

  • Adding a foreign key to an access table

    I have set up two tables in access and I am using SQL insert
    with the form to populate the database. When I “submit”
    the form I receive the following error:
    Element CUST_ID is undefined in CUSTOMER_INFO.
    The error occurred in
    C:\Inetpub\wwwroot\spl_web_site\public_records\order_insert.cfm:
    line 23
    21 : <cfquery datasource="#odbc_datasource#"
    name="desc">
    22 : insert into rec_results (customer_info.cust_id,rec_desc)
    23 : values('#customer_info.cust_id#',
    24 : '#rec_desc#')
    25 : </cfquery>
    How do I get cust_id into the second table to be used as the
    foreigh key when it “has not been created (defined)” in
    the first table?

    If your cust_id is an autonumber primary key field in your
    first table, then you will have to create that record first, then
    retrieve the primary key value to be used as your foreign key in
    your second table, especially if you have foreign key constraints
    on the table. This is normal relational database behavior, since it
    isn't really possible to have a child if there is no parent. In
    this case, you will need some sort or SELECT between your two
    inserts in order to determine the newly created cust_id. Or, you
    may need some other means of gerating your unique PK value for
    cust_id outside of Access. If you do the SELECT method, you would
    do something like SELECT MAX(cust_id) FROM customer_info (assuming
    that cust_id is generated sequentially), and you should enclose all
    three database transactions within CFTRANSACTION tags so that you
    don't accidentally grab the wrong cust_id from another simultaneous
    transaction.
    Phil

  • Foreign keys in source tables (sybase database)

    Hi,
    Foreign key / primary key constraints are not captured / converted in oracle model nor they are generated.
    I am migrating sybase database tables to oracle. These tables have foreign keys, but while capturing and then converting the table to oracle model and then generating, these foreign keys are not present in the ddl script.
    In the source connection ( in the tool) to the sybase database, these keys are shown,
    and logically when the generation is done for table, the foreign / primary key syntax should be present in the ddl generated
    Am more worried about the foreign key not getting converted
    Please help
    Regards

    Hello,
    do you have a very simple example showing this behaviour ?
    What is your sybase version ? And what about Sqldev version ?
    Do you tested migration with offline method ?
    As testcase, a emp and dept schema with foreign key should be ideal.
    Thanks
    Regards,
    Marcello

  • Foreign Key reference

    Hi all,
    I have two tables called PATIENT and PERSON. All persons are not patients but all patients are persons. Now how to define the relation between the two tables? Should PATIENT have foreign key reference to PERSON or should PERSON have foreign key reference to PATIENT?
    thanks in advance.

    It really depends what you want to accomplish. If 99% of people are patients, then it may be better to merge these two tables into one. If just a few people are patients, that a separate patient table is o.k. with the same key as in person table.

  • 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.

  • Hiding foreign keys

    Is there an option to hide foreign keys in Relational model? I have more then 1000 keys in model and all those lines in diagrams slows program.
    In the part of the model where are no foreign keys the transitions and scrolling are very smooth.
    Thanks!

    The problem is that you may want to include tables (or entities in ER diagrams) that have multiple foreign keys, and you only want to include some of the foreign keys on the diagram.
    Example:
    If the diagram contains tables A, B and C that all have foreign keys between them, you may want to show all foreign keys to and from table B but not the ones between A and C.
    / Marc de Oliveira

  • 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.

  • Does a foreign key have to be a primary key

    Hey all.I was checking on the database code written by sambapos.To my surprise, I found a foreign key that is not a primary key anywhere.
    Is that possible?
    If, so why?
    I am really astonished.

    Limitations and Restrictions
    A foreign key constraint does not have to be linked only to a primary key constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.
    When a value other than NULL is entered into the column of a FOREIGN KEY constraint, the value must exist in the referenced column; otherwise, a foreign key violation error message is returned. To make sure that all values of a composite foreign key constraint
    are verified, specify NOT NULL on all the participating columns.
    FOREIGN KEY constraints can reference only tables within the same database on the same server. Cross-database referential integrity must be implemented through triggers. For more information, see
    CREATE TRIGGER (Transact-SQL).
    FOREIGN KEY constraints can reference another column in the same table. This is referred to as a self-reference.
    A FOREIGN KEY constraint specified at the column level can list only one reference column. This column must have the same data type as the column on which the constraint is defined.
    A FOREIGN KEY constraint specified at the table level must have the same number of reference columns as the number of columns in the constraint column list. The data type of each reference column must also be the same as the corresponding column in the column
    list.
    The Database Engine does not have a predefined limit on either the number of FOREIGN KEY constraints a table can contain that reference other tables, or the number of FOREIGN KEY constraints that are owned by other tables that reference a specific table.
    Nevertheless, the actual number of FOREIGN KEY constraints that can be used is limited by the hardware configuration and by the design of the database and application. We recommend that a table contain no more than 253 FOREIGN KEY constraints, and that it
    be referenced by no more than 253 FOREIGN KEY constraints.
    FOREIGN KEY constraints are not enforced on temporary tables.
    If a foreign key is defined on a CLR user-defined type column, the implementation of the type must support binary ordering. For more information, see
    CLR User-Defined Types.
    A column of type varchar(max) can participate in a FOREIGN KEY constraint only if the primary key it references is also defined as type
    varchar(max).
    Read this article
    http://msdn.microsoft.com/en-us/library/ms189049.aspx
    Regards, Ashwin Menon My Blog - http:\\sqllearnings.com

  • Building relationships that have no foreign key

    Hi,
    I have a situation where the database team have provided me a schema
    something like that shown below:
    PERSON
    |========================================|
    | PK | NAME | SSNUM | MGR |
    |========================================|
    | 1 | john | 123-45-6789 | 3 |
    | 2 | fred | 987-65-4321 | 3 |
    | 3 | alice | 111-22-3333 | NULL |
    | ... |
    |========================================|
    COMPANY
    |=================|
    | PK | NAME |
    |=================|
    | 1 | acme |
    | 2 | bcme |
    | 3 | ccme |
    | |
    |=================|
    ATTRIBUTE
    |========================================|
    | PK | SOURCE_ID | SOURCE_TYPE | VALUE |
    |========================================|
    | 1 | 1 | P | one |
    | 2 | 1 | C | two |
    | ... |
    |========================================|
    PERSON and COMPANY are application objects. A PERSON or a COMPANY can
    have an ATTRIBUTE tagged to it. Now, there is not a foreign key
    relationship from PERSON/COMPANY to ATTRIBUTE. The relationship can be
    derived by understanding that ATTRIBUTE.SOURCE_ID is the PK value for
    either PERSON or COMPANY, and ATTRIBUTE.SOURCE_TYPE states whether its a
    PERSON (P) or COMPANY (C).
    The database guys say this is a common approach to modelling data that
    is not a "first order" property of a class, which I guess may be up for
    debate. In any case....
    I can't model this using objects and relationships using KODO. So, my
    question is, how would you approach this?
    Thanks,
    Mike.
    PS As an aside, if there was a way in the JDO metadata to "hardcode" a
    value for one or more of the columns in a foreign key relationship, it
    would help a lot. Then I could build a navigable link from PERSON to
    ATTRIBUTE, where the SOURCE_ID data column is PERSON.PK and the
    SOURCE_TYPE is the string literal 'P'.

    That certainly is a unique problem... and certainly not a sort of model I
    would encourage...
    however, you should be able to accomplish this in Kodo with a non-direct
    relationship... i.e. Attributes has a reference-id not a reference. So
    Attribute may say look like this:
    private int referenceId = -1;
    private char type;
    public void getSource ()
    Object oid = null;
    if (referenceId < 0)
         return null;
    switch (type)
    case 'P':
         oid = new PersonId (referenceId);
         break;
    case 'C':
         oid = new CompanyId (referenceId);
         break:
    default: return null;
    return JDOHelper.getPeristenceManager (this).getObjectById (oid, true);
    public void setSource (Person p)
    PersonId pid = (PersonId) JDOHelper.getObjectId (p);
    refernceId = pid.id;
    type = 'P';
    Now this would get a little more complicated for objects which
    haven't been made persistent, but can simply be accomplished by using
    javax.jdo.InstanceCallbacks and preStore () and using a transient field
    Object source. Using non-application identity, would require casting oids to
    our ObjectIds.Id (in our JavaDoc). Actually, as I think about it, it may
    be better to do this all in a transient state:
    public Attribute
         private transient Object src;
         private int referenceId;
         private char type;
         public void setSource (Person p)
              src = p;
         public Object getSource ()
              if (src != null || JDOHelper.getPersistenceManager (this) == null)
                   return src;
              /// same as previous example
              return JDOHelper.getPersistenceManager (this).getObjectById (oid);
         public void preStore () // instancecallbacks
              PersonId id = (PersonId) JDOHelkper.getObjectId (src);
              referenceId = id.id;
              type = 'P';
    Again this isn't 100% solid code but a suggestion to get you started on
    your problem.
    While 3.0 will support more complex mappings, it won't get around the
    multiple types per field problem... but this could be solved via
    interfaces (e.g. public interface AttributeSource), though I'd have to
    defer to our 3.0 architect on that one.
    On Fri, 14 Feb 2003 16:15:04 +0200, Mike Hogan wrote:
    Hi,
    I have a situation where the database team have provided me a schema
    something like that shown below:
    PERSON
    |========================================|
    | PK | NAME | SSNUM | MGR |
    |========================================|
    | 1 | john | 123-45-6789 | 3 |
    | 2 | fred | 987-65-4321 | 3 |
    | 3 | alice | 111-22-3333 | NULL |
    | ... |
    |========================================|
    COMPANY
    |=================|
    | PK | NAME |
    |=================|
    | 1 | acme |
    | 2 | bcme |
    | 3 | ccme |
    | |
    |=================|
    ATTRIBUTE
    |========================================|
    | PK | SOURCE_ID | SOURCE_TYPE | VALUE |
    |========================================|
    | 1 | 1 | P | one |
    | 2 | 1 | C | two |
    | ... |
    |========================================|
    PERSON and COMPANY are application objects. A PERSON or a COMPANY can
    have an ATTRIBUTE tagged to it. Now, there is not a foreign key
    relationship from PERSON/COMPANY to ATTRIBUTE. The relationship can be
    derived by understanding that ATTRIBUTE.SOURCE_ID is the PK value for
    either PERSON or COMPANY, and ATTRIBUTE.SOURCE_TYPE states whether its a
    PERSON (P) or COMPANY (C).
    The database guys say this is a common approach to modelling data that
    is not a "first order" property of a class, which I guess may be up for
    debate. In any case....
    I can't model this using objects and relationships using KODO. So, my
    question is, how would you approach this?
    Thanks,
    Mike.
    PS As an aside, if there was a way in the JDO metadata to "hardcode" a
    value for one or more of the columns in a foreign key relationship, it
    would help a lot. Then I could build a navigable link from PERSON to
    ATTRIBUTE, where the SOURCE_ID data column is PERSON.PK and the
    SOURCE_TYPE is the string literal 'P'.--
    Stephen Kim
    [email protected]
    SolarMetric, Inc.
    http://www.solarmetric.com

  • Field MATNR does not have a foreign key; as a result no intervals are allow

    Hello,
    We are having ROH and FERT material , in that we were entered the wrong Item category group , we need to change the
    Item category group through mass posting through MM17. we are getting below given error
    "Field MATNR does not have a foreign key; as a result no intervals are allowed"
    Message no. M&132
    Diagnosis
    You have either specified an interval such as from 1 to 10 or M* for a field, or you have left the field blank, corresponding to the interval *. However, since there is no foreign key for this field, the system cannot resolve the interval into valid single values.
    Procedure
    Enter only single values for this field.
    We have checked the the structure MPGD_MASS in SE11, and here foreign key  mark is tciked in "entry help/ check.
    How to solve this error.
    Regards
    sapman man

    how did you then proceed, on the selection screen:
    did you just use the change tab or did you work as well or only at the create tab?
    What did you enter in the selection?
    (...because I just tried a mass change without specying a material number and do not get such message)

  • VLD-0203: Foreign key ENC_COLLECTION_TYPES_1 does not have a referenced col

    When I try to deploy a source table a got the error.
    VLD-0203: Foreign key ENC_COLLECTION_TYPES_1 does not have a referenced column.
    All foreign keys must have referenced columns. Complete the foreign key definition by referencing a column
    How can I fix this issue ??

    Hi,
    Just refer to the correct column for the FK created (you need to have a parent table available/deployed first before this is deployed). Since you are deploying the source table which will create the same in DB you have to make sure the keys are set correctly and the parent table is already available in DB.
    We generally create the tables first in DB and then use the import functionality to bring it into OWB repository.
    Regards,
    Bharadwaj Hari

  • I need to have multiple foreign keys on one column, can Designer do this?..

    Basically I've a table that needs to link to three other tables. As far as I can tell Designer will only let me have the foreign keys on seperate columns;
    Column_1 - FK1
    Column_2 - FK2
    Column_3 - FK3
    I think it's a much better database design is to have all three link to one column and have a Type column denoting which table to references
    Column_1 - FK1, FK2, FK3
    Column_2 - Type (FK1, FK2, FK3)

    Can you tell us more about the problem you're trying to solve?
    Thanks, George

Maybe you are looking for

  • Internal Server error while approving leave in absence management

    hi In my project absence mgt is applicable for Netherland employees. When an NL manager applies leave and his manager from puerto rico tries to approve it from UWL , the manager is getting the following ERROR on clicking on the leave request in his i

  • Discoverer viewer 10g without edit layout

    Hello, I'm using Discoverer 10. The old version I used when viewing the books at firefox the user could select a button of 'edit layout' so that he could change the table. In this new version I can not find that button. Any idea?

  • Query for a chart

    I have this query: select null link, creationdate  label, COUNT(PROBLEM) BER from INCIDENTS where PROBLEM=:p10_id AND SOURCE = 'BER' and creationdate between (select (min(creationdate)) from  INCIDENTS where PROBLEM=:p10_id) and SYSDATE group by crea

  • How can I edit persian or arabic text in Pages?

    I'm using Pages in my laptop, my iPhone, and iPad. In my laptop i can't edit any Persian or Arabic texts. What should I do?

  • How to do a sitemap

    Hey guys, i was reading online that submitting a sitemap to Google Webmaster Tools is a good idea. But how do i go about doing so? Help is appreciated, thanks