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

Similar Messages

  • Foreign Key Relationship.

    Hi,
    I asked a question in the link below(which I solved).
    https://social.msdn.microsoft.com/Forums/en-US/514a4f48-b1fa-4fe7-bc2b-4c252b34cf50/insertion-of-null-value?forum=vbgeneral
    Here my question is that:
    We make relation of foreign keys to enforce user for accurate data insertion, but sometime we do need null values to be passed to attribute that is used as
    Foreign key. We studied and know that Foreign keys are meant to except null values, which it doesn't(In simple way. I mean when we select an attribute as foreign key and check it to except nulls, then it should.)
    As I explained in the upper link how I solved the problem, if similar problems have to be solved like that then,
    Why we define Foreign Key over there, i mean it could be done without defining the Foreign key?
    If it is necessary in any meaning then why we Enforce Foreign
    Key Constraint as No,
    to except nulls from user?
    What effects does Enforce Foreign Key Constraint as No
    gives to Foreign Key Constraints?
    Thnx in advance.
    Habib Ur Rehman

    Hello Habib,
    I do not think what you are seeing is normal and without having the exact DDL it will be hard to comment.
    I suspect,  you may had "NOT NULL" constraint on the foreign key column. thats why it is not allowing NULLs.
    By definition, Foreign Key maintains(enforces) referential integrity and does allows NULLs. So,  you should be able to enter nulls without having to disable the foreign key enforcement.  
    by setting
    Enforce Foreign Key Constraint to NO, you are effectively disabling the enforcement. you are invalidating the whole purpose of the constraint. 
    The purpose of having that option(disabling foreign key) is to fasten the data load process bypassing the check(extra work) to make sure the value exists in the primary key table when importing large amounts of data.  But to maintain the referential
    integrity you should have that enabled.
    I am not sure without having the ddl, where you did wrong.PLEASE POST YOUR DDL and it should be clear where it went wrong for you. but i hope this script below should help you understand...
    CREATE TABLE [dbo].[Make]([sid] [int] NULL unique,[sname] [varchar](50) NULL)
    go
    CREATE TABLE [dbo].[Make_FK]([sno] [int] NULL unique,[sname] [varchar](50) NULL,[sid] [int] NULL
    CONSTRAINT FK_MAKE FOREIGN KEY (sid) REFERENCES dbo.make (SID))
    insert into make(sid,sname)
    values(1,'sam')
    insert into dbo.Make_FK(sno,sname,sid)
    values(100,'somename',null)
    select * from Make
    --sid is null. SID is the foreig n key referring to sid in MAKE table.
    select * from Make_FK
    Hope it Helps!!

  • 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

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

  • 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

  • Does Berkeley DB Support Foreign key access?

    BDB Java Editon supports.
    But in BDB I cant find similar APIs.

    Hi Liu,
    The Java Collections API in Berkeley DB does not offer the foreign key functionality as the Java Collections API in Berkeley DB Java Edition. You'll have to maintain the integrity constraint imposed by a foreign key yourself through the application code.
    Regards,
    Andrei

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

  • When entering an address, I am always queried to allow forwarding or not. Does that mean I have a key-logger attached to my searches?

    When entering an address, I am always queried to allow forwarding or not. Does that mean I have a key-logger attached to my searches? The message says Firefox prohibited redirection to another page, even when I open my home page. Please advise.

    I would recommend posting in the iTunes forum: https://discussions.apple.com/community/itunes/itunes_for_mac

  • 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

  • OBIEE 11g: Fact table does not have  a properly defined primary key

    Hi,
    We have two fact tables and two dimension tables.
    We have joined the tables like
    D1-->F1
    D2-->F2
    D1-->D2
    We dont have any hieracies.
    It is throwing error in consistency check,
    [nQSError: 15001] Could not load navigation space for subject area ABC.
    [nQSError: 15033] Logical table Fact1 does not have a properly defined primary key.
    It is not like STAR Schema, its like snowflake schema. How to define primary key for fact table.
    Thanks.

    Hi,
    My suggestion would be bring both the facts to the same logical table sources and have a single fact table in the BMM layer joined with multiple dimensions.
    Build a dimension hierarchy for the dimensions and then in the content logical layer mapping, map the dimensions to the fact tables with detailed level/Total
    Refer the below link-
    http://108obiee.blogspot.com/2009/08/joining-two-fact-tables-with-different.html
    Hope this help's
    Thanks,
    Satya

  • Why to I FREQUENTLY have to reload my page to get hot links to work? Andmy keyboard frquently does not register key strokes, particularly the space bar.

    Why to I FREQUENTLY have to reload my page to get hot links to work? Andmy keyboard frquently does not register key strokes, particularly the space bar.

    # Right-click an empty area of the tab bar and choose Customize.
    # Click the Restore Defaults button, then the Exit Customize button.
    # If you don't already have it, install [https://addons.mozilla.org/firefox/addon/classicthemerestorer/ Classic Theme Restorer] and restart Firefox when prompted.
    # Open the Add-ons Manager (Ctrl+Shift+A; Mac: Command+Shift+A), then the Extensions category.
    # Next to Classic Theme Restorer, click the Options button.
    # On the Main tab, make sure "Movable back-forward button" and "Hide urlbars stop & reload buttons" are checked. You might also want to check "Combine stop & reload buttons". Close the options window when done.
    # Right-click an empty area of the tab bar and choose Customize.
    # Drag the Back/Forward, Stop and Reload buttons onto the navigation toolbar.
    # Click the Exit Customize button in the lower right corner when done.
    That being said, I should point out that you can reload pages in other ways.
    * Right-click any tab and choose Reload.
    * Right-click an empty area of the page and choose Reload.
    * Press F5.
    * Press Ctrl+R (Mac: Command+R).

  • 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

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

  • 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

Maybe you are looking for

  • How can I install Photoshop on a second computer?

    I have photoshop on a computer that is about to die and I downloaded it online so I don't have the original disks to put photoshop on my new computer how can I download it again or do I have to pay for another copy?

  • Weblogic 10.0mp1 domain migration over windows to weblogic12c

    Need to know the migration steps while migrating the weblogic domain 10.0 mp1 hosted over windows server 2008 to another windows machine and the weblogic should be 12.2 . The admin server is running with 3 managed server in prodcution.Can anyone sugg

  • Control Break Statements ----- Please Help me

    I have some idea about the control-break statements(AT-ENDAT). Can any one please give me the example ? Thanks in Advance. Points will be rewarded immediately.

  • BGP LOAD BALANCING

    BGP chooses only a single best path to reach a specific destination. BGP is not designed to perform load balancing.. what i learned from every doc... but suppose this two ebgp neighbor have multiple path, creating neighborship by using loopback ip, t

  • Could not migrate some archives from migration list when upgrade to 7.31

    Getting below error message. Any idea to solve it.. It seem like unable to locate the file. Error Log:JMT_FRAMEWORK_06.OUT   INFO: Loading tool launcher...   INFO: [OS: Windows Server 2008 R2] [VM vendor: SAP AG] [VM version: 1.6.0_31] [VM type: SAP